Reputation: 323
I am having xml file called temp.xml with the below content.
<?xml version="1.0" encoding="UTF-8" standalone="no"?><combinedstaff>
<staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff></combinedstaff>
I am having xml string this way,
String sampleXML="<staff><firstname>fff</firstname><lastname>lll</lastname><nickname>nnn</nickname><salary>$20,000</salary></staff>";
I need to append this xml string to the above xml file, so that final xml should look this way,
<?xml version="1.0" encoding="UTF-8" standalone="no"?><combinedstaff>
<staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>
<staff><firstname>fff</firstname><lastname>lll</lastname><nickname>nnn</nickname><salary>$20,000</salary></staff>
</combinedstaff>
I have coded this way, But I am getting some junk characters like ;<
are added to the xml string this way,
<?xml version="1.0" encoding="UTF-8" standalone="no"?><combinedstaff>
<staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff><staff><staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff></staff><staff><staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff></staff><staff><staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff></staff></combinedstaff>
Here is my code,
public class writexml1 {
public static void main (String args[])
{
File docFile = new File(".xml");
Document doc = null;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(docFile);
}
catch (java.io.IOException e)
{
System.out.println("Can't find the file");
}
catch (Exception e)
{
System.out.print("Problem parsing the file.");
}
Element root = doc.getDocumentElement();
System.out.println("The root element is " + root.getNodeName() + ".\n");
NodeList children = root.getChildNodes();
System.out.print("There are "+children.getLength()+" child elements.\n");
System.out.print("They are: \n");
Element staffElement = doc.createElement("staff");
Node updateText = doc.createTextNode("<firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>");
staffElement.appendChild(updateText);
root.appendChild(staffElement);
try{
String outputURL = "temp.xml";
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new FileOutputStream(outputURL));
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
How can i solve this issue? Where is the issue ?
Can someone help?
Upvotes: 0
Views: 3971
Reputation: 1061
Dont add your nested elements as text.
The line:
Node updateText = doc.createTextNode("<firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>");
What you want to do is pare this string as the XML it is and then append it to the the combinedstaff-element.
Take a peek at the accepted answer here on how to create XML from a string: How to create a XML object from String in Java?
And apply that solution on your xml-string:
"<staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>"
Afterwards you take the resulting document and add it to you original XML. By doing something like this:
Node newNode = doc.importNode(theNewDocument.getDocumentElement() , true); //Need to import prior to appending it
doc.getDocumentElement().appendChild(newNode);
Its a much better solution to use the DOM to append content rather than trying to do it manully.
Upvotes: 2
Reputation: 2288
Try out using
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
Upvotes: 0