我正在使用名为temp.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();
}
}
}
我该如何解决这个问题?这个问题在哪里?
有人可以帮忙吗?
答案 0 :(得分:1)
不要将嵌套元素添加为文本。
该行:
Node updateText = doc.createTextNode("<firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>");
你想要做的是将这个字符串作为XML,然后将它附加到combinedstaff元素。
在此处查看已接受的答案,了解如何从字符串创建XML: How to create a XML object from String in Java?
将该解决方案应用于xml-string:
"<staff><firstname>added firstname</firstname><lastname>added lastname</lastname><nickname>added nickname</nickname><salary>$10,000</salary></staff>"
然后,您获取生成的文档并将其添加到原始XML中。 做这样的事情:
Node newNode = doc.importNode(theNewDocument.getDocumentElement() , true); //Need to import prior to appending it
doc.getDocumentElement().appendChild(newNode);
这是一个更好的解决方案,可以使用DOM来附加内容,而不是尝试手动操作。
答案 1 :(得分:0)
尝试使用
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);