XML 文档 - 检查父节点然后遍历文档

XML Document - Checking Parent Node and then Iterating through the document

我在尝试遍历 XML 文档时遇到了挑战。请帮忙。

此代码适用于第一个文档

NodeList nodeList = xmlDocument.getElementsByTagName("add:MemberKey");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node currentNode = nodeList.item(i);
                if (currentNode.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                Element currentElement = (Element)currentNode;
                String tagValue = null;
                tagValue = getElementValue("add:MemberCID",currentElement);
                System.out.println(tagValue);
            }

这是我的第二份文件

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Body>
        <bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
          <ResultHeader>
            <abc:Version>1.0</abc:Version>
            <abc:ResultNumber>1000</abc:ResultNumber>
            <abc:ResultOutcome>Complete.</abc:ResultOutcome>
            <abc:ResultReason>New Result</abc:ResultReason>
          </ResultHeader>
          <p>
            <bbb:ActivationTime>20200111215217</bbb:ActivationTime>
            <bbb:MemberInst>
              <add:MemberKey>
                <add:MemberID>123458687</add:MemberID>
                <add:MemberSeq>0987654332456</add:MemberSeq>
              </add:MemberKey>
              <add:ActivationTime>20200111215217</add:ActivationTime>
              <add:DeativationTime>20370101000000</add:DeativationTime>
            </bbb:MemberInst>
            <bbb:MemberInst>
              <add:MemberKey>
                <add:MemberID>0987699544</add:MemberID>
                <add:MemberSeq>999999999999999</add:MemberSeq>
              </add:MemberKey>
              <add:ActivationTime>20200111215217</add:ActivationTime>
              <add:DeativationTime>20370101000000</add:DeativationTime>
            </bbb:MemberInst>
            <bbb:MemberInst>
              <add:MemberKey>
                <add:MemberID>4444444444</add:MemberID>
                <add:MemberSeq>4444444444444</add:MemberSeq>
              </add:MemberKey>
              <add:ActivationTime>20200111215217</add:ActivationTime>
              <add:DeativationTime>20370101000000</add:DeativationTime>
            </bbb:MemberInst>
          </CreateMemberResult>
        </bbb:CreateMemberResultAnnotation>
      </soapenv:Body>
    </soapenv:Envelope>

检查我的第二个文档的代码:我很挣扎

public static checkMessageType(org.w3c.dom.Document xmlDocument) {
    Element docEle = xmlDocument.getDocumentElement();
    NodeList nlist= docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
    int length = nlist.getLength(); // I am getting a 0 length here, 
    List<Element> childElement = new ArrayList<>();
    for (int i = 0; i < nlist.getLength(); i++) {
        //Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
       // if (node instanceof Element) {
           // childElement.add((Element) node);
        if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) nlist.item(i);
            if (el.getNodeName().contains("bbb:MemberInst")) {
                String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
                String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
            }
        }
 }

您的 xml 是否已解析? 第 10 行的 p 标签是什么?它没有在任何地方关闭。 在第 36 行也有一个 createMemberResult 结束标记,但在此之前没有打开标记

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <bbb:CreateMemberResultAnnotation xmlns:bbb="http://www.google.com">
      <ResultHeader>
        <abc:Version>1.0</abc:Version>
        <abc:ResultNumber>1000</abc:ResultNumber>
        <abc:ResultOutcome>Complete.</abc:ResultOutcome>
        <abc:ResultReason>New Result</abc:ResultReason>
      </ResultHeader>
        <bbb:ActivationTime>20200111215217</bbb:ActivationTime>
        <bbb:MemberInst>
          <add:MemberKey>
            <add:MemberID>123458687</add:MemberID>
            <add:MemberSeq>0987654332456</add:MemberSeq>
          </add:MemberKey>
          <add:ActivationTime>20200111215217</add:ActivationTime>
          <add:DeativationTime>20370101000000</add:DeativationTime>
        </bbb:MemberInst>
        <bbb:MemberInst>
          <add:MemberKey>
            <add:MemberID>0987699544</add:MemberID>
            <add:MemberSeq>999999999999999</add:MemberSeq>
          </add:MemberKey>
          <add:ActivationTime>20200111215217</add:ActivationTime>
          <add:DeativationTime>20370101000000</add:DeativationTime>
        </bbb:MemberInst>
        <bbb:MemberInst>
          <add:MemberKey>
            <add:MemberID>4444444444</add:MemberID>
            <add:MemberSeq>4444444444444</add:MemberSeq>
          </add:MemberKey>
          <add:ActivationTime>20200111215217</add:ActivationTime>
          <add:DeativationTime>20370101000000</add:DeativationTime>
        </bbb:MemberInst>
    </bbb:CreateMemberResultAnnotation>
  </soapenv:Body>
</soapenv:Envelope>

此处测试运行成功:

      @Test
   public void test() throws ParserConfigurationException, SAXException, IOException {
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      URL resource = getClass().getResource("/yourxml.xml");
      Document xmlDocument = builder.parse(resource.openStream());
      Element docEle = xmlDocument.getDocumentElement();
      NodeList nlist = docEle.getElementsByTagName("bbb:CreateMemberResultAnnotation");
      int length = nlist.getLength(); // I am getting a 0 length here,
      List<Element> childElement = new ArrayList<>();
      for (int i = 0; i < nlist.getLength(); i++) {
         // Node node = nlist.item(i).getOwnerDocument().getDocumentElement();
         // if (node instanceof Element) {
         // childElement.add((Element) node);
         if (nlist.item(i).getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) nlist.item(i);
            if (el.getNodeName().contains("bbb:MemberInst")) {
               String XMLMemberID = el.getElementsByTagName("add:MemberID").item(0).getTextContent();
               String XMLMemberSeq = el.getElementsByTagName("add:MemberSeq").item(0).getTextContent();
            }
         }

      }
   }