Java: XPath: Read series of attributes from xml files

The following piece of code is an example of extraction of specific attribute from an xml file.

The xml looks like this:

<?xml version="1.0" ?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xft-xliff="http://www.xfa.org/schema/xfa-xliff/1.0/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="1.1"
xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml">
<body>
<unit id="2401" approved="no">
<note>First unit</note>
</unit>
<unit id="2402" approved="no">
<note>Second unit</note>
</unit>

and we want to extract exactly the attribute “id” from tag “unit”

import java.io.StringReader;   
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;  
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ReadXML_SingleParameter {
  
    //Set the directory:
    final static File file = new File("C:/myXML");
    //Set the single xml repository
    /*
     * XPath example:
     * you can use //add[@job='351']/tag[position()=1]/text()
     * if you have an xml like <add job=\"351\"><tag>foobar</tag><tag>foobar2</tag></add>
     * in order to extract the value of the firt <tag>
     */
  
    /**
     * @param args
     */
    public static void main(String[] args) {
        ReadXML_SingleParameter readXML = new ReadXML_SingleParameter();
        Document document = null;
        try {
            document = readXML.parseXML(file);
        } catch (Exception e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        NodeList nl;      
        nl = document.getElementsByTagName("trans-unit");
        System.out.println("this is the NodeList length: " + nl.getLength());
  
        //Initialize XPathFactory:
        XPathFactory xpf = XPathFactory.newInstance();
        XPath xp = xpf.newXPath();      
        String text="";
      
        for(int i=1;i<= nl.getLength();i++){
            try {
                text = xp.evaluate("/xliff/file/body/unit["+ i +"]/@id", document.getDocumentElement());
               

            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
            System.out.println(text);
        }
       
    }  

    public Document parseXML(File file) throws Exception {
        Document doc;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(file);
        return doc;
    }
}

Leave a Reply