【问题标题】:How to retrieve all the child tags based on given parent tag using XML and Java?如何使用 XML 和 Java 根据给定的父标签检索所有子标签?
【发布时间】:2018-01-16 11:05:35
【问题描述】:

我有一个 XML 文件,我想读取给定 Parent 标记的所有子节点/标记值。

示例 XML 内容:

<ns2:customerSummary>
               <ns2:address>
                  <ns2:city>SOUTH CHESTERFIELD</ns2:city>
                  <ns2:country>USA</ns2:country>
                  <ns2:isoCountryCode>US</ns2:isoCountryCode>
                  <ns2:line1>9998, N. MICHIGAN ROAD.</ns2:line1>
                  <ns2:postalCode>23834</ns2:postalCode>
                  <ns2:state>VA</ns2:state>
               </ns2:address>
               <ns2:allowPasswordChange>true</ns2:allowPasswordChange>
               <ns2:arpMember>false</ns2:arpMember>
               <ns2:brandCode>RCI</ns2:brandCode>
               <ns2:brandId>1</ns2:brandId>
               <ns2:companyCode>RCI</ns2:companyCode>
               <ns2:eliteMemberRewardStatus>false</ns2:eliteMemberRewardStatus>
               <ns2:eliteRewardStatus>true</ns2:eliteRewardStatus>
               <ns2:europePointsClubMember>false</ns2:europePointsClubMember>
               <ns2:firstName>FRANK</ns2:firstName>
               <ns2:homePhone>804/733-3004</ns2:homePhone>
               <ns2:isoCurrencyCode>USD</ns2:isoCurrencyCode>
               <ns2:isoLanguageCode>EN</ns2:isoLanguageCode>
               <ns2:language>EN</ns2:language>
               <ns2:lastName>BROWNING B</ns2:lastName>
               <ns2:locale>en_US</ns2:locale>

例如,如果我提供像 "ns2:customerSummary" 这样的标签作为父标签,它应该返回所有兄弟/子节点及其数据或者如果父标签是 "ns2 :address" 它应该像城市、国家等一样返回。

我试过这样,但它需要从顶层开始。

public static void getAllChildTags(String strXmlFile, String strParentTag) {

    String strXmlFileName = System.getProperty("user.dir") + File.separator + "\\resources\\customers.xml";
    String tagName = "ns2:customerSummary";

    // Parse Xml File
    parseXmlFile(strXmlFileName);

    //get the root element
    Element docEle = dom.getDocumentElement();

    Node childNode = docEle.getFirstChild();

    while (childNode.getNextSibling() != null)
    {
        childNode = childNode.getNextSibling();

        if (childNode.getNodeType() == Node.ELEMENT_NODE)
        {
            Element childElement = (Element) childNode;
            System.out.println("Node -> " + childElement.getNodeName() + " Value -> " + childElement.getNodeValue());
        }
    }
}

【问题讨论】:

    标签: java xml automation xml-parsing


    【解决方案1】:

    我得到了逻辑并编写了脚本,用于根据给定的父标签获取所有子元素数据。看看这个。

    // This method returns all the child elements of the given Parent tag
    public static void getAllChildTags(String strXmlFile, String strParentTag) {
    
        // Parse Xml File
        parseXmlFile(strXmlFile);
    
        NodeList flowList = dom.getElementsByTagName(strParentTag);
    
        for (int i = 0; i < flowList.getLength(); i++)
        {
            NodeList childList = flowList.item(i).getChildNodes();
    
            for (int j = 0; j < childList.getLength(); j++)
            {
                Node child = childList.item(j);
                if (!child.getNodeName().equals("#text"))
                {
                    System.out.println(child.getNodeName() + " -> " + childList.item(j).getTextContent());
                }
            }
        }
    }
    

    谢谢,

    【讨论】:

      猜你喜欢
      • 2018-11-01
      • 2020-01-03
      • 2016-05-21
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多