我有一個名爲temp.xml的XML文件,下面的內容。如何使用JAVA將XML字符串添加到現有的XML文件中?
<?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>
我有XML字符串這樣,
String sampleXML="<staff><firstname>fff</firstname><lastname>lll</lastname><nickname>nnn</nickname><salary>$20,000</salary></staff>";
我需要這個XML字符串追加到上面的XML文件,這樣最終的XML應該這樣看,
<?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>
我用這種方式編碼,但我得到一些像;<
這樣的垃圾字符被添加到這種xml字符串,
<?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>
這裏是我的代碼,
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();
}
}
}
我怎樣才能解決這個問題呢?問題在哪裏?
有人可以幫忙嗎?
這個問題太長了,而且是通用的 - 請嘗試更多地分離問題或考慮將其移至:http://codereview.stackexchange.com/ –