Xpath - 无法找到属性

时间:2014-09-18 08:18:14

标签: java xpath

我正在尝试使用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);

2 个答案:

答案 0 :(得分:0)

尝试使用Request的命名空间:

//soa:Request/BookingPriceRequest/@Language

答案 1 :(得分:0)

“Request元素有一个命名空间。请求与soa:Request不同。你甚至将NamespaceAware设置为”true“,但忽略命名空间”这有用。

MathiasMüller回答。