Class DomWrapper


  • public class DomWrapper
    extends Object
    Utility to create a DOM structure for an XML source and provide XPath-based access and iteration over it.

    Default is to not be namespace aware. If set to be namespace aware then the namespace of the root node of the document will be used as the default namespace. Additional namespace mappings can be registered with addnamespace.

    Author:
    Dave Reynolds
    • Constructor Detail

      • DomWrapper

        public DomWrapper()
    • Method Detail

      • setNamespaceAware

        public void setNamespaceAware​(boolean aware)
        Set to true to make the parse and accesses namespace aware.
      • addNamespace

        public void addNamespace​(String prefix,
                                 String uri)
        Register a namespace prefix
      • load

        public void load​(String file)
        Load and parse a source document.
      • load

        public void load​(InputStream in)
        Load and parse a source document.
      • getDocument

        public Document getDocument()
        Return the document root node.
      • findNode

        public Node findNode​(String path)
        Find the node addressed by the given path starting from the root of the document
      • findNode

        public Node findNode​(String path,
                             Node root)
        Find the node addressed by the given path starting from the given root
      • listNodes

        public List<Node> listNodes​(String path)
        Find all nodes matching the given xpath expression, starting from the root of the document
      • listNodes

        public List<Node> listNodes​(String path,
                                    Node root)
        Find all nodes matching the given xpath expression
        Parameters:
        path - the expression to search for
        root - the root node to search from
        Returns:
        list of matching nodes
      • getRequiredAttribute

        public static String getRequiredAttribute​(Node n,
                                                  String name)
        Helper function. Retrieve a required attribute value from a node.
      • getAttribute

        public static String getAttribute​(Node n,
                                          String name)
        Helper function. Retrieve an attribute value from a node or return null if not present
      • textNode

        public String textNode​(String path,
                               Node root)
        Return the text value addressed by the given xpath starting from the given root.
      • textNodes

        public String textNodes​(String path,
                                Node root)
        Return the text value addressed by the given xpath starting from the given root.
      • textNode

        public String textNode​(String path)
        Return the text value addressed by the given xpath starting from the document root
      • serialize

        public static String serialize​(Node root)
        Return a serialization of element/text contents of this node.
      • flatten

        public static String flatten​(Node n)
        Return textual serialization of the text content of the node, stripping out nested elements. For example:
            <emphasis>g</emphasis> general intelligence
         
        would return "g general intelligence"