java中基于条件的多级xml节点读取

jfewjypa  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(371)

我正在使用下面的xml结构并阅读 GUID 价值来自 Address 节点在哪里 UsageCode 值设置为“标准”。代码运行良好,但现在 AddressUsage 段由源系统重新定义为可选,因此 UsageCode 不是全部返回 Address xml中的节点。

<BusinessPartner>
   <ID>2494271</ID>
   <Address>
      <GUID>000d8a5c-8bc5-1eda-b8cc-53078a383c3f</GUID>
      <ValidityPeriod>
         <StartDate>2020-08-06</StartDate>
         <EndDate>9999-12-31</EndDate>
      </ValidityPeriod>
   </Address>
   <Address>
      <GUID>000d8a5c-8bc5-1eda-b8cc-53078a384c5f</GUID>
      <ValidityPeriod>
         <StartDate>2020-08-06</StartDate>
         <EndDate>9999-12-31</EndDate>
      </ValidityPeriod>
      <AddressUsage>
         <UsageCode>STANDARD</UsageCode>
      </AddressUsage>
   </Address>
</BusinessPartner>

上面的xml可以有多个 Address 段,和 AddressUsage 节点是可选的。由于失踪 AddressUsage 节点此代码现在返回错误 GUID (从一开始) Address 节点)。
有什么建议可以解决这个问题吗?

// Get Document Builder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

// Build Document
Document document = builder.parse(new File("BPData2.xml"));

// Normalize the XML Structure;
document.getDocumentElement().normalize();

NodeList AddList = document.getElementsByTagName("AddressUsage");
for (int i = 0; i < AddList.getLength(); i++) {
    NodeList AddChildList = AddList.item(i).getChildNodes();
    for (int j = 0; j < AddChildList.getLength(); j++) {
        Node UsageChildNode = AddChildList.item(j);
        if ("UsageCode".equals(UsageChildNode.getNodeName()) && 
            "STANDARD".equals(AddChildList.item(j).getTextContent())) {
            NodeList AddUUID = document.getElementsByTagName("GUID");
            System.out.println(AddUUID.item(i).getTextContent());
            break;
        }
    }
}
hrysbysz

hrysbysz1#

UsageCode 是您感兴趣的标记,因此获取这些元素,而不是 AddressUsage . 对于每个元素,检查其文本内容是否为“标准”,如果是,则获取祖父母节点,即 Address 元素。然后遍历 Address 直到你找到 GUID . 获取的文本内容 GUID 你就完了。

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlPars2 {

    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new File("BPData2.xml"));
            document.getDocumentElement().normalize();
            NodeList usages = document.getElementsByTagName("UsageCode");
            int count = usages.getLength();
            for (int i = 0; i < count; i++) {
                Node n = usages.item(i);
                if ("STANDARD".equals(n.getTextContent())) {
                    Node parent = n.getParentNode();
                    Node grandparent = parent.getParentNode();
                    NodeList children = grandparent.getChildNodes();
                    int count2 = children.getLength();
                    for (int j = 0; j < count2; j++) {
                        Node target = children.item(j);
                        if ("GUID".equals(target.getNodeName())) {
                            System.out.println(target.getTextContent());
                        }
                    }
                }
            }
        }
        catch (IOException | ParserConfigurationException | SAXException x) {
            x.printStackTrace();
        }
    }
}

注意,上面的代码是基于您的问题中的示例xml的。

相关问题