【问题标题】:Xpath - unable to find attributeXpath - 无法找到属性
【发布时间】: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);

【问题讨论】:

标签: java xpath


【解决方案1】:

也尝试使用 Request 的命名空间:

//soa:Request/BookingPriceRequest/@Language

【讨论】:

    【解决方案2】:

    “Request 元素有一个命名空间。Request 与 soa:Request 不同。您甚至将 NamespaceAware 设置为“true”,但忽略命名空间”这行得通。

    由 Mathias Müller 回答。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多