Search code examples
javaxmlxml-parsingfor-xml-path

xml parse - xpath clarification in Java


How should i get the Link value from the below xml

XML Content

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

i traverse update desc attribute after that i'm struggle

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

Expected output : retrieve the Link value

www.google.com

Solution

  •     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();