繁体   English   中英

Xpath-无法找到属性

[英]Xpath - unable to find attribute

我正在尝试使用Xpath访问以下xml中的Language属性。 我得到一个空字符串。 谁能告诉我为什么?

<?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

“ Request元素具有一个名称空间。Request与soa:Request不同。您甚至将NamespaceAware设置为“ true”,但是忽略名称空间“是可行的。

MathiasMüller的回答。

暂无
暂无

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

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