I have the following problem. I have an XML inside an XML. See example:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE ..."><AIDEM><OSERVER>xxx</OSERVER>
<OBJECT SystemID = "111" ObjectID = "00000004009e8bc1" Docu = "some value" DirectoryID = "111" InternalType = "1" TemplateID = "1234" TemplateType = "6" TemplateName = "String">
<OHEADER><OFIELD FieldID = "1" FieldType = "3" FieldName = "string" IsEmpty = "no"><ODATETIME>11111</ODATETIME></OFIELD>
<OFIELD FieldID = "123" FieldType = "3" FieldName = "string" IsEmpty = "no"><ODATETIME>11111</ODATETIME></OFIELD>
<OFIELD FieldID = "124" FieldType = "1" FieldName = "string" IsEmpty = "no"><TEST_STRING><mos>
<ID>some.some.some</ID>
<sID>some.some</sID>
<mID>53570320</mID>
<mObj>
<oID>cl178317481</oID>
....
</TEST_STRING></OFIELD>
In this example, the inner XML in the OFIELD is defined with ID 124. This is 99% true, but could also be in another field. Now I would like to extract the inner XML from the upper XML and create it into a new XML and save the original one without the inner xml. At the end I want two new xml's, one without inside xml and one only the inner xml. Which library packages do i need to solve this problem? I am very grateful for every tip.
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
File file = new File("example.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String mystring = writer.toString();
System.out.println("String XML: \n" + mystring);
}
}
With my example I have everything in one string, but I don't have the idea how to process the inner XML correctly. To be honest, the formatting irritates me a bit. What is a good way to do this and pack the inner XML into a new XML? Thanks in advance.