xml 解析 - Java 中的 xpath 说明

xml parse - xpath clarification in Java

我应该如何从下面的 xml

中获取 Link

XML 内容

<document-instance system="abc.org" number-of-pages="6" desc="Drawing" link="www.google.com">
        <document-format-options>
          <document-format>application/pdf</document-format>
          <document-format>application/tiff</document-format>
        </document-format-options>
        <document-section name="DRAWINGS" start-page="1" />
      </document-instance>

我遍历更新desc属性之后我很挣扎

 XPathExpression firstPageUrl = xPath.compile("//document-instance/@desc=\"Drawing\"]");

预期输出:检索 Link 值

www.google.com
    File file = new File("path to file");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(file);
    XPath xPath = XPathFactory.newInstance().newXPath();

    String expression = "//document-instance/@link";
    Node node = (Node) xPath.compile(expression).evaluate(doc, XPathConstants.NODE);
    String url= node.getTextContent();