Class DocumentHelper
- java.lang.Object
-
- org.dom4j.DocumentHelper
-
public final class DocumentHelper extends java.lang.Object
DocumentHelper
is a collection of helper methods for using DOM4J.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Attribute
createAttribute(Element owner, java.lang.String name, java.lang.String value)
static Attribute
createAttribute(Element owner, QName qname, java.lang.String value)
static CDATA
createCDATA(java.lang.String text)
static Comment
createComment(java.lang.String text)
static Document
createDocument()
static Document
createDocument(Element rootElement)
static Element
createElement(java.lang.String name)
static Element
createElement(QName qname)
static Entity
createEntity(java.lang.String name, java.lang.String text)
static Namespace
createNamespace(java.lang.String prefix, java.lang.String uri)
static Pattern
createPattern(java.lang.String xpathPattern)
createPattern
parses the given XPath expression to create an XSLT stylePattern
instance which can then be used in an XSLT processing model.static ProcessingInstruction
createProcessingInstruction(java.lang.String pi, java.lang.String d)
static ProcessingInstruction
createProcessingInstruction(java.lang.String pi, java.util.Map<java.lang.String,java.lang.String> data)
static QName
createQName(java.lang.String localName)
static QName
createQName(java.lang.String localName, Namespace namespace)
static Text
createText(java.lang.String text)
static XPath
createXPath(java.lang.String xpathExpression)
createXPath
parses an XPath expression and creates a new XPathXPath
instance using the singletonDocumentFactory
.static XPath
createXPath(java.lang.String xpathExpression, org.jaxen.VariableContext context)
createXPath
parses an XPath expression and creates a new XPathXPath
instance using the singletonDocumentFactory
.static NodeFilter
createXPathFilter(java.lang.String xpathFilterExpression)
createXPathFilter
parses a NodeFilter from the given XPath filter expression using the singletonDocumentFactory
.static Element
makeElement(Branch source, java.lang.String path)
makeElementstatic Document
parseText(java.lang.String text)
parseText
parses the given text as an XML document and returns the newly created Document.static java.util.List<Node>
selectNodes(java.lang.String xpathFilterExpression, java.util.List<Node> nodes)
selectNodes
performs the given XPath expression on theList
ofNode
instances appending all the results together into a single list.static java.util.List<Node>
selectNodes(java.lang.String xpathFilterExpression, Node node)
selectNodes
performs the given XPath expression on theList
ofNode
instances appending all the results together into a single list.static void
sort(java.util.List<Node> list, java.lang.String xpathExpression)
sort
sorts the given List of Nodes using an XPath expression as aComparator
.static void
sort(java.util.List<Node> list, java.lang.String expression, boolean distinct)
sort
sorts the given List of Nodes using an XPath expression as aComparator
and optionally removing duplicates.
-
-
-
Method Detail
-
createDocument
public static Document createDocument()
-
createElement
public static Element createElement(java.lang.String name)
-
createAttribute
public static Attribute createAttribute(Element owner, QName qname, java.lang.String value)
-
createAttribute
public static Attribute createAttribute(Element owner, java.lang.String name, java.lang.String value)
-
createCDATA
public static CDATA createCDATA(java.lang.String text)
-
createComment
public static Comment createComment(java.lang.String text)
-
createText
public static Text createText(java.lang.String text)
-
createEntity
public static Entity createEntity(java.lang.String name, java.lang.String text)
-
createNamespace
public static Namespace createNamespace(java.lang.String prefix, java.lang.String uri)
-
createProcessingInstruction
public static ProcessingInstruction createProcessingInstruction(java.lang.String pi, java.lang.String d)
-
createProcessingInstruction
public static ProcessingInstruction createProcessingInstruction(java.lang.String pi, java.util.Map<java.lang.String,java.lang.String> data)
-
createQName
public static QName createQName(java.lang.String localName)
-
createXPath
public static XPath createXPath(java.lang.String xpathExpression) throws InvalidXPathException
createXPath
parses an XPath expression and creates a new XPathXPath
instance using the singletonDocumentFactory
.- Parameters:
xpathExpression
- is the XPath expression to create- Returns:
- a new
XPath
instance - Throws:
InvalidXPathException
- if the XPath expression is invalid
-
createXPath
public static XPath createXPath(java.lang.String xpathExpression, org.jaxen.VariableContext context) throws InvalidXPathException
createXPath
parses an XPath expression and creates a new XPathXPath
instance using the singletonDocumentFactory
.- Parameters:
xpathExpression
- is the XPath expression to createcontext
- is the variable context to use when evaluating the XPath- Returns:
- a new
XPath
instance - Throws:
InvalidXPathException
- if the XPath expression is invalid
-
createXPathFilter
public static NodeFilter createXPathFilter(java.lang.String xpathFilterExpression)
createXPathFilter
parses a NodeFilter from the given XPath filter expression using the singletonDocumentFactory
. XPath filter expressions occur within XPath expressions such asself::node()[ filterExpression ]
- Parameters:
xpathFilterExpression
- is the XPath filter expression to create- Returns:
- a new
NodeFilter
instance
-
createPattern
public static Pattern createPattern(java.lang.String xpathPattern)
createPattern
parses the given XPath expression to create an XSLT stylePattern
instance which can then be used in an XSLT processing model.- Parameters:
xpathPattern
- is the XPath pattern expression to create- Returns:
- a new
Pattern
instance
-
selectNodes
public static java.util.List<Node> selectNodes(java.lang.String xpathFilterExpression, java.util.List<Node> nodes)
selectNodes
performs the given XPath expression on theList
ofNode
instances appending all the results together into a single list.- Parameters:
xpathFilterExpression
- is the XPath filter expression to evaluatenodes
- is the list of nodes on which to evalute the XPath- Returns:
- the results of all the XPath evaluations as a single list
-
selectNodes
public static java.util.List<Node> selectNodes(java.lang.String xpathFilterExpression, Node node)
selectNodes
performs the given XPath expression on theList
ofNode
instances appending all the results together into a single list.- Parameters:
xpathFilterExpression
- is the XPath filter expression to evaluatenode
- is the Node on which to evalute the XPath- Returns:
- the results of all the XPath evaluations as a single list
-
sort
public static void sort(java.util.List<Node> list, java.lang.String xpathExpression)
sort
sorts the given List of Nodes using an XPath expression as aComparator
.- Parameters:
list
- is the list of Nodes to sortxpathExpression
- is the XPath expression used for comparison
-
sort
public static void sort(java.util.List<Node> list, java.lang.String expression, boolean distinct)
sort
sorts the given List of Nodes using an XPath expression as aComparator
and optionally removing duplicates.- Parameters:
list
- is the list of Nodes to sortexpression
- is the XPath expression used for comparisondistinct
- if true then duplicate values (using the sortXPath for comparisions) will be removed from the List
-
parseText
public static Document parseText(java.lang.String text) throws DocumentException
parseText
parses the given text as an XML document and returns the newly created Document.- Parameters:
text
- the XML text to be parsed- Returns:
- a newly parsed Document
- Throws:
DocumentException
- if the document could not be parsed
-
makeElement
public static Element makeElement(Branch source, java.lang.String path)
makeElement
a helper method which navigates from the given Document or Element node to some Element using the path expression, creating any necessary elements along the way. For example the patha/b/c
would get the first child <a> element, which would be created if it did not exist, then the next child <b> and so on until finally a <c> element is returned.- Parameters:
source
- is the Element or Document to start navigating frompath
- is a simple path expression, seperated by '/' which denotes the path from the source to the resulting element such as a/b/c- Returns:
- the first Element on the given path which either already existed on the path or were created by this method.
-
-