Quick JDK Default XPath XML parser

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

	public void quickParseCBISResposne() {

		XPath xpath = XPathFactory.newInstance().newXPath();
		String response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				+ "<ApplicationTransactionId xmlns=\"http://www.infocomp.com/cbis/messages/Application/1.0\">"
				+ "<transactionId>6426072</transactionId>"
				+ "<allPriced>false</allPriced>"
				+ "</ApplicationTransactionId>";

		Document document = null;
		try {
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
			document = builder
					.parse(new InputSource(new StringReader(response)));
		} catch (Exception e) {
			// throw new
			// RuntimeException("The UK New Cash response message could not be parsed. \n\n"
			// + message, e);

		try {
			xpath.evaluate("//transactionId", document);
//xpath.evaluate("//*[local-name() = 'transactionId']", document);//this would ignore name space
		} catch (XPathExpressionException e) {
			// TODO Auto-generated catch block


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s