Java HTML / XML How to - Get value from XPath








Question

We would like to know how to get value from XPath.

Answer

import java.io.IOException;
import java.io.StringReader;
//  w  ww .j  a v a 2s. co m
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

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

public class Main {

  public static void main(String[] args) throws Exception {
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();

    String xml1 = "<xml><product><product_id>2</product_id></product><car><product_id>12345678</product_id></car></xml>";
    String xml2 = "<xml><product><product_id>2</product_id></product><car><car_type id_1=\"2\" id_2=\"32\">55555</car_type></car></xml>";

    Document doc1 = stringToDom(xml1);
    Document doc2 = stringToDom(xml2);

    XPathExpression expr1 = xpath.compile("//car/product_id/text()");
    String carId = (String) expr1.evaluate(doc1, XPathConstants.STRING);

    XPathExpression expr2 = xpath.compile("//car/car_type/text()");
    String carType = (String) expr2.evaluate(doc2, XPathConstants.STRING);

    System.out.println("carId: " + carId);
    System.out.println("carType: " + carType);

  }

  public static Document stringToDom(String xmlSource) throws SAXException,
      ParserConfigurationException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(new InputSource(new StringReader(xmlSource)));
  }
}