【发布时间】:2014-01-24 12:52:15
【问题描述】:
我的问题如下。我有用户可以添加和更改的属性和参数。 我已经设法使用 Xstream 创建了新的 xml 结构。 但是现在我想将所有存储在 String 变量中的新 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
【问题讨论】:
-
你能解释一下原始文件的结构吗?缩进似乎表明 prop、param 和 restri 元素是 Object_type 文档的子元素。但是 Object_type 元素在 prop 元素开始之前结束。
-
您的
getRootFile和getNewContent方法不断将文本替换为您已阅读的最后一行,因此XML 文档无效。但是,您不需要从文件读取到字符串,XStream 可以直接从文件读取。