Class DomUtils


  • public class DomUtils
    extends Object
    • Method Detail

      • createElement

        public static Element createElement​(Document document,
                                            String name)
        Create a new element
        Parameters:
        document - Document to contain the new element
        name - the element name
        Returns:
        new Element
      • createElement

        public static Element createElement​(Element parent,
                                            String name)
        Add a new element to the given parent
        Parameters:
        parent - the parent Element
        name - the child name
        Returns:
        new Element
      • addText

        public static void addText​(Element element,
                                   String text)
        Add Text object to an Element.
        Parameters:
        element - the containing element
        text - the text to add
      • addEntity

        public static void addEntity​(Element element,
                                     String entity)
        Add an entity to a specified Element. (eg DomUtils.addEntity(element, "nbsp");)
        Parameters:
        element - the containing element
        entity - the entity to add
      • normalizeText

        public static String normalizeText​(String update)
        "Normalize" XML text node content to create a simple string
        Parameters:
        update - Text to add to the original string
        Returns:
        Concatenated contents (trimmed, pagination characters (\r, \n, etc.) removed, with a space seperator)
      • normalizeText

        public static String normalizeText​(String original,
                                           String update)
        "Normalize" XML text node content to create a simple string
        Parameters:
        original - Original text
        update - Text to add to the original string
        Returns:
        Concatenated contents (trimmed, pagination characters (\r, \n, etc.) removed, with a space seperator)
      • getText

        public static String getText​(Node parent)
        Get the text associated with this element, at this level only
        Parameters:
        parent - the node containing text
        Returns:
        Text (trimmed of leading/trailing whitespace, null if none)
      • getText

        public static String getText​(Node parent,
                                     String elementName)
        Get the text associated with a specified element, at this level only
        Parameters:
        parent - the node containing text
        elementName - Element with the text we want to fetch
        Returns:
        Text (trimmed of leading/trailing whitespace, null if none)
      • getTextNS

        public static String getTextNS​(String namespace,
                                       Node parent,
                                       String elementName)
        Get the text associated with a specified element, at this level only - namespace aware
        Parameters:
        namespace - Namespace URI
        parent - the node containing text
        elementName - Element with the text we want to fetch
        Returns:
        Text (trimmed of leading/trailing whitespace, null if none)
      • getAllTextAtNode

        public static String getAllTextAtNode​(Node parent)
        Get the text associated with this element, at all suboordinate levels
        Parameters:
        parent - the node containing text
        Returns:
        Text (trimmed of leading/trailing whitespace, null if none)
      • textSearch

        public static String textSearch​(Node parent,
                                        boolean recursiveSearch)
        Get the text associated with this element at this level only, or recursivley, searching through all child elements
        Parameters:
        parent - the node containing text
        recursiveSearch - Search all child elements?
        Returns:
        Text (trimmed of leading/trailing whitespace, null if none)
      • getFirstText

        public static String getFirstText​(Node parent)
        Get the first text node associated with this element
        Parameters:
        parent - the node containing text
        Returns:
        Text (trimmed of leanding/trailing whitespace, null if none)
      • getTextNodeByNumber

        public static String getTextNodeByNumber​(Node parent,
                                                 int number)
        Get the specified text node associated with this element
        Parameters:
        parent - the node containing text
        number - The text node to fetch (1st, 2nd, etc)
        Returns:
        Text (trimmed of leanding/trailing whitespace, null if none)
      • getAllText

        public static String getAllText​(Node parent)
        Get any text associated with this element and it's children. Null if none.
        Parameters:
        parent - the node containing text
        Returns:
        Text
      • getAttribute

        public static String getAttribute​(Element element,
                                          String name)
        Get an Attribute from an Element. Returns an empty String if none found
        Parameters:
        element - the containing Element
        name - the attribute name
        Returns:
        Attribute as a String
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        String value)
        Set an Attribute in an Element
        Parameters:
        element - the containing Element
        name - the attribute name
        value - the attribute value
      • getElementList

        public static NodeList getElementList​(Element element,
                                              String name)
        Return a list of named Elements.
        Parameters:
        element - the containing Element
        name - the tag name
        Returns:
        NodeList of matching elements
      • getElementListNS

        public static NodeList getElementListNS​(String namespace,
                                                Element element,
                                                String name)
        Return a list of specified namespace:Elements
        Parameters:
        namespace - Namespace URI
        element - the containing Element
        name - the tag name
        Returns:
        NodeList of matching elements
      • selectElementsByAttributeValue

        public static List selectElementsByAttributeValue​(Element element,
                                                          String name,
                                                          String attribute,
                                                          String value)
        Return a list of named Elements with a specific attribute value.
        Parameters:
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        Returns:
        List of matching elements
      • selectFirstElementByAttributeValue

        public static Element selectFirstElementByAttributeValue​(Element element,
                                                                 String name,
                                                                 String attribute,
                                                                 String value)
        Return the first named Element with a specific attribute value.
        Parameters:
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        Returns:
        The first matching Element (null if none)
      • selectElementsByAttributeValue

        public static List selectElementsByAttributeValue​(Element element,
                                                          String name,
                                                          String attribute,
                                                          String value,
                                                          boolean returnFirst)
        Return a list of named Elements with a specific attribute value.
        Parameters:
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        returnFirst - Return only the first matching value?
        Returns:
        List of matching elements
      • selectElementsByAttributeValueNS

        public static List selectElementsByAttributeValueNS​(String namespace,
                                                            Element element,
                                                            String name,
                                                            String attribute,
                                                            String value)
        Return a list of named Elements with a specific attribute value (namespace aware)
        Parameters:
        namespace - Namespace URI
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        Returns:
        List of matching elements
      • selectFirstElementByAttributeValueNS

        public static Element selectFirstElementByAttributeValueNS​(String namespace,
                                                                   Element element,
                                                                   String name,
                                                                   String attribute,
                                                                   String value)
        Return the first named Element with a specific attribute value (namespace aware) namespace The namespace URI
        Parameters:
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        Returns:
        The first matching Element (null if none)
      • selectElementsByAttributeValueNS

        public static List selectElementsByAttributeValueNS​(String namespace,
                                                            Element element,
                                                            String name,
                                                            String attribute,
                                                            String value,
                                                            boolean returnFirst)
        Return a list of named Elements with a specific attribute value (namespace aware)
        Parameters:
        namespace - Namespace URI
        element - the containing Element
        name - the tag name
        attribute - Attribute name
        value - Attribute value
        returnFirst - Return only the first matching value?
        Returns:
        List of matching elements
      • getElement

        public static Element getElement​(Element element,
                                         String name)
        Return the first named Element found.
        Parameters:
        element - the containing Element
        name - the tag name
        Returns:
        matching Element (null if none)
      • getElementNS

        public static Element getElementNS​(String namespace,
                                           Element element,
                                           String name)
        Return the first named Element found - namespace aware
        Parameters:
        namespace - Namespace URI
        element - the containing Element
        name - the tag name
        Returns:
        matching Element (null if none)
      • removeNode

        public Node removeNode​(Node node)
        Remove this node from its parent.
        Parameters:
        node - the node to remove
        Returns:
        Node removed
      • getPreviousNodeByName

        public static Node getPreviousNodeByName​(Node currentNode,
                                                 String tagName)
        Search up the tree for a given node
        Parameters:
        currentNode - Starting point for our search
        tagName - Node name to look up
        Returns:
        matching Node (null if none)
      • getPreviousSiblingByName

        public static Node getPreviousSiblingByName​(Node currentNode,
                                                    String tagName)
        Search earlier siblings for a given node
        Parameters:
        currentNode - Starting point for our search
        tagName - Node name to look up
        Returns:
        matching Node (null if none)
      • getNextSiblingByName

        public static Node getNextSiblingByName​(Node currentNode,
                                                String tagName)
        Search our next siblings for a given node
        Parameters:
        currentNode - Starting point for our search
        tagName - Node name to look up
        Returns:
        matching Node (null if none)
      • getNextNodeByName

        public static Node getNextNodeByName​(Node currentNode,
                                             String tagName)
        Search across the tree for a given sibling
        Parameters:
        currentNode - Starting point for our search
        tagName - Node name to look up
        Returns:
        matching Node (null if none)
      • getChildSiblingByName

        public static Node getChildSiblingByName​(Node currentNode,
                                                 String tagName)
        Search for a named child of a given node
        Parameters:
        currentNode - Starting point for our search
        tagName - Node name to look up
        Returns:
        matching Node (null if none)
      • getXmlDocumentBuilderNS

        public static DocumentBuilder getXmlDocumentBuilderNS()
                                                       throws DomException
        Get a DOM Document builder - namespace aware
        Returns:
        The DocumentBuilder
        Throws:
        DomException
      • getXmlDocumentBuilder

        public static DocumentBuilder getXmlDocumentBuilder​(boolean namespaceAware)
                                                     throws DomException
        Get a DOM Document builder.
        Parameters:
        namespaceAware - true if we're to handle namespace details
        Returns:
        The DocumentBuilder
        Throws:
        DomException
      • createXmlDocument

        public static Document createXmlDocument()
                                          throws DomException
        Start a new XML Document (with root name = xml)
        Returns:
        the Document
        Throws:
        DomException
      • createXmlDocument

        public static Document createXmlDocument​(String rootName)
                                          throws DomException
        Start a new XML Document.
        Parameters:
        rootName - The name of the Document root Element (created here)
        Returns:
        the Document
        Throws:
        DomException
      • copyDocument

        public static void copyDocument​(Document source,
                                        Document target)
        Copy an XML document, adding it as a child of the target document root
        Parameters:
        source - Document to copy
        target - Document to contain copy
      • copyDocumentNode

        public static void copyDocumentNode​(Node source,
                                            Document target)
        Copy a Node from one source document, adding it to the document root of a different, target Document
        Parameters:
        source - Document to copy
        target - Document to contain copy
      • parseXmlStream

        public static Document parseXmlStream​(InputStream xmlStream)
                                       throws DomException
        Parse XML text (from an input stream) into a Document.
        Parameters:
        xmlStream - The XML text stream
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlStreamNS

        public static Document parseXmlStreamNS​(InputStream xmlStream)
                                         throws DomException
        Parse XML text (from an input stream) into a Document - namespace aware.
        Parameters:
        xmlStream - The XML text stream
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlReader

        public static Document parseXmlReader​(Reader xmlReader)
                                       throws DomException
        Parse XML text (from a Reader) into a Document.
        Parameters:
        xmlReader - The XML Reader
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlBytes

        public static Document parseXmlBytes​(byte[] xml)
                                      throws DomException
        Parse XML text (from a raw byte array) into a Document.
        Parameters:
        xml - The XML text
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlBytesNS

        public static Document parseXmlBytesNS​(byte[] xml)
                                        throws DomException
        Parse XML text (from a raw byte array) into a Document - namespace aware.
        Parameters:
        xml - The XML text
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlString

        public static Document parseXmlString​(String xml)
                                       throws DomException
        Parse XML text (from a string) into a Document.
        Parameters:
        xml - The XML text
        Returns:
        DOM Document
        Throws:
        DomException
      • parseXmlFile

        public static Document parseXmlFile​(String filename)
                                     throws DomException
        Parse an XML file into a Document.
        Parameters:
        filename - - The filename to parse
        Returns:
        DOM Document
        Throws:
        DomException
      • parseHtmlBytes

        public static Document parseHtmlBytes​(byte[] html)
                                       throws DomException
        Parse HTML text (from a raw byte array) into a Document.
        Parameters:
        html - The HTML text
        Returns:
        DOM Document
        Throws:
        DomException -

        The used to be: parseHtmlStream(new ByteArrayInputStream(html));

      • parseHtmlString

        public static Document parseHtmlString​(String html)
                                        throws DomException
        Parse HTML text (from a String) into a Document.
        Parameters:
        html - The HTML text
        Returns:
        DOM Document
        Throws:
        DomException -

        This used to be: return parseHtmlReader(new StringReader(html));

      • serializeXml

        public static void serializeXml​(Node node,
                                        OutputStream target)
                                 throws DomException
        Write formatted XML text to supplied OutputStream.
        Parameters:
        node - Node to write
        target - stream to write to
        Throws:
        DomException
      • serializeXml

        public static void serializeXml​(Node node,
                                        Writer writer)
                                 throws DomException
        Write formatted XML text to supplied Writer.
        Parameters:
        node - the Node to write
        writer - Writer the document is written to
        Throws:
        DomException
      • serialize

        public static String serialize​(Object object)
                                throws DomException
        Write formatted XML text to a String.
        Parameters:
        object - The XML Document, HTML Document, or Element to write
        Returns:
        String containing the formatted document text
        Throws:
        DomException