1

My xml file looks like this

 <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>. How do i get those values in java?

1
  • You need to navigate the tree structure to extract those values Commented Mar 27, 2016 at 4:26

4 Answers 4

4

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.

Solution using 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);
Sign up to request clarification or add additional context in comments.

Comments

1

StAX based solution:

    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

Comments

1

Try something like this(use getElementsByTagName to get the parent nodes and then get the value be reaching out to child node):

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

2 Comments

@yogsma I think its item(0) in child nodes as well. Change and try. If still fails, let me know, I will try myself and let you know. Its definitely those indexes.
I tried changing it ..I hope you noted there "Amount" tag after FamilyAnnualDeductibleAmount tag.
0

I think I found the solution with this question from stackoverflow

Getting XML Node text value with Java DOM

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.