Package com.epimorphics.xmlutil
Class DomWrapper
- java.lang.Object
-
- com.epimorphics.xmlutil.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 Summary
Constructors Constructor Description DomWrapper()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddNamespace(String prefix, String uri)Register a namespace prefixNodefindNode(String path)Find the node addressed by the given path starting from the root of the documentNodefindNode(String path, Node root)Find the node addressed by the given path starting from the given rootstatic Stringflatten(Node n)Return textual serialization of the text content of the node, stripping out nested elements.static StringBufferflattenTo(Node n, StringBuffer buffer)static StringgetAttribute(Node n, String name)Helper function.DocumentgetDocument()Return the document root node.static StringgetRequiredAttribute(Node n, String name)Helper function.List<Node>listNodes(String path)Find all nodes matching the given xpath expression, starting from the root of the documentList<Node>listNodes(String path, Node root)Find all nodes matching the given xpath expressionvoidload(InputStream in)Load and parse a source document.voidload(String file)Load and parse a source document.static Stringserialize(Node root)Return a serialization of element/text contents of this node.static StringBufferserializeChildren(Node root, StringBuffer buffer)static StringBufferserializeTo(Node n, StringBuffer buffer)voidsetNamespaceAware(boolean aware)Set to true to make the parse and accesses namespace aware.StringtextNode(String path)Return the text value addressed by the given xpath starting from the document rootStringtextNode(String path, Node root)Return the text value addressed by the given xpath starting from the given root.StringtextNodes(String path, Node root)Return the text value addressed by the given xpath starting from the given root.
-
-
-
Method Detail
-
setNamespaceAware
public void setNamespaceAware(boolean aware)
Set to true to make the parse and accesses namespace aware.
-
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 forroot- 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.
-
serializeTo
public static StringBuffer serializeTo(Node n, StringBuffer buffer)
-
serializeChildren
public static StringBuffer serializeChildren(Node root, StringBuffer buffer)
-
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 intelligencewould return "g general intelligence"
-
flattenTo
public static StringBuffer flattenTo(Node n, StringBuffer buffer)
-
-