here’s my situation.
I have a string containing XML data:
<tag>
<anotherTag> data </anotherTag>
</tag>
I take that string and I run it through this code to convert it to a Document:
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(new StringReader(sXMLString)));
}
catch (Exception e) {
// Parser with specified options can't be built
ceLogger.logError("Unable to build a new XML Document from string provided:\t" + e.getMessage());
return null;
}
The resulting xml is almost perfect. Its missing the data however and looks like this:
<tag>
<anotherTag />
</tag>
How can I copy over the text when creating an XML Document and why is it removing the text in the first place?
Edit:
The actual problem ended up being something along the lines of this:
While parsing through the XML structure with my own function this line is there:
if (curChild.getNodeType()==Node.ELEMENT_NODE)
sResult.append(XMLToString((Element)children.item(i),attribute_mask));
But no such logic exists for TEXT nodes, so they are simply ignored.
Your code is correct. The only guess I can make is that you are outputting your code incorrectly. I’ve tested your code, and used the following method to output, and the XML was displayed correctly with the text node:
The output was: