Class XMLUtil


  • public class XMLUtil
    extends java.lang.Object
    This class with handle some simple XML operations.
    Version:
    $Revision: 1.4 $
    Author:
    Ben Litchfield, Christopher Oezbek
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] asByteArray​(org.w3c.dom.Document doc, java.lang.String encoding)
      Convert the document to an array of bytes.
      static org.w3c.dom.Element getElement​(org.w3c.dom.Element parent, java.lang.String elementName)
      Get the first instance of an element by name.
      static java.lang.Integer getIntValue​(org.w3c.dom.Element parent, java.lang.String nodeName)
      Get the integer value of a subnode.
      static java.lang.String getStringValue​(org.w3c.dom.Element node)
      This will get the text value of an element.
      static java.lang.String getStringValue​(org.w3c.dom.Element parent, java.lang.String nodeName)
      Get the value of a subnode.
      static org.w3c.dom.Document newDocument()
      Create a new blank XML document.
      static org.w3c.dom.Document parse​(java.io.InputStream is)
      This will parse an XML stream and create a DOM document.
      static org.w3c.dom.Document parse​(java.lang.String fileName)
      This will parse an XML stream and create a DOM document.
      static org.w3c.dom.Document parse​(org.xml.sax.InputSource is)
      This will parse an InputSource and create a DOM document.
      static void save​(org.w3c.dom.Document doc, java.lang.String file, java.lang.String encoding)
      Save the XML document to a file.
      static void save​(org.w3c.dom.Node doc, java.io.OutputStream outStream, java.lang.String encoding)
      Save the XML document to an output stream.
      static void setElementableValue​(org.w3c.dom.Element parent, java.lang.String name, Elementable node)
      Set an XML element document.
      static void setIntValue​(org.w3c.dom.Element parent, java.lang.String nodeName, java.lang.Integer intValue)
      Set the integer value of an element.
      static void setStringValue​(org.w3c.dom.Element node, java.lang.String value)
      This will set the text value of an element.
      static void setStringValue​(org.w3c.dom.Element parent, java.lang.String nodeName, java.lang.String nodeValue)
      Set the value of an element.
      • Methods inherited from class java.lang.Object

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

      • parse

        public static org.w3c.dom.Document parse​(java.io.InputStream is)
                                          throws java.io.IOException
        This will parse an XML stream and create a DOM document.
        Parameters:
        is - The stream to get the XML from.
        Returns:
        The DOM document.
        Throws:
        java.io.IOException - It there is an error creating the dom.
      • parse

        public static org.w3c.dom.Document parse​(org.xml.sax.InputSource is)
                                          throws java.io.IOException
        This will parse an InputSource and create a DOM document.
        Parameters:
        is - The stream to get the XML from.
        Returns:
        The DOM document.
        Throws:
        java.io.IOException - It there is an error creating the dom.
      • parse

        public static org.w3c.dom.Document parse​(java.lang.String fileName)
                                          throws java.io.IOException
        This will parse an XML stream and create a DOM document.
        Parameters:
        fileName - The file to get the XML from.
        Returns:
        The DOM document.
        Throws:
        java.io.IOException - It there is an error creating the dom.
      • newDocument

        public static org.w3c.dom.Document newDocument()
                                                throws java.io.IOException
        Create a new blank XML document.
        Returns:
        The new blank XML document.
        Throws:
        java.io.IOException - If there is an error creating the XML document.
      • getElement

        public static org.w3c.dom.Element getElement​(org.w3c.dom.Element parent,
                                                     java.lang.String elementName)
        Get the first instance of an element by name.
        Parameters:
        parent - The parent to get the element from.
        elementName - The name of the element to look for.
        Returns:
        The element or null if it is not found.
      • getIntValue

        public static java.lang.Integer getIntValue​(org.w3c.dom.Element parent,
                                                    java.lang.String nodeName)
        Get the integer value of a subnode.
        Parameters:
        parent - The parent element that holds the values.
        nodeName - The name of the node that holds the integer value.
        Returns:
        The integer value of the node.
      • setIntValue

        public static void setIntValue​(org.w3c.dom.Element parent,
                                       java.lang.String nodeName,
                                       java.lang.Integer intValue)
        Set the integer value of an element.
        Parameters:
        parent - The parent element that will hold this subelement.
        nodeName - The name of the subelement.
        intValue - The value to set.
      • getStringValue

        public static java.lang.String getStringValue​(org.w3c.dom.Element parent,
                                                      java.lang.String nodeName)
        Get the value of a subnode.
        Parameters:
        parent - The parent element that holds the values.
        nodeName - The name of the node that holds the value.
        Returns:
        The value of the sub node.
      • setStringValue

        public static void setStringValue​(org.w3c.dom.Element parent,
                                          java.lang.String nodeName,
                                          java.lang.String nodeValue)
        Set the value of an element.
        Parameters:
        parent - The parent element that will hold this subelement.
        nodeName - The name of the subelement.
        nodeValue - The value to set.
      • getStringValue

        public static java.lang.String getStringValue​(org.w3c.dom.Element node)
        This will get the text value of an element.
        Parameters:
        node - The node to get the text value for.
        Returns:
        The text of the node.
      • setStringValue

        public static void setStringValue​(org.w3c.dom.Element node,
                                          java.lang.String value)
        This will set the text value of an element.
        Parameters:
        node - The node to get the text value for.
        value - The new value to set the node to.
      • setElementableValue

        public static void setElementableValue​(org.w3c.dom.Element parent,
                                               java.lang.String name,
                                               Elementable node)
        Set an XML element document.
        Parameters:
        parent - The parent document to set the value in.
        name - The name of the XML element to set.
        node - The node to set or clear.
      • save

        public static void save​(org.w3c.dom.Document doc,
                                java.lang.String file,
                                java.lang.String encoding)
                         throws javax.xml.transform.TransformerException
        Save the XML document to a file.
        Parameters:
        doc - The XML document to save.
        file - The file to save the document to.
        encoding - The encoding to save the file as.
        Throws:
        javax.xml.transform.TransformerException - If there is an error while saving the XML.
      • save

        public static void save​(org.w3c.dom.Node doc,
                                java.io.OutputStream outStream,
                                java.lang.String encoding)
                         throws javax.xml.transform.TransformerException
        Save the XML document to an output stream.
        Parameters:
        doc - The XML document to save.
        outStream - The stream to save the document to.
        encoding - The encoding to save the file as.
        Throws:
        javax.xml.transform.TransformerException - If there is an error while saving the XML.
      • asByteArray

        public static byte[] asByteArray​(org.w3c.dom.Document doc,
                                         java.lang.String encoding)
                                  throws javax.xml.transform.TransformerException
        Convert the document to an array of bytes.
        Parameters:
        doc - The XML document.
        encoding - The encoding of the output data.
        Returns:
        The XML document as an array of bytes.
        Throws:
        javax.xml.transform.TransformerException - If there is an error transforming to text.