我的问题如下。我有用户可以添加和更改的属性和参数。 我已经成功使用 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
最佳答案
嗯,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);
关于java - 如何使用 Xstream 在现有 xml 文件中导入带有节点的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21333232/