使用Java解析XML – 无法获取所有XML值

huangapple go评论74阅读模式
英文:

Parsing XML with java - does not get all xml values

问题

尝试解析 XML 以获取“CreDtTm”标签的值,以下是解析和编辑 XML 的方法(暂时跳过写回 XML 文件的部分):

public void modifyXmlFile(String filePath, Map<String, String> tagValuesToChange) {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document document = docBuilder.parse(filePath);
        XPath xpath = XPathFactory.newInstance().newXPath();

        for (Map.Entry<String, String> entry : tagValuesToChange.entrySet()) {
            Node node = (Node) xpath.compile(entry.getKey()).evaluate(document, XPathConstants.NODE); // **这个会变成 null**
            node.setTextContent(entry.getValue());
        }

基本上,<CreDtTm> 标签未被找到,而且 node 变量被设置为 null。不太清楚原因是什么?能帮我解决一下吗?

(注意:上面的翻译是你提供的内容的直接翻译,没有进行额外的解释或添加。)

英文:

Trying to parse xml to get "CreDtTm" tag value from this XML (pasted not the whole):

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;Document xmlns=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;&gt;
    &lt;CstmrCdtTrfInitn&gt;
        &lt;GrpHdr&gt;
            &lt;MsgId&gt;SANDISS_2020_10_08_001&lt;/MsgId&gt;
            &lt;CreDtTm&gt;2020-10-15T18:15:33&lt;/CreDtTm&gt;
            &lt;NbOfTxs&gt;3&lt;/NbOfTxs&gt;
            &lt;CtrlSum&gt;36.00&lt;/CtrlSum&gt;
            &lt;InitgPty&gt;
                &lt;Nm&gt;Bank&lt;/Nm&gt;
                &lt;Id&gt;
                    &lt;OrgId&gt;
                        &lt;Othr&gt;
                            &lt;Id&gt;40100&lt;/Id&gt;
                            &lt;SchmeNm&gt;
                                &lt;Cd&gt;COID&lt;/Cd&gt;
                            &lt;/SchmeNm&gt;
                        &lt;/Othr&gt;
                    &lt;/OrgId&gt;
                &lt;/Id&gt;
            &lt;/InitgPty&gt;
        &lt;/GrpHdr&gt;

However, getting not all values

使用Java解析XML – 无法获取所有XML值

使用Java解析XML – 无法获取所有XML值

Here is the method for parsing and editing xml (writing back to XML file is skipped atm)

 public void modifyXmlFile(String filePath, Map&lt;String, String&gt; tagValuesToChange) {
    try {
      DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
      Document document = docBuilder.parse(filePath);
      XPath xpath = XPathFactory.newInstance().newXPath();

      for (Map.Entry&lt;String, String&gt; entry : tagValuesToChange.entrySet()) {
        Node node = (Node) xpath.compile(entry.getKey()).evaluate(document, XPathConstants.NODE); //**This becomes null**
        node.setTextContent(entry.getValue());
      }

Basically <CreDtTm> tag is not found and node variable is set to null.
Not sure why? Can you help me out?

答案1

得分: 1

运行您的示例代码使我相信您在tagValuesToChange映射中的键是标签的名称,而不是有效的XPath表达式。请尝试使用//CreDtTm作为映射的键,然后查看是否有效。

当我将标签名称用作映射键时,我能够复现NullPointerException。使用我提供的XPath表达式,代码能够找到节点并更新文本内容。

英文:

Running you sample leads me to believe your key in the tagValuesToChange Map is the name of the tag and not a valid XPath expression. Try using //CreDtTm as the key to your map, and see if that works.

I was able to reproduce the NullPointerException when I used the name of the tag as the Map key. Using the XPath expression I suggested, the code was able to find the node and update the text content.

huangapple
  • 本文由 发表于 2020年10月19日 21:41:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64428631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定