我试图在一个exsting节点中插入一个新节点N次,但它对我不起作用,以下是我正在做的事情:
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filePath);
Node pNode = doc.getElementsByTagName(parentNode).item(0);
for (int i = 1; i <= count; i++) {
String nodeValue = value;
Element newNode = doc.createElement(childNode);
newNode.appendChild(doc.createTextNode(nodeValue));
pNode.appendChild(newNode);
}
这就是我想要实现的目标:
<parentNode>
<childNode> value1 </childNode>
<childNode> value2 </childNode>
...
<childNode> valueN </childNode>
</parentNode>
子节点的名称不会改变。 父节点不是根节点。 有人可以帮我弄清楚我错过了什么。
感谢。
答案 0 :(得分:0)
我为你检查了这个,它看起来像我预期的那样工作。您可能错过的是xml的更改是在内存中发生的?这就是我所做的:
输入xml文件
<rootNode>
<parentNode></parentNode>
</rootNode>
java测试类
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class DocBuilderTest {
private String filePath = "src/test/resource/doc.xml";
@Test
public void builder()
throws ParserConfigurationException, IOException, SAXException, TransformerException {
System.out.println(processFile("parentNode", "value", "childNode"));
}
private String processFile(String parentNode, String value, String childNode)
throws ParserConfigurationException, SAXException, IOException, TransformerException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(filePath);
Node pNode = doc.getElementsByTagName(parentNode).item(0);
for (int i = 0; i < 5; i++) {
String nodeValue = value + i;
Element newNode = doc.createElement(childNode);
newNode.appendChild(doc.createTextNode(nodeValue));
pNode.appendChild(newNode);
}
return toPrettyPrintString(doc);
}
// From https://stackoverflow.com/a/139096/7421645
private String toPrettyPrintString(Document doc) throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
return result.getWriter().toString();
}
}
pretty print reference如果文件已经部分缩进。
和输出到System.out
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<rootNode>
<parentNode>
<childNode>value0</childNode>
<childNode>value1</childNode>
<childNode>value2</childNode>
<childNode>value3</childNode>
<childNode>value4</childNode>
</parentNode>
</rootNode>
注意此输出只打印到System.out
,我没有覆盖输入文件。您需要初始化FileWriter
才能执行此操作。