简体   繁体   English

在java中从XML获取子节点的值

[英]Getting value of child node from XML in java

My xml file looks like this我的 xml 文件看起来像这样

 <InNetworkCostSharing>
                <FamilyAnnualDeductibleAmount>
                    <Amount>6000</Amount>
                </FamilyAnnualDeductibleAmount>
                <IndividualAnnualDeductibleAmount>
                    <NotApplicable>Not Applicable</NotApplicable>
                </IndividualAnnualDeductibleAmount>
                <PCPCopayAmount>
                    <CoveredAmount>0</CoveredAmount>
                </PCPCopayAmount>
                <CoinsuranceRate>
                    <CoveredPercent>0</CoveredPercent>
                </CoinsuranceRate>
                <FamilyAnnualOOPLimitAmount>
                    <Amount>6000</Amount>
                </FamilyAnnualOOPLimitAmount>
                <IndividualAnnualOOPLimitAmount>
                    <NotApplicable>Not Applicable</NotApplicable>
                </IndividualAnnualOOPLimitAmount>
 </InNetworkCostSharing>

I am trying to get Amount value from <FamilyAnnualDeductibleAmount> and also from <FamilyAnnualOOPLimitAmount> .我试图从获得金额值<FamilyAnnualDeductibleAmount>也从<FamilyAnnualOOPLimitAmount> How do i get those values in java?我如何在java中获取这些值?

You may use two XPath queries /InNetworkCostSharing/FamilyAnnualDeductibleAmount and InNetworkCostSharing/FamilyAnnualOOPLimitAmount or just get the node InNetworkCostSharing and retrieve the values of its two direct children.您可以使用两个 XPath 查询/InNetworkCostSharing/FamilyAnnualDeductibleAmountInNetworkCostSharing/FamilyAnnualOOPLimitAmount或者只获取节点InNetworkCostSharing并检索其两个直接子节点的值。

Solution using XPath:使用 XPath 的解决方案:

// load the XML as String into a DOM Document object
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
ByteArrayInputStream bis = new ByteArrayInputStream("YOUR XML".getBytes());
Document doc = docBuilder.parse(bis);

// XPath to retrieve the content of the <FamilyAnnualDeductibleAmount> tag
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("/InNetworkCostSharing/FamilyAnnualDeductibleAmount/text()");
String familyAnnualDeductibleAmount = (String)expr.evaluate(doc, XPathConstants.STRING);

Try something like this(use getElementsByTagName to get the parent nodes and then get the value be reaching out to child node):尝试这样的事情(使用 getElementsByTagName 获取父节点,然后获取值到达子节点):

   File xmlFile = new File("NetworkCost.xml");
   DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
   Document doc = dBuilder.parse(xmlFile );
   doc.getDocumentElement().normalize();

   NodeList nList = doc.getElementsByTagName("FamilyAnnualDeductibleAmount");
   String familyDedAmount = nList.item(0).getChildNodes().item(0).getTextContent();

   nList = doc.getElementsByTagName("FamilyAnnualOOPLimitAmount");
   String familyAnnualAmount = 
                            nList.item(0).getChildNodes().item(0).getTextContent();

StAX based solution:基于 StAX 的解决方案:

    XMLInputFactory f = XMLInputFactory.newInstance();
    XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("test.xml"));
    while (rdr.hasNext()) {
        if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
            if (rdr.getLocalName().equals("FamilyAnnualDeductibleAmount")) {
                rdr.nextTag();
                int familyAnnualDeductibleAmount = Integer.parseInt(rdr.getElementText());
                System.out.println("familyAnnualDeductibleAmount = " + familyAnnualDeductibleAmount);
            } else if (rdr.getLocalName().equals("FamilyAnnualOOPLimitAmount")) {
                rdr.nextTag();
                int familyAnnualOOPLimitAmount = Integer.parseInt(rdr.getElementText());
                System.out.println("FamilyAnnualOOPLimitAmount = " + familyAnnualOOPLimitAmount);
            }
        }
    }
    rdr.close();

Note that StAX is especially good for cases like yours, it skips all unnecessary elements reading only the ones you need请注意,StAX 特别适合像您这样的情况,它会跳过所有不必要的元素,只读取您需要的元素

I think I found the solution with this question from stackoverflow我想我从stackoverflow找到了这个问题的解决方案

Getting XML Node text value with Java DOM 使用 Java DOM 获取 XML 节点文本值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM