Package com.iizix

Class XMLUtilities


  • public class XMLUtilities
    extends java.lang.Object
    Helper class containing utility methods used when processing XML documents.
    Author:
    Christopher Mindus
    • Field Summary

      Fields 
      Modifier and TypeFieldDescription
      static java.lang.StringXML_FILE_ENCODING
      The encoding (UTF-8 or ISO-8859-1).
    • Constructor Summary

      Constructors 
      ConstructorDescription
      XMLUtilities() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and TypeMethodDescription
      static org.w3c.dom.DocumentcreateDocument()
      Creates a new empty Document from the document builder factory.
      static javax.xml.transform.TransformercreateLoader​(java.net.URL schemaURL)
      Creates a transformer from an XSL schema used to load multiple XML files to be validated.
      static java.lang.StringgetAttributeString​(org.w3c.dom.NamedNodeMap namedNodeMap, java.lang.String name)
      Gets an attribute from a node attribute list.
      static java.lang.StringgetAttributeString​(org.w3c.dom.NamedNodeMap namedNodeMap, java.lang.String name, java.lang.String defaultValue)
      Gets an attribute from a node attribute list.
      static java.lang.StringgetAttributeString​(org.w3c.dom.Node node, java.lang.String name)
      Gets an attribute from a node.
      static java.lang.StringgetAttributeString​(org.w3c.dom.Node node, java.lang.String name, java.lang.String defaultValue)
      Gets an attribute from a node.
      static org.w3c.dom.ElementgetFirstElement​(org.w3c.dom.Element parent, java.lang.String name)
      Gets the first Element with the specified name.
      static java.lang.StringgetNodeString​(org.w3c.dom.Node node)
      Gets a Node as a String.
      static java.lang.StringgetXMLDocument​(org.w3c.dom.Document document)
      Gets a Document as a String.
      static voidlist​(org.w3c.dom.Document doc)
      Lists a Document.
      static voidlist​(org.w3c.dom.Node node, int indent)
      Lists a Node and it's tree.
      static org.w3c.dom.DocumentloadXMLDocument​(java.io.File file)
      Loads an XML document from a file into a Document without schema validation.
      static org.w3c.dom.DocumentloadXMLDocument​(java.io.InputStream in)
      Loads an XML document from a file into a Document without schema validation.
      static org.w3c.dom.DocumentloadXMLDocument​(java.lang.String data)
      Loads an XML document from a String into a Document.
      static org.w3c.dom.DocumentloadXMLDocument​(java.net.URL url)
      Loads an XML document from a file into a Document without schema validation.
      static javax.xml.parsers.DocumentBuildernewDocumentBuilderWithoutSchema()
      Create a new XML parser without schema, namespace aware for W3C XML Schema 1.0.
      static voidsaveXMLDocument​(org.w3c.dom.Document document, java.io.File file)
      Saves a Document to a File.
      static voidsaveXMLDocument​(org.w3c.dom.Document document, java.io.OutputStream output)
      Saves a Document to an output stream.
      static voidsaveXMLDocument​(org.w3c.dom.Document document, java.io.Writer writer)
      Saves a Document to a writer.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • XML_FILE_ENCODING

        public static final java.lang.String XML_FILE_ENCODING
        The encoding (UTF-8 or ISO-8859-1).
        See Also:
        Constant Field Values
    • Constructor Detail

      • XMLUtilities

        public XMLUtilities()
    • Method Detail

      • newDocumentBuilderWithoutSchema

        public static javax.xml.parsers.DocumentBuilder newDocumentBuilderWithoutSchema()
        Create a new XML parser without schema, namespace aware for W3C XML Schema 1.0. The error handler does not accept warnings or errors and throws the exception.
        Returns:
        A new Document Builder that can parse XML.
      • createDocument

        public static org.w3c.dom.Document createDocument()
        Creates a new empty Document from the document builder factory.
        Returns:
        The new Document.
      • createLoader

        public static javax.xml.transform.Transformer createLoader​(java.net.URL schemaURL)
                                                            throws java.io.IOException
        Creates a transformer from an XSL schema used to load multiple XML files to be validated.
        Parameters:
        schemaURL - The schema URL for the XSL schema.
        Returns:
        The XML transformer to use.
        Throws:
        java.io.IOException - For failures in creating the transformer for the URL.
      • loadXMLDocument

        public static org.w3c.dom.Document loadXMLDocument​(java.lang.String data)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Loads an XML document from a String into a Document.
        Parameters:
        data - The String.
        Returns:
        the loaded document.
        Throws:
        java.io.IOException - for file errors.
        org.xml.sax.SAXException - a SAX exception when loading/parsing the XML file.
      • loadXMLDocument

        public static org.w3c.dom.Document loadXMLDocument​(java.io.File file)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Loads an XML document from a file into a Document without schema validation.
        Parameters:
        file - The file to load.
        Returns:
        the loaded document.
        Throws:
        java.io.IOException - for file errors.
        org.xml.sax.SAXException - a SAX exception when loading/parsing the XML file.
      • loadXMLDocument

        public static org.w3c.dom.Document loadXMLDocument​(java.net.URL url)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Loads an XML document from a file into a Document without schema validation.
        Parameters:
        url - The file to load.
        Returns:
        the loaded document.
        Throws:
        java.io.IOException - for file errors.
        org.xml.sax.SAXException - a SAX exception when loading/parsing the XML file.
      • loadXMLDocument

        public static org.w3c.dom.Document loadXMLDocument​(java.io.InputStream in)
                                                    throws java.io.IOException,
                                                           org.xml.sax.SAXException
        Loads an XML document from a file into a Document without schema validation.
        Parameters:
        in - The input stream.
        Returns:
        the loaded document.
        Throws:
        java.io.IOException - for file errors.
        org.xml.sax.SAXException - a SAX exception when loading/parsing the XML file.
      • getXMLDocument

        public static java.lang.String getXMLDocument​(org.w3c.dom.Document document)
                                               throws javax.xml.transform.TransformerException
        Gets a Document as a String.
        Parameters:
        document - The document to save.
        Returns:
        The XML representation of the document.
        Throws:
        javax.xml.transform.TransformerException - For document errors.
        javax.xml.transform.TransformerFactoryConfigurationError - For XML factory errors.
      • getNodeString

        public static java.lang.String getNodeString​(org.w3c.dom.Node node)
                                              throws javax.xml.transform.TransformerException
        Gets a Node as a String.
        Parameters:
        node - The document to save.
        Returns:
        The XML representation of the node.
        Throws:
        javax.xml.transform.TransformerException - For document errors.
        javax.xml.transform.TransformerFactoryConfigurationError - For XML factory errors.
      • saveXMLDocument

        public static void saveXMLDocument​(org.w3c.dom.Document document,
                                           java.io.File file)
                                    throws java.io.IOException,
                                           javax.xml.transform.TransformerException
        Saves a Document to a File.
        Parameters:
        document - The document to save.
        file - The file to use for document saving.
        Throws:
        java.io.IOException - For file errors.
        javax.xml.transform.TransformerException - For document errors.
      • saveXMLDocument

        public static void saveXMLDocument​(org.w3c.dom.Document document,
                                           java.io.OutputStream output)
                                    throws java.io.IOException,
                                           javax.xml.transform.TransformerException
        Saves a Document to an output stream.
        Parameters:
        document - The document to save.
        output - The output stream.
        Throws:
        java.io.IOException - For file errors.
        javax.xml.transform.TransformerException - For document errors.
      • saveXMLDocument

        public static void saveXMLDocument​(org.w3c.dom.Document document,
                                           java.io.Writer writer)
                                    throws java.io.IOException,
                                           javax.xml.transform.TransformerException
        Saves a Document to a writer.
        Parameters:
        document - The document to save.
        writer - The writer.
        Throws:
        java.io.IOException - For file errors.
        javax.xml.transform.TransformerException - For document errors.
      • list

        public static void list​(org.w3c.dom.Document doc)
        Lists a Document.
        Parameters:
        doc - The document to list.
      • list

        public static void list​(org.w3c.dom.Node node,
                                int indent)
        Lists a Node and it's tree.
        Parameters:
        node - The node to list.
        indent - The indent level.
      • getAttributeString

        public static java.lang.String getAttributeString​(org.w3c.dom.Node node,
                                                          java.lang.String name)
        Gets an attribute from a node.
        Parameters:
        node - the node.
        name - name of attribute.
        Returns:
        the string value of the attribute or null if it's not found.
      • getAttributeString

        public static java.lang.String getAttributeString​(org.w3c.dom.NamedNodeMap namedNodeMap,
                                                          java.lang.String name)
        Gets an attribute from a node attribute list.
        Parameters:
        namedNodeMap - the node's attributes.
        name - name of attribute.
        Returns:
        the string value of the attribute or null if it's not found.
      • getAttributeString

        public static java.lang.String getAttributeString​(org.w3c.dom.Node node,
                                                          java.lang.String name,
                                                          java.lang.String defaultValue)
        Gets an attribute from a node.
        Parameters:
        node - the node.
        name - name of attribute.
        defaultValue - the default value to use if attribute is not found.
        Returns:
        the string value of the attribute or defaultValue if it's not found.
      • getAttributeString

        public static java.lang.String getAttributeString​(org.w3c.dom.NamedNodeMap namedNodeMap,
                                                          java.lang.String name,
                                                          java.lang.String defaultValue)
        Gets an attribute from a node attribute list.
        Parameters:
        namedNodeMap - the node's attributes.
        name - name of attribute.
        defaultValue - the default value to use if attribute is not found.
        Returns:
        the string value of the attribute or defaultValue if it's not found.
      • getFirstElement

        public static org.w3c.dom.Element getFirstElement​(org.w3c.dom.Element parent,
                                                          java.lang.String name)
        Gets the first Element with the specified name.
        Parameters:
        parent - The parent element.
        name - The name.
        Returns:
        null if not found.