有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Xstream在现有xml文件中导入带有节点的字符串?

我的问题如下。我有用户可以添加和更改的属性和参数。 我已经设法使用Xstream创建了新的xml结构。 但现在我想将存储在字符串变量中的新xml导入到我的旧xml文件中的某个位置。我该怎么做

存储在我要导入的字符串中的Xml:

<param>
  <PARAMETER>nidRB</PARAMETER>
  <DATA__TYPE>String</DATA__TYPE>
  <DESCRIPTION>A nice feature</DESCRIPTION>
  <MIN__NO>1</MIN__NO>
  <MAX__NO>1</MAX__NO>
  <ORDER1>1</ORDER1>
  <NESTED>0</NESTED>
  <DEFAULT1>NULL</DEFAULT1>
  <FORMAT>NULL</FORMAT>
</param>

xml结构如下所示:

<root>
<info>
</info>
  <type>
    <Object_type>blabla</Object_Type>
       <prop>
          <blab>...</blab>
        </prop>
       <param>
           <blab>...</blab>
        </param>
        <restri>
        </restri>
     <Object_type>blabla</Object_Type>
       <prop>
          <blab>...</blab>
        </prop>
        <param>
           <blab>...</blab>
        </param>
        <restri>
        </restri>
  <Object_type>blabla</Object_Type>
       <prop>
          <blab>...</blab>
        </prop>


      New XML DATA Inserted here

         <restri>
        </restri>
   </type>
</root>

在Xstream文档中,我真的找不到任何执行此操作的方法

我试过了,但它不是XStream,所以我不能使用它

Update : 

我使用的代码是:

public void buildNewFile() {

        XStream xstream = new XStream(new DomDriver());

        String myBigDocument = getRootFile();
        String myImportDocument = getNewContent();
        Type rootObject = (Type) xstream.fromXML(myBigDocument);
        Parameters param = (Parameters) xstream.fromXML(myImportDocument);

        Type type = (Type) rootObject.getTypes().get(0);

        type.setParam(param);

        String mergedXml = xstream.toXML(rootObject);
        System.out.println(mergedXml);
    }

    public String getRootFile() {

        String text = "";
        File file = new File("type.xml");
        try {
            Scanner scanner = new Scanner(file);

            while (scanner.hasNext()) {

                text = scanner.nextLine();

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        return text;

    }

    public String getNewContent() {

        String text = "";
        File file = new File("param.xml");
        try {
            Scanner scanner = new Scanner(file);

            while (scanner.hasNext()) {

                text = scanner.nextLine();

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        return text;

    }

这给了我一个错误:

[Fatal Error] :1:2: The markup in the document preceding the root element must be well-formed.
Exception in thread "main" com.thoughtworks.xstream.io.StreamException:  : The markup in the document preceding the root element must be well-formed.
    at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:105)
    at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:77)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1012)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1003)
    at 
xmleditor.service.CreateNewXMLData.buildNewFile(CreateNewXMLData.java:77)
    xmleditor.domain.Main.main(Main.java:10)
Caused by: org.xml.sax.SAXParseException: The markup in the document preceding the root element must be well-formed.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
    at com.thoughtworks.xstream.io.xml.DomDriver.createReader(DomDriver.java:98)
    ... 5 more

共 (1) 个答案

  1. # 1 楼答案

    XStream就是把XML映射到Java对象,然后再映射回来

    所以为了解决你的问题

    • 首先合并XML文档(如使用XSLT),然后通过XStream读取它以获得Java对象,或者以相反的方式进行
    • 读取这两个文件以创建对象结构,然后使用Java将一个对象设置为另一个对象的子对象

    如果您已经对XStream和Java有点熟悉,那么第二种方法应该会更容易

    所以首先需要这样的东西(在Java伪代码中):

    String myBigDocument; // Or provide XStream with a Reader on the input file or whereever you get the data from.
    String myImportDocument;
    Root rootObject = XStream.fromXml(myBigDocument);
    Param param = XStream.fromXml(myImportDocument);
    // now search the fitting element in your Root object and assign the parameter
    // I just select the 1st object from your types, you'll have to do this with the fitting one.
    Type type = rootObject.getInfo().getType().get(0);
    
    // Now set the child object
    type.setParam(param);
    
    // Now convert back to XML if you need that.
    String mergedXml = XStream.toXml(rootObject);