0

这是我的 XML 文件。

<config> <properties> 
<entry key="delivaryDate">15-FEBRUARY-2013</entry> 
<entry key="externalCOID">123456</entry> 
<entry key="inputFileName">Generic_ChipONLY.chp</entry> 
<entry key="recordTotal">2</entry> 
<entry key="dueDate">15-MARCH-2013</entry>
</properties> </config>

例如,我想将“recordTotal”中的记录从 2 更改为 10,我该怎么做?现在我只知道使用 getElementsByTagName("entry") 获取。

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("customer.xml"));
NodeList entry = doc.getElementsByTagName("entry");
4

1 回答 1

0
 String xmlFile = "customer.xml";

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(xmlFile);

            //Retrieve the third entry node
            Node entryNode = doc.getElementsByTagName("entry").item(3);

            //Set new value
            entryNode.setTextContent("10");

            //Save to file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(xmlFile));
            transformer.transform(source, result);
于 2013-04-24T05:04:26.860 回答