language: Java (sun-jdk-1.7.0_10)
date: 187 days 21 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.StringReader;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
 
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
 
public class Main {
    public static void main(String[] args) throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n" + //
                "<REPOSITORY xmlns:LIBRARY = \"http://www.openarchives.org/LIBRARY/2.0/\" xmlns:xsi = \"http://www.w3.prg/2001/XMLSchema-instance\"  xsi:schemaLocation = \"http://www.openarchives.org/LIBRARY/2.0/ http://www.openarchives.org/LIBRARY/2.0/LIBRARY-PHM.xsd\">\r\n" + // 
                "<repository>Test</repository>\r\n" + //
                "<records><record>\r\n" + //
                "<ejemplar>\r\n" + //
                "<library_book:book \r\n" + // 
                "    xmlns:library_book=\"http://www.w3c.es/LIBRARY/book/\"\r\n" + // 
                "    xmlns:book=\"http://www.w3c.es/LIBRARY/book/\"\r\n" + //
                "    xmlns:bookAssets=\"http://www.w3c.es/LIBRARY/book/\"\r\n" + // 
                "    xmlns:bookAsset=\"http://www.w3c.es/LIBRARY/book/\"\r\n" + //
                "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n" + //
                "    xsi:schemaLocation=\"http://www.w3c.es/LIBRARY/book/ http://www.w3c.es/LIBRARY/replacement/book.xsd\">\r\n" + // 
                "<book:bookAssets count=\"1\">\r\n" + //
                "<book:bookAsset nasset=\"1\">\r\n" + //
                "<book:bookAsset.id>value1</book:bookAsset.id>\r\n" + // 
                "<book:bookAsset.event>\r\n" + //
                "<book:bookAsset.event.id>value2</book:bookAsset.event.id>\r\n" + // 
                "</book:bookAsset.event>\r\n" + //
                "</book:bookAsset>\r\n" + //
                "</book:bookAssets>\r\n" + //
                "</library_book:book>\r\n" + //
                "</ejemplar>\r\n" + //
                "</record></records>\r\n" + // 
                "</REPOSITORY>";
        // Standard of reading a XML file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        XPathExpression expr = null;
        builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
        // Create a XPathFactory
        XPathFactory xFactory = XPathFactory.newInstance();
        // Create a XPath object
        XPath xpath = xFactory.newXPath();
        expr = xpath.compile("//*[local-name()='bookAsset.event.id']/text()");
        Object result = expr.evaluate(doc, XPathConstants.STRING);
        System.out.println("RESULT=" + result);
    }
}