修改xml中特定标记的值

时间:2017-06-30 02:34:18

标签: java xml xml-parsing

我有一个xml如下

<?xml version="1.0" encoding="ISO-8859-1"?><TXNEXP FileDate="2017-05-23" FileName="/cortex/tsd/out/OPTSKRtxnexp20170523.xml" Instcode="SKR" TotNumTxns="74330">              
    <AUTHADV>
        <LOCALDATE>2017-05-22</LOCALDATE>
        <LOCALTIME>200011</LOCALTIME>
        <PAN>336890380<PAN>
    </AUTHADV>
    <AUTHREV>
        <LOCALDATE>2017-05-22</LOCALDATE>
        <LOCALTIME>200011</LOCALTIME>
        <PAN>336890380<PAN>
    </AUTHREV>
    <FINAL>
        <LOCALDATE>2017-05-22</LOCALDATE>
        <LOCALTIME>200011</LOCALTIME>
        <PAN>336890380<PAN>
    </FINAL>
   </TXNEXP>

现在,我正在修改PAN标记的值并将其写回xml,但我无法对所有PAN标记执行此操作。

这就是我在做什么。

NodeList node = doc.getElementsByTagName("TXNEXP"); 
Element emp = null;
for (int i = 0; i < node.getLength(); i++) {
  emp = (Element) node.item(i);
  Node name = emp.getElementsByTagName("PAN").item(0).getFirstChild();
          //Modifying the tag
 }

从上面的代码中,只有AUTHADV标签下的PAN被修改,其余两个值不会改变。

如何确保修改所有PAN标记?

1 个答案:

答案 0 :(得分:1)

这不是最漂亮的解决方案,但是在修复了PAN结束标记中丢失的斜杠后,这将有效。

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

class Extract {
    public static void main(String[] args) {

        try {
            File fXmlFile = new File("data.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            NodeList topNodes = doc.getElementsByTagName("TXNEXP");
            for (int i = 0; i < topNodes.getLength(); i++) {
                NodeList middleNodes = topNodes.item(i).getChildNodes();
                for (int j = 0; j < middleNodes.getLength(); j++) {
                    try {
                       NodeList theNodes = ((Element)middleNodes.item(j)).getElementsByTagName("PAN");
                       System.out.println(theNodes.item(0).getFirstChild().getNodeValue());
                        if (j == 1) {
                            // modify a value                                                                                                                                                               
                            theNodes.item(0).getFirstChild().setNodeValue("4567");
                            System.out.println(theNodes.item(0).getFirstChild().getNodeValue());
                        }

                    } catch (ClassCastException e) {}
                }
            }

        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

对于更好的方法,您可以使用XPath。

import javax.xml.xpath.*;

class Extract {
    public static void main(String[] args) {

        try {
            File fXmlFile = new File("data.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList nodes = (NodeList)xpath.evaluate("/TXNEXP/*/PAN", doc, XPathConstants.NODESET);
            for (int n = 0; n < nodes.getLength(); n++) {
                System.out.println(nodes.item(n).getFirstChild().getNodeValue());
                if (n == 1) {
                    nodes.item(n).getFirstChild().setNodeValue("4567");
                    System.out.println(nodes.item(n).getFirstChild().getNodeValue());
                }
            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }
}