XML : Elliotte Rusty Harold, Adjunct Professor, Polytechnic University 2006 9 04 2006 10 17 문서옵션 제안및의견 XPath Document Object Model (DOM). XML XPath. Java 5 XPath XML - javax.xml.xpath.,? "?"? ".... 4. 5..."? ".".., "Find a copy of Cryptonomicon". "Find all the books by Neal Stephenson". XPath, Structured Query Language (SQL). Object Query Language (OQL) XQuery. XML XPath., Neal Stephenson XPath. //book[author="neal Stephenson"]/title DOM Listing 1 : Listing 1. Neal Stephenson DOM http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (1 / 17) [2008-03-14 8:47:46]
ArrayList result = new ArrayList(); NodeList books = doc.getelementsbytagname("book"); for (int i = 0; i < books.getlength(); i++) { Element book = (Element) books.item(i); NodeList authors = book.getelementsbytagname("author"); boolean stephenson = false; for (int j = 0; j < authors.getlength(); j++) { Element author = (Element) authors.item(j); NodeList children = author.getchildnodes(); StringBuffer sb = new StringBuffer(); for (int k = 0; k < children.getlength(); k++) { Node child = children.item(k); // really should to do this recursively if (child.getnodetype() == Node.TEXT_NODE) { sb.append(child.getnodevalue()); if (sb.tostring().equals("neal Stephenson")) { stephenson = true; break; http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (2 / 17) [2008-03-14 8:47:46]
if (stephenson) { NodeList titles = book.getelementsbytagname("title"); for (int j = 0; j < titles.getlength(); j++) { result.add(titles.item(j)); Listing 1 DOM XPath.,,?. XPath. XPath. XPath.., XPath International Standard Book Number (ISBN)., XPath. XPath., XPath (API) XPath. Xalan API, Saxon API, API.. Java 5 javax.xml.xpath XPath. XML Processing (JAXP) 1.3 API Java 1.3., Xalan 2.7 Saxon 8.. Neal Stephenson., Listing 2 : http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (3 / 17) [2008-03-14 8:47:46]
Listing 2. XML <inventory> <book year="2000"> <title>snow Crash</title> <author>neal Stephenson</author> <publisher>spectra</publisher> <isbn>0553380958</isbn> <price>14.95</price> </book> <book year="2005"> <title>burning Tower</title> <author>larry Niven</author> <author>jerry Pournelle</author> <publisher>pocket</publisher> <isbn>0743416910</isbn> <price>5.99</price> <book> <book year="1995"> <title>zodiac</title> <author>neal Stephenson<author> http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (4 / 17) [2008-03-14 8:47:46]
<publisher>spectra</publisher> <isbn>0553573862</isbn> <price>7.50</price> <book> <!-- more books... --> </inventory> XPath : //book[author="neal Stephenson"].,. //book[author="neal Stephenson"]/title. title.. : //book [author="neal Stephenson"]/title/ text(). XPathFactory. API DOM, JDOM, XOM. Uniform Resource Identifier (URI) XPathFactory. newinstance()., http://xom.nu/ XOM., API DOM.,,., DOM Document. books.xml. Document : Listing 3. JAXP DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setnamespaceaware(true); // never forget this! DocumentBuilder builder = factory.newdocumentbuilder(); Document doc = builder.parse("books.xml"); http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (5 / 17) [2008-03-14 8:47:46]
, JAXP DOM.. XPathFactory : XPathFactory factory = XPathFactory.newInstance(); XPath : XPath xpath = factory.newxpath(); XPath XPath : PathExpression expr = xpath.compile("//book[author='neal Stephenson']/title/text()"); XPath Immediate evaluation. XPath. XPath. evaluate().. node-set :. Object result = expr.evaluate(doc, XPathConstants.NODESET); DOM NodeList : NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getlength(); i++) { System.out.println(nodes.item(i).getNodeValue()); Listing 4. throws. Listing 4. XPath XML http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (6 / 17) [2008-03-14 8:47:46]
import java.io.ioexception; import org.w3c.dom.*; import org.xml.sax.saxexception; import javax.xml.parsers.*; import javax.xml.xpath.*; public class XPathExample { public static void main(string[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException { DocumentBuilderFactory domfactory = DocumentBuilderFactory.newInstance(); domfactory.setnamespaceaware(true); // never forget this! DocumentBuilder builder = domfactory.newdocumentbuilder(); Document doc = builder.parse("books.xml"); XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newxpath(); XPathExpression expr = xpath.compile("//book[author='neal Stephenson']/title/text()"); http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (7 / 17) [2008-03-14 8:47:46]
Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getlength(); i++) { System.out.println(nodes.item(i).getNodeValue()); XPath XPath,.. XPath. XPath 1.0 : node-set number boolean string,. XPath, node-set.., XPath count (//book). XPath count(//book[@author="neal Stephenson"]) > 10 : Neal Stephenson 10 true 10 false. evaluate() Object. XPath,. XPath, java.lang.double. java.lang.string. java.lang.boolean. node-set org.w3c.dom.nodelist. XPath. http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (8 / 17) [2008-03-14 8:47:46]
, javax.xml.xpath.xpathconstants XPath 2 :, XPath 1.0 XPathConstants.NODESET. XPath 2 XPathConstants.BOOLEAN XPathConstants.NUMBER. XPath 2 XPathConstants.STRING XPath API XPathConstants.NODE XPathConstants.NODE XPath XPath 2. XPath.. XPath XPathConstants.NODE evaluate(). XPath XPathConstants.NODE evaluate() null. evaluate() XPathException. XML XPath. XPath URI., XML XPath.,., XML. URI. javax.xml.namespace.namespacecontext. http://www.example.com/books Listing 5 : Listing 5. XML http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (9 / 17) [2008-03-14 8:47:46]
<inventory xmlns="http://www.example.com/books"> <book year="2000"> <title>snow Crash</title> <author>neal Stephenson</author> <publisher>spectra</publisher> <isbn>0553380958</isbn> <price>14.95<price> </book> <!-- more books... --> <inventory> Neal Stephenson XPath //pre:book[pre:author="neal Stephenson"]/pre: title/text(). pre http://www.example.com/books URI. NamespaceContext (JDK) JAXP.. Listing 6. xml. Listing 6. http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (10 / 17) [2008-03-14 8:47:46]
import java.util.iterator; import javax.xml.*; import javax.xml.namespace.namespacecontext; public class PersonalNamespaceContext implements NamespaceContext { public String getnamespaceuri(string prefix) { if (prefix == null) throw new NullPointerException("Null prefix"); else if ("pre".equals(prefix)) return "http://www.example.org/books"; else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI; return XMLConstants.NULL_NS_URI; // This method isn't necessary for XPath processing. public String getprefix(string uri) { throw new UnsupportedOperationException(); // This method isn't necessary for XPath processing either. public Iterator getprefixes(string uri) { throw new UnsupportedOperationException(); http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (11 / 17) [2008-03-14 8:47:46]
. NamespaceContext, XPath.. Listing 7. XPath XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newxpath(); xpath.setnamespacecontext(new PersonalNamespaceContext()); XPathExpression expr = xpath.compile("//pre:book[pre:author='neal Stephenson']/pre:title/text()"); Object result = expr.evaluate(doc, XPathConstants.NODESET); NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getlength(); i++) { System.out.println(nodes.item(i).getNodeValue());, XPath. XPath..,. (XPath.) XPath API javax.xml.xpath.xpathfunction., evaluate : http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (12 / 17) [2008-03-14 8:47:46]
public Object evaluate(list args) throws XPathFunctionException XPath. String Double Boolean Nodelist Node, Listing 8 ISBN Boolean. 9. ( 1 2.) 11. 10 X. Listing 8. ISBN XPath import java.util.list; import javax.xml.xpath.*; import org.w3c.dom.*; public class ISBNValidator implements XPathFunction { // This class could easily be implemented as a Singleton. public Object evaluate(list args) throws XPathFunctionException { if (args.size()!= 1) { throw new XPathFunctionException("Wrong number of arguments to valid-isbn()"); http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (13 / 17) [2008-03-14 8:47:46]
String isbn; Object o = args.get(0); // perform conversions if (o instanceof String) isbn = (String) args.get(0); else if (o instanceof Boolean) isbn = o.tostring(); else if (o instanceof Double) isbn = o.tostring(); else if (o instanceof NodeList) { NodeList list = (NodeList) o; Node node = list.item(0); // gettextcontent is available in Java 5 and DOM 3. // In Java 1.4 and DOM 2, you'd need to recursively // accumulate the content. isbn= node.gettextcontent(); else { throw new XPathFunctionException("Could not convert argument type"); char[] data = isbn.tochararray(); if (data.length!= 10) return Boolean.FALSE; int checksum = 0; for (int i = 0; i < 9; i++) { http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (14 / 17) [2008-03-14 8:47:46]
checksum += (i+1) * (data[i]-'0'); int checkdigit = checksum % 11; if (checkdigit + '0' == data[9] (data[9] == 'X' && checkdigit == 10)) { return Boolean.TRUE; return Boolean.FALSE;. XPath javax.xml.xpath.xpathfunctionresolver. Resolver XPath URI. Listing 9 http://www.example.org/books valid-isbn Listing 8., XPath //book[not(pre: valid-isbn(isbn))] ISBM. Listing 9. isbn iimport javax.xml.namespace.qname; import javax.xml.xpath.*; public class ISBNFunctionContext implements XPathFunctionResolver { private static final QName name http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (15 / 17) [2008-03-14 8:47:46]
= new QName("http://www.example.org/books", "valid-isbn"); public XPathFunction resolvefunction(qname name, int arity) { if (name.equals(isbnfunctioncontext.name) && arity == 1) { return new ISBNValidator(); return null; NamespaceResolver.. XPathFunctionResolver, XPathFunction, NamespaceResolver. SQL XPath C. C SQL XPath. XPathFunctionResolver, XPathFunction, NamespaceResolver API. XML javax.xml.xpath java.sql. The Java XPath API "XPath 2.0 " by Benoît Marchal (developerworks korea, 2006 5 ) http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (16 / 17) [2008-03-14 8:47:46]
Working with JAXP namespace contexts XML in a Nutshell (Elliotte Rusty Harold and W. Scott Means, O'Reilly, 2005) IBM XML 1.1 certification XML developerworks technical events and webcasts JAXP Project Xalan 2 SAXON 8 IBM trial software XML developerworks blogs Elliotte Rusty Harold New Orleans. Beth Charm Marjorie Brooklyn Prospect Heights. Polytechnic University. Cafe au Lait, Cafe con Leche XML. Effective XML, Processing XML with Java, Java Network Programming, Java I/O, 2nd edition. XML XOM API, Jaxen XPath Jester. 9 Java at Software Development Best Practices. http://www.ibm.com/developerworks/kr/library/x-javaxpathapi.html (17 / 17) [2008-03-14 8:47:46]