1

Hoping someone could assist, i am trying to extract xml data using xpath in java. i would like to get a specific element value. but cannot seem to get it right, below is my code and xml document

nothing prints after : System.out.println("===============================================================");

<?xml version="1.0"?>
<ArrayOfPurchaseEntitites xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <PurchaseEntitites>
    <rInstalmentAmt>634.0</rInstalmentAmt>
    <rAnnualRate>12.0</rAnnualRate>
    <rInterestAmt>2670.0</rInterestAmt>
    <dFirstInstalment>3/31/2016 12:00:00 AM</dFirstInstalment>
    <dLastInstalment>8/31/2018 12:00:00 AM</dLastInstalment>
    <rInsurancePremium>1350.0</rInsurancePremium>
    <sResponseCode>00</sResponseCode>
  </PurchaseEntitites>
</ArrayOfPurchaseEntitites>

public void fetchXML()
{
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            try {
                URL url = new URL(urlString);
                HttpURLConnection connect =(HttpURLConnection)url.openConnection();
                connect.setReadTimeout(10000);
                connect.setConnectTimeout(15000);
                connect.setRequestMethod("GET");
                connect.setDoInput(true);
                connect.connect();

                InputStream stream = connect.getInputStream();
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document doc = documentBuilder.parse(stream);
                doc.getDocumentElement().normalize();


                XPath xpath = XPathFactory.newInstance().newXPath();
                System.out.println("===============================================================");
                // System.out.println("Root elements :" + doc.getDocumentElement().getTextContent());
                // XPathExpression tag = xpath.compile("/ArrayOfPurchaseEntitites/PurchaseEntitites/dFirstInstalment");
                 String xml = (xpath.evaluate("ArrayOfPurchaseEntitites/PurchaseEntitites/dFirstInstalment", doc.getDocumentElement().getTextContent()));
                 System.out.println(xml);

                stream.close();

            }
            catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (XPathExpressionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    thread.start();
}
  • Post your XML as text, not picture. I'd suggest to play around in XPath tester online (f.e http://www.xpathtester.com/xpath) to get the correct XPath expression, before incorporating the XPath into your android codes – har07 Feb 05 '16 at 08:12
  • [How to read XML using XPath in Java](http://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java) – har07 Feb 05 '16 at 08:28

1 Answers1

0

Add a namespace attribute xmlns="ns" after the xsi attribute in your xml file then try the following.

Change...

"ArrayOfPurchaseEntitites/PurchaseEntitites/dFirstInstalment"

to...

"/ns:ArrayOfPurchaseEntitites/ns:PurchaseEntitites/ns:dFirstInstalment"