简体   繁体   English

Xpath-无法找到属性

[英]Xpath - unable to find attribute

I am trying to access the Language attribute in the following xml using Xpath. 我正在尝试使用Xpath访问以下xml中的Language属性。 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. “ Request元素具有一个名称空间。Request与soa:Request不同。您甚至将NamespaceAware设置为“ true”,但是忽略名称空间“是可行的。

Answered by Mathias Müller. MathiasMüller的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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