cancel
Showing results for 
Search instead for 
Did you mean: 

Using XPath in Java Mapping

shankul_saxena
Explorer
0 Kudos

Hi Guys,

I am using XPath in my Java Mapping and unfortunately not getting out from the node. Please find piece of code below using which I am extracting Value from node.

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

  DocumentBuilder db=factory.newDocumentBuilder();

  XPathFactory xfactory=XPathFactory.newInstance();

  XPath xpath=xfactory.newXPath();

  String exp=null;

  InputSource is=new InputSource();

  NodeList nl=null;

  Node node=null;

  ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());

  Document doc=db.parse(bis);

  exp= "/ns0:MT_Source_JM/Record//Employee[2]/text()";

  node=(Node)xpath.evaluate(exp,doc,XPathConstants.NODESET);

  System.out.println("XPath Value=["+node.getNodeName()+"]");

  }

  catch(Exception e)

  {

  e.printStackTrace();

  }

My XML is below

<ns0:MT_Source_JM xmlns:ns0="http://aa.com/test">

<Record>

<Employee id="1">

<age>29</age>

<name>Pankaj</name>

<gender>male</gender>

<role>Java Developer</role>

</Employee>

<Employee id="2">

<age>35</age>

<name>Lisa</name>

<gender>male</gender>

<role>CEO</role>

</Employee>

</Record>

</ns0:MT_Source_JM>

I need your help in executing this java code through which I can access node value through Xpath or help me with links where I can find help.

Regards,

Shankul

Accepted Solutions (1)

Accepted Solutions (1)

vadimklimov
Active Contributor
0 Kudos

Hi Shankul,

Which node's text would you like to access in particular? Node "Employee" is not a text node, so attempt to access its text will return no result.

Few comments regarding source code you use:

  • XPath expression

exp= "/ns0:MT_Source_JM/Record//Employee[2]/text()";

Don't use namespace prefix, you can omit it here. As already mentioned above, making node test for a text against a node that is not a text node, will not yield result. So please also specify a text node, which value you need to extract - for example, if you need to retrieve's the second employee's name, then an expression will be /MT_Source_JM/Record/Employee[2]/name/text().

  • XPath evaluation return type

node=(Node)xpath.evaluate(exp,doc,XPathConstants.NODESET);

There is a mismatch between return type (Nodeset) and casted type (Node). If an XPath expression retrieves only a single node, then instead of XPathConstants.NODESET, make use of XPathConstants.NODE.

  • Access to node's text

System.out.println("XPath Value=["+node.getNodeName()+"]");

Here, instead of getNodeName(), getNodeValue() shall be used when accessing node's text value.

Regards,

Vadim

shankul_saxena
Explorer
0 Kudos

Thanks Vadim.

I have tried using your way but it was giving error

vadimklimov
Active Contributor
0 Kudos

Hi Shankul,

Here is a small standalone program I used to verify XPath result for your example (source_msg.xml is the file to which I pasted XML content provided in your original post):


import java.io.File;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.xpath.XPathConstants;

import javax.xml.xpath.XPathExpression;

import javax.xml.xpath.XPathExpressionException;

import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.xml.sax.SAXException;

public class XPathChecker {

  public static void main(String[] args)

  throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {

  File srcFile = new File("source_msg.xml");

  String xPathValue = "/MT_Source_JM/Record/Employee[2]/name/text()";

  DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();

  DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

  Document doc = docBuilder.parse(srcFile);

  XPathFactory xPathFactory = XPathFactory.newInstance();

  XPathExpression xPathExpression = xPathFactory.newXPath().compile(xPathValue);

  Node nodeEmployeeNameText = (Node) xPathExpression.evaluate(doc, XPathConstants.NODE);

  System.out.println(nodeEmployeeNameText.getNodeValue());

  }

}

And this is a result of its execution for me:

Can you please provide details about error you get?

Regards,

Vadim

shankul_saxena
Explorer
0 Kudos

Thanks Vadim for all your help. Your code helped me a lot.

With best regards,

Shankul

Answers (2)

Answers (2)

shankul_saxena
Explorer
0 Kudos

Thanks Guys for all your help in resolving this issue. I have tried using below code

exp= "/MT_Source_JM/Record/Employee/role";

  String xmlstring=xpath.compile(exp).evaluate(doc);

  NodeList list=(NodeList)xpath.compile(exp).evaluate(doc,XPathConstants.NODESET);

  for(int i=0;i<list.getLength();i++)

  {

  System.out.println("Xpath Values["+i+"]="+list.item(i).getFirstChild().getNodeValue());

  }

It is working fine for list. Can you also suggest me if I can XPathConstants.NODE for a single node value?

Regards,

Shankul

nabendu_sen
Active Contributor
0 Kudos