0

I am unable to read attribute value from the xml file below. I am new to XPath and tried all solutions available on the net with no luck.

Below is input xml

<?xml version="1.0" encoding="UTF-8"?>
  <ns0:TPML xmlns:ns0="urn:x-schemas-xx:tpmbase/tpml/tpml_1_2">
<ns1:tpHeader xmlns:ns1="urn:x-schemas-xx:tpmbase/tpml/tpheader_1_2">
    <ns1:messageTimestamp>2010-02-22T11:49:24.13Z</ns1:messageTimestamp>
    <ns1:messageSender>Sml2Tpml</ns1:messageSender>
    <ns1:originatingSystem>
        <ns1:name>SummitGboUtp</ns1:name>
        <ns1:location>LDN</ns1:location>
    </ns1:originatingSystem>
    <ns1:trackingId>3345069L</ns1:trackingId>
    <ns1:tradeId>3345069L</ns1:tradeId>
    <ns1:transactionId/>
    <ns1:versionNo>1</ns1:versionNo>
 </ns1:tpHeader>
</ns0:TPML>

and here is java code

package com.db.test.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class SummitTestXSL 
{
    public static void main(String arg[])
    {

        try 
        {
            FileInputStream file = new FileInputStream(new File("TestXml.xml"));
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder =builderFactory.newDocumentBuilder();
            Document xmlDocument = builder.parse(file);
            XPath xPath = XPathFactory.newInstance().newXPath();
            xPath.setNamespaceContext(new MyNamespaceContext());

            System.out.println("*************************");

            String expression="/ns0:TPML/ns1:tpHeader/ns1:messageTimestamp";

            System.out.println(expression);

            String str = xPath.compile(expression).evaluate(xmlDocument);

            System.out.println(str);

        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally
        {

        }

    }


    private static class MyNamespaceContext implements NamespaceContext {

        public String getNamespaceURI(String prefix) {
            if("ns0".equals(prefix)) {
                return "urn:x-schemas-xx:tpmbase/tpml/tpml_1_2";
            }
            else if("ns1".equals(prefix))
            {
                return "urn:x-schemas-xx:tpmbase/tpml/tpheader_1_2";
            }
            return null;
        }

        public String getPrefix(String namespaceURI) {
            return null;
        }

        public Iterator getPrefixes(String namespaceURI) {
            return null;
        }

    }
}

I want to read the value of messageTimeStamp.

Jeff B
  • 8,572
  • 17
  • 61
  • 140
Ravi
  • 21
  • 2
  • What's the error you experience with the code in your question? – Artjom B. May 09 '14 at 13:08
  • possible duplicate of [javax.xml, XPath is not extracted from XML with namespaces](http://stackoverflow.com/questions/23203421/javax-xml-xpath-is-not-extracted-from-xml-with-namespaces) – JLRishe May 09 '14 at 13:12

1 Answers1

1

It doesn't look like you are setting NamespaceAware on your DocumentBuilderFactory. You need to do this before you create your builder:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);     //  <---- This line
DocumentBuilder builder =builderFactory.newDocumentBuilder();
JLRishe
  • 99,490
  • 19
  • 131
  • 169