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 {
String xml
= "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n" + // "<REPOSITORY xmlns:LIBRARY = \"http://w...content-available-to-author-only...s.org/LIBRARY/2.0/\" xmlns:xsi = \"http://w...content-available-to-author-only...3.prg/2001/XMLSchema-instance\" xsi:schemaLocation = \"http://w...content-available-to-author-only...s.org/LIBRARY/2.0/ http://w...content-available-to-author-only...s.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://w...content-available-to-author-only...c.es/LIBRARY/book/\"\r\n" + //
" xmlns:book=\"http://w...content-available-to-author-only...c.es/LIBRARY/book/\"\r\n" + //
" xmlns:bookAssets=\"http://w...content-available-to-author-only...c.es/LIBRARY/book/\"\r\n" + //
" xmlns:bookAsset=\"http://w...content-available-to-author-only...c.es/LIBRARY/book/\"\r\n" + //
" xmlns:xsi=\"http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance\"\r\n" + //
" xsi:schemaLocation=\"http://w...content-available-to-author-only...c.es/LIBRARY/book/ http://w...content-available-to-author-only...c.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;
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
); }
}