我的问题如下。我有用户可以添加和更改的属性和参数。 我已经设法使用Xstream创建新的xml结构。 但现在我想将存储在String变量中的新xml导入到某个位置的旧xml文件中。我怎样才能做到这一点?
存储在我要导入的String中的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
答案 0 :(得分:1)
嗯,XStream就是将XML映射到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);