Java Tutorial/XML/XPath

Материал из Java эксперт
Перейти к: навигация, поиск

Get and set value through XPath

   <source lang="java">

import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Attr; import org.xml.sax.InputSource; public class GetNameAsAttr {

 public static void main(String[] args) throws Exception {
   XPathFactory factory = XPathFactory.newInstance();
   XPath xPath = factory.newXPath();
   Attr result = (Attr) xPath.evaluate("/schedule/@name", new InputSource(
       new FileReader("tds.xml")), XPathConstants.NODE);
   System.out.println(result.getValue());
   result.setValue("The Colbert Report");
 }

}</source>





Get integer value

   <source lang="java">

import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.xml.sax.InputSource; public class GuestListCounter {

   public static void main(String[] args) throws Exception {
       XPathFactory factory = XPathFactory.newInstance();
       XPath xPath = factory.newXPath();
       Number shows = (Number) xPath.evaluate("count(/schedule/show)",
               new InputSource(new FileReader("tds.xml")),
               XPathConstants.NUMBER);
       System.out.println("Document has " + shows.intValue() + " shows.");
   }

}</source>





Get the String data associated with the XPath selection supplied

   <source lang="java">

/*

Milyn - Copyright (C) 2006
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software 
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU Lesser General Public License for more details:    
http://www.gnu.org/licenses/lgpl.txt
*/

import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; /**

* XMl utility methods.
*
* @author Tom Fennelly
*/

public class XmlUtil {

   /**
    * Document validation types.
    */
   public static enum VALIDATION_TYPE {
       /**
        * No validation.
        */
       NONE,
       /**
        * DTD based validation.
        */
       DTD,
       /**
        * XSD based validation.
        */
       XSD,
   }
   private static String ELEMENT_NAME_FUNC = "/name()";
   private static XPathFactory xPathFactory = XPathFactory.newInstance();
   /**
    * Get the String data associated with the XPath selection supplied.
    *
    * @param node  The node to be searched.
    * @param xpath The XPath String to be used in the selection.
    * @return The string data located at the specified location in the
    *         document, or an empty string for an empty resultset query.
    */
   public static String getString(Node node, String xpath) {
       NodeList nodeList = getNodeList(node, xpath);
       if (nodeList == null || nodeList.getLength() == 0) {
           return "";
       }
       if (xpath.endsWith(ELEMENT_NAME_FUNC)) {
           if (nodeList.getLength() > 0) {
               return nodeList.item(0).getNodeName();
           } else {
               return "";
           }
       } else {
           return serialize(nodeList);
       }
   }
   /**
    * Serialise the supplied W3C DOM subtree.
    * <p/>
    * The output is unformatted.
    *
    * @param nodeList The DOM subtree as a NodeList.
    * @return The subtree in serailised form.
    * @throws DOMException Unable to serialise the DOM.
    */
   public static String serialize(NodeList nodeList) throws DOMException {
       return serialize(nodeList, false);
   }
   /**
    * Serialise the supplied W3C DOM subtree.
    *
    * @param node The DOM node to be serialized.
    * @param format Format the output.
    * @return The subtree in serailised form.
    * @throws DOMException Unable to serialise the DOM.
    */
   public static String serialize(final Node node, boolean format) throws DOMException {
       StringWriter writer = new StringWriter();
       serialize(node, format, writer);
       return writer.toString();
   }
   /**
    * Serialise the supplied W3C DOM subtree.
    *
    * @param node The DOM node to be serialized.
    * @param format Format the output.
    * @param writer The target writer for serialization.
    * @throws DOMException Unable to serialise the DOM.
    */
   public static void serialize(final Node node, boolean format, Writer writer) throws DOMException {
       if(node.getNodeType() == Node.DOCUMENT_NODE) {
           serialize(node.getChildNodes(), format, writer);
       } else {
           serialize(new NodeList() {
               public Node item(int index) {
                   return node;
               }
               public int getLength() {
                   return 1;
               }
           }, format, writer);
       }
   }
   /**
    * Serialise the supplied W3C DOM subtree.
    *
    * @param nodeList The DOM subtree as a NodeList.
    * @param format Format the output.
    * @return The subtree in serailised form.
    * @throws DOMException Unable to serialise the DOM.
    */
   public static String serialize(NodeList nodeList, boolean format) throws DOMException {
       StringWriter writer = new StringWriter();
       serialize(nodeList, format, writer);
       return writer.toString();
   }
   /**
    * Serialise the supplied W3C DOM subtree.
    *
    * @param nodeList The DOM subtree as a NodeList.
    * @param format Format the output.
    * @param writer The target writer for serialization.
    * @throws DOMException Unable to serialise the DOM.
    */
   public static void serialize(NodeList nodeList, boolean format, Writer writer) throws DOMException {
       if (nodeList == null) {
           throw new IllegalArgumentException(
                   "null "subtree" NodeIterator arg in method call.");
       }
       try {
           TransformerFactory factory = TransformerFactory.newInstance();
           Transformer transformer;
           if(format) {
               try {
                   factory.setAttribute("indent-number", new Integer(4));
               } catch(Exception e) {
                   // Ignore... Xalan may throw on this!!
                   // We handle Xalan indentation below (yeuckkk) ...
               }
           }
           transformer = factory.newTransformer();
           transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
           if(format) {
               transformer.setOutputProperty(OutputKeys.INDENT, "yes");
               transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
           }
           int listLength = nodeList.getLength();
           // Iterate through the Node List.
           for (int i = 0; i < listLength; i++) {
               Node node = nodeList.item(i);
               if (isTextNode(node)) {
                   writer.write(node.getNodeValue());
               } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
                   writer.write(((Attr) node).getValue());
               } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                   transformer.transform(new DOMSource(node), new StreamResult(writer));
               }
           }
       } catch (Exception e) {
           DOMException domExcep = new DOMException(
                   DOMException.INVALID_ACCESS_ERR,
                   "Unable to serailise DOM subtree.");
           domExcep.initCause(e);
           throw domExcep;
       }
   }
   /**
    * Is the supplied W3C DOM Node a text node.
    *
    * @param node The node to be tested.
    * @return True if the node is a text node, otherwise false.
    */
   public static boolean isTextNode(Node node) {
       short nodeType;
       if (node == null) {
           return false;
       }
       nodeType = node.getNodeType();
       return nodeType == Node.CDATA_SECTION_NODE
               || nodeType == Node.TEXT_NODE;
   }
   /**
    * Get the W3C NodeList instance associated with the XPath selection
    * supplied.
    *
    * @param node  The document node to be searched.
    * @param xpath The XPath String to be used in the selection.
    * @return The W3C NodeList instance at the specified location in the
    *         document, or null.
    */
   public static NodeList getNodeList(Node node, String xpath) {
       if (node == null) {
           throw new IllegalArgumentException(
                   "null "document" arg in method call.");
       } else if (xpath == null) {
           throw new IllegalArgumentException(
                   "null "xpath" arg in method call.");
       }
       try {
           XPath xpathEvaluater = xPathFactory.newXPath();
           if (xpath.endsWith(ELEMENT_NAME_FUNC)) {
               return (NodeList) xpathEvaluater.evaluate(xpath.substring(0,
                       xpath.length() - ELEMENT_NAME_FUNC.length()), node,
                       XPathConstants.NODESET);
           } else {
               return (NodeList) xpathEvaluater.evaluate(xpath, node,
                       XPathConstants.NODESET);
           }
       } catch (XPathExpressionException e) {
           throw new IllegalArgumentException("bad "xpath" expression ["
                   + xpath + "].");
       }
   }

}</source>





Get the W3C Node instance associated with the XPath selection supplied

   <source lang="java">

/*

Milyn - Copyright (C) 2006
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software 
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU Lesser General Public License for more details:    
http://www.gnu.org/licenses/lgpl.txt
*/

import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; /**

* XMl utility methods.
*
* @author Tom Fennelly
*/

public class XmlUtil {

   /**
    * Document validation types.
    */
   public static enum VALIDATION_TYPE {
       /**
        * No validation.
        */
       NONE,
       /**
        * DTD based validation.
        */
       DTD,
       /**
        * XSD based validation.
        */
       XSD,
   }
   private static String ELEMENT_NAME_FUNC = "/name()";
   private static XPathFactory xPathFactory = XPathFactory.newInstance();
   /**
    * Get the W3C Node instance associated with the XPath selection supplied.
    *
    * @param node  The document node to be searched.
    * @param xpath The XPath String to be used in the selection.
    * @return The W3C Node instance at the specified location in the document,
    *         or null.
    */
   public static Node getNode(Node node, String xpath) {
       NodeList nodeList = getNodeList(node, xpath);
       if (nodeList == null || nodeList.getLength() == 0) {
           return null;
       } else {
           return nodeList.item(0);
       }
   }
   /**
    * Get the W3C NodeList instance associated with the XPath selection
    * supplied.
    *
    * @param node  The document node to be searched.
    * @param xpath The XPath String to be used in the selection.
    * @return The W3C NodeList instance at the specified location in the
    *         document, or null.
    */
   public static NodeList getNodeList(Node node, String xpath) {
       if (node == null) {
           throw new IllegalArgumentException(
                   "null "document" arg in method call.");
       } else if (xpath == null) {
           throw new IllegalArgumentException(
                   "null "xpath" arg in method call.");
       }
       try {
           XPath xpathEvaluater = xPathFactory.newXPath();
           if (xpath.endsWith(ELEMENT_NAME_FUNC)) {
               return (NodeList) xpathEvaluater.evaluate(xpath.substring(0,
                       xpath.length() - ELEMENT_NAME_FUNC.length()), node,
                       XPathConstants.NODESET);
           } else {
               return (NodeList) xpathEvaluater.evaluate(xpath, node,
                       XPathConstants.NODESET);
           }
       } catch (XPathExpressionException e) {
           throw new IllegalArgumentException("bad "xpath" expression ["
                   + xpath + "].");
       }
   }

}</source>





Get the W3C NodeList instance associated with the XPath selection supplied

   <source lang="java">

/*

Milyn - Copyright (C) 2006
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software 
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU Lesser General Public License for more details:    
http://www.gnu.org/licenses/lgpl.txt
*/

import org.w3c.dom.Attr; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.EntityResolver; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;

import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; /**

* XMl utility methods.
*
* @author Tom Fennelly
*/

public class XmlUtil {

   /**
    * Document validation types.
    */
   public static enum VALIDATION_TYPE {
       /**
        * No validation.
        */
       NONE,
       /**
        * DTD based validation.
        */
       DTD,
       /**
        * XSD based validation.
        */
       XSD,
   }
   private static String ELEMENT_NAME_FUNC = "/name()";
   private static XPathFactory xPathFactory = XPathFactory.newInstance();
   /**
    * Get the W3C NodeList instance associated with the XPath selection
    * supplied.
    *
    * @param node  The document node to be searched.
    * @param xpath The XPath String to be used in the selection.
    * @return The W3C NodeList instance at the specified location in the
    *         document, or null.
    */
   public static NodeList getNodeList(Node node, String xpath) {
       if (node == null) {
           throw new IllegalArgumentException(
                   "null "document" arg in method call.");
       } else if (xpath == null) {
           throw new IllegalArgumentException(
                   "null "xpath" arg in method call.");
       }
       try {
           XPath xpathEvaluater = xPathFactory.newXPath();
           if (xpath.endsWith(ELEMENT_NAME_FUNC)) {
               return (NodeList) xpathEvaluater.evaluate(xpath.substring(0,
                       xpath.length() - ELEMENT_NAME_FUNC.length()), node,
                       XPathConstants.NODESET);
           } else {
               return (NodeList) xpathEvaluater.evaluate(xpath, node,
                       XPathConstants.NODESET);
           }
       } catch (XPathExpressionException e) {
           throw new IllegalArgumentException("bad "xpath" expression ["
                   + xpath + "].");
       }
   }

}</source>





Parse with XPath

   <source lang="java">

import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class Main {

 public static void main(String[] args) throws Exception {
   XPath xpath = XPathFactory.newInstance().newXPath();
   String xpathExpression = "/howto/topic/@name";
   InputSource inputSource = new InputSource("howto.xml");
   NodeList nodes = (NodeList) xpath
       .evaluate(xpathExpression, inputSource, XPathConstants.NODESET);
   int j = nodes.getLength();
   for (int i = 0; i < j; i++) {
     System.out.println(nodes.item(i).getTextContent());
   }
 }

}</source>





Return a NodeList

   <source lang="java">

import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class GuestList {

 public static void main(String[] args) throws Exception {
   XPathFactory factory = XPathFactory.newInstance();
   XPath xPath = factory.newXPath();
   NodeList shows = (NodeList) xPath.evaluate("/schedule/show", new InputSource(new FileReader(
       "tds.xml")), XPathConstants.NODESET);
   for (int i = 0; i < shows.getLength(); i++) {
     Element show = (Element) shows.item(i);
     String guestName = xPath.evaluate("guest/name", show);
     String guestCredit = xPath.evaluate("guest/credit", show);
     System.out.println(show.getAttribute("weekday") + ", " + show.getAttribute("date") + " - "
         + guestName + " (" + guestCredit + ")");
   }
 }

}</source>





Search an XPath

   <source lang="java">

import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathFactory; import org.xml.sax.InputSource; public class GetName {

   public static void main(String[] args) throws Exception {
       XPathFactory factory = XPathFactory.newInstance();
       XPath xPath = factory.newXPath();
       String result = xPath.evaluate("/schedule/@name", new InputSource(
               new FileReader("tds.xml")));
       System.out.println(result);
   }

}</source>





XPath.evaluate("/schedule/@seriesId")

   <source lang="java">

import java.io.FileReader; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathFactory; import org.xml.sax.InputSource; public class GetSeriesId {

 public static void main(String[] args) throws Exception {
   XPathFactory factory = XPathFactory.newInstance();
   XPath xPath = factory.newXPath();
   Double result = (Double) xPath.evaluate("/schedule/@seriesId", new InputSource(new FileReader(
       "tds.xml")), XPathConstants.NUMBER);
   System.out.println(result.intValue());
 }

}</source>