【发布时间】:2014-09-18 08:18:14
【问题描述】:
我正在尝试使用 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);
【问题讨论】:
-
Request元素有一个命名空间。Request与soa:Request不同。您甚至将NamespaceAware设置为“true”,但忽略了命名空间。