简体   繁体   中英

Xpath - unable to find attribute

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);

也尝试对请求使用命名空间:

//soa:Request/BookingPriceRequest/@Language

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM