1

我的 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>

我正在尝试从中获取 Amount 值<FamilyAnnualDeductibleAmount>,也从<FamilyAnnualOOPLimitAmount>. 我如何在java中获得这些值?

4

4 回答 4

4

您可以使用两个 XPath 查询/InNetworkCostSharing/FamilyAnnualDeductibleAmountInNetworkCostSharing/FamilyAnnualOOPLimitAmount或者只获取节点InNetworkCostSharing并检索其两个直接子节点的值。

使用 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);
于 2012-11-27T19:15:06.823 回答
1

尝试这样的事情(使用 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();
于 2012-11-27T19:16:48.257 回答
1

基于 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();

请注意,StAX 对于像您这样的情况特别有用,它会跳过所有不必要的元素,只读取您需要的元素

于 2012-11-27T19:23:22.613 回答
0

我想我从stackoverflow找到了这个问题的解决方案

使用 Java DOM 获取 XML 节点文本值

于 2012-12-19T17:27:31.300 回答