0

I am trying to access the Language attribute in the following xml using Xpath. I get an empty string. Can anyone tell me why?

<?xml version="1.0" encoding="UTF-8"?> <soa:Request xmlns:soa=\"http://www.site.coom/soa\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <BookingPriceRequest Client=\"5000000\" SiteID=\"500\" Currency=\"AAA\" Language=\"ar\"> <BookingItems> </BookingItems> </BookingPriceRequest> </soa:Request>

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder;
Document doc = null;
builder = factory.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(requestXml)));
XPathFactory xFactory = XPathFactory.newInstance();
XPath xpath = xFactory.newXPath();
String language = (String) xpath.compile("//Request/BookingPriceRequest/@Language").evaluate(doc, XPathConstants.STRING);
TheCoder
  • 8,413
  • 15
  • 42
  • 54
  • The `Request` element has a namespace. `Request` is not the same as `soa:Request`. You even set `NamespaceAware` to "true", but the ignore the namespace. – Mathias Müller Sep 18 '14 at 10:24
  • possible duplicate of [Parsing xml that has different namespaces using XPath in JAVA](http://stackoverflow.com/questions/14392200/parsing-xml-that-has-different-namespaces-using-xpath-in-java) – JLRishe Sep 18 '14 at 11:55
  • See also: http://stackoverflow.com/questions/23203421/javax-xml-xpath-is-not-extracted-from-xml-with-namespaces – JLRishe Sep 18 '14 at 11:57

2 Answers2

0

Try using the namespace for Request as well:

//soa:Request/BookingPriceRequest/@Language
Adrian B.
  • 1,592
  • 1
  • 20
  • 38
0

"The Request element has a namespace. Request is not the same as soa:Request. You even set NamespaceAware to "true", but the ignore the namespace" this worked.

Answered by Mathias Müller.

TheCoder
  • 8,413
  • 15
  • 42
  • 54