Reputation: 1147
My problem is the following. I have properties and parameters that a user can add and change. I have managed to create the new xml structure using Xstream. But now I want to import the new xml that is all stored in a String variable into my old xml file in a certain location. How Can I do this?
The Xml stored in a String that i want to import :
<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>
the xml structure is the following :
<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>
I cant really find any method in the Xstream documentation that performs this.
Ive tried this but its not XStream so i cant use it.
Update :
I am using this code :
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;
}
And it gives me this error :
[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
Upvotes: 0
Views: 888
Reputation: 3592
Well, XStream is all about mapping XML to Java objects and back again.
So to solve you problem either
The 2nd approach should be easier if you are already a bit comfortable with XStream and Java I guess.
So you 1st need something like this (in Java pseudo code):
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);
Upvotes: 1