Top Description Constructors Methods
org.dom4j

public final Class DocumentHelper

extends Object
Class Inheritance
Annotations
@SuppressWarnings:unused
Imports
java.io.StringReader, java.util.List, .Map, .StringTokenizer, org.dom4j.io.SAXReader, org.dom4j.rule.Pattern, org.jaxen.VariableContext, org.xml.sax.InputSource, .SAXException

DocumentHelper is a collection of helper methods for using DOM4J.
Author
James Strachan
Version
$Revision: 1.26 $

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static Attribute
createAttribute(Element owner, QName qname, String value)

public static Attribute
createAttribute(Element owner, String name, String value)

public static CDATA
public static Comment
public static Document
public static Document
createDocument(Element rootElement)

public static Element
public static Element
public static Entity
public static Namespace
public static Pattern

Returns:

a new Pattern instance
createPattern
(String
is the XPath pattern expression to create
xpathPattern
)

createPattern parses the given XPath expression to create an XSLT style Patterninstance which can then be used in an XSLT processing model.

public static ProcessingInstruction
public static ProcessingInstruction
public static QName
createQName(String localName, Namespace namespace)

public static QName
createQName(String localName)

public static Text
public static XPath

Returns:

a new XPath instance
createXPath
(String
is the XPath expression to create
xpathExpression
)

createXPath parses an XPath expression and creates a new XPath XPath instance using the singleton DocumentFactory.

public static XPath

Returns:

a new XPath instance
createXPath
(String
is the XPath expression to create
xpathExpression
,
VariableContext
is the variable context to use when evaluating the XPath
context
)

createXPath parses an XPath expression and creates a new XPath XPath instance using the singleton DocumentFactory.

public static NodeFilter

Returns:

a new NodeFilter instance
createXPathFilter
(String
is the XPath filter expression to create
xpathFilterExpression
)

createXPathFilter parses a NodeFilter from the given XPath filter expression using the singleton DocumentFactory.

private static DocumentFactory
private static String
public static Element

Returns:

the first Element on the given path which either already existed on the path or were created by this method.
makeElement
(Branch
is the Element or Document to start navigating from
source
,
String
is a simple path expression, seperated by '/' which denotes the path from the source to the resulting element such as a/b/c
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.

public static Document

Returns:

a newly parsed Document
parseText
(String
the XML text to be parsed
text
)

parseText parses the given text as an XML document and returns the newly created Document.

public static List<Node>

Returns:

the results of all the XPath evaluations as a single list
selectNodes
(String
is the XPath filter expression to evaluate
xpathFilterExpression
,
List<Node>
is the list of nodes on which to evalute the XPath
nodes
)

selectNodes performs the given XPath expression on the Listof Nodeinstances appending all the results together into a single list.

public static List<Node>

Returns:

the results of all the XPath evaluations as a single list
selectNodes
(String
is the XPath filter expression to evaluate
xpathFilterExpression
,
Node
is the Node on which to evalute the XPath
node
)

selectNodes performs the given XPath expression on the Listof Nodeinstances appending all the results together into a single list.

public static void
sort(List<Node>
is the list of Nodes to sort
list
,
String
is the XPath expression used for comparison
xpathExpression
)

sort sorts the given List of Nodes using an XPath expression as a java.util.Comparator.

public static void
sort(List<Node>
is the list of Nodes to sort
list
,
String
is the XPath expression used for comparison
expression
,
boolean
if true then duplicate values (using the sortXPath for comparisions) will be removed from the List
distinct
)

sort sorts the given List of Nodes using an XPath expression as a java.util.Comparatorand optionally removing duplicates.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

DocumentHelperback to summary
private DocumentHelper()

Method Detail

createAttributeback to summary
public static Attribute createAttribute(Element owner, QName qname, String value)
createAttributeback to summary
public static Attribute createAttribute(Element owner, String name, String value)
createCDATAback to summary
public static CDATA createCDATA(String text)
createCommentback to summary
public static Comment createComment(String text)
createDocumentback to summary
public static Document createDocument()
createDocumentback to summary
public static Document createDocument(Element rootElement)
createElementback to summary
public static Element createElement(QName qname)
createElementback to summary
public static Element createElement(String name)
createEntityback to summary
public static Entity createEntity(String name, String text)
createNamespaceback to summary
public static Namespace createNamespace(String prefix, String uri)
createPatternback to summary
public static Pattern createPattern(String xpathPattern)

createPattern parses the given XPath expression to create an XSLT style Patterninstance which can then be used in an XSLT processing model.

Parameters
xpathPattern:String

is the XPath pattern expression to create

Returns:Pattern

a new Pattern instance

createProcessingInstructionback to summary
public static ProcessingInstruction createProcessingInstruction(String pi, String d)
createProcessingInstructionback to summary
public static ProcessingInstruction createProcessingInstruction(String pi, Map<String, String> data)
createQNameback to summary
public static QName createQName(String localName, Namespace namespace)
createQNameback to summary
public static QName createQName(String localName)
createTextback to summary
public static Text createText(String text)
createXPathback to summary
public static XPath createXPath(String xpathExpression) throws InvalidXPathException

createXPath parses an XPath expression and creates a new XPath XPath instance using the singleton DocumentFactory.

Parameters
xpathExpression:String

is the XPath expression to create

Returns:XPath

a new XPath instance

Exceptions
InvalidXPathException:
if the XPath expression is invalid
createXPathback to summary
public static XPath createXPath(String xpathExpression, VariableContext context) throws InvalidXPathException

createXPath parses an XPath expression and creates a new XPath XPath instance using the singleton DocumentFactory.

Parameters
xpathExpression:String

is the XPath expression to create

context:VariableContext

is the variable context to use when evaluating the XPath

Returns:XPath

a new XPath instance

Exceptions
InvalidXPathException:
if the XPath expression is invalid
createXPathFilterback to summary
public static NodeFilter createXPathFilter(String xpathFilterExpression)

createXPathFilter parses a NodeFilter from the given XPath filter expression using the singleton DocumentFactory. XPath filter expressions occur within XPath expressions such as self::node()[ filterExpression ]

Parameters
xpathFilterExpression:String

is the XPath filter expression to create

Returns:NodeFilter

a new NodeFilter instance

getDocumentFactoryback to summary
private static DocumentFactory getDocumentFactory()
getEncodingback to summary
private static String getEncoding(String text)
makeElementback to summary
public static Element makeElement(Branch source, 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 path a/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:Branch

is the Element or Document to start navigating from

path:String

is a simple path expression, seperated by '/' which denotes the path from the source to the resulting element such as a/b/c

Returns:Element

the first Element on the given path which either already existed on the path or were created by this method.

parseTextback to summary
public static Document parseText(String text) throws DocumentException

parseText parses the given text as an XML document and returns the newly created Document.

Loading external DTD and entities is disabled (if it is possible) for security reasons.
Parameters
text:String

the XML text to be parsed

Returns:Document

a newly parsed Document

Exceptions
DocumentException:
if the document could not be parsed
selectNodesback to summary
public static List<Node> selectNodes(String xpathFilterExpression, List<Node> nodes)

selectNodes performs the given XPath expression on the Listof Nodeinstances appending all the results together into a single list.

Parameters
xpathFilterExpression:String

is the XPath filter expression to evaluate

nodes:List<Node>

is the list of nodes on which to evalute the XPath

Returns:List<Node>

the results of all the XPath evaluations as a single list

selectNodesback to summary
public static List<Node> selectNodes(String xpathFilterExpression, Node node)

selectNodes performs the given XPath expression on the Listof Nodeinstances appending all the results together into a single list.

Parameters
xpathFilterExpression:String

is the XPath filter expression to evaluate

node:Node

is the Node on which to evalute the XPath

Returns:List<Node>

the results of all the XPath evaluations as a single list

sortback to summary
public static void sort(List<Node> list, String xpathExpression)

sort sorts the given List of Nodes using an XPath expression as a java.util.Comparator.

Parameters
list:List<Node>

is the list of Nodes to sort

xpathExpression:String

is the XPath expression used for comparison

sortback to summary
public static void sort(List<Node> list, String expression, boolean distinct)

sort sorts the given List of Nodes using an XPath expression as a java.util.Comparatorand optionally removing duplicates.

Parameters
list:List<Node>

is the list of Nodes to sort

expression:String

is the XPath expression used for comparison

distinct:boolean

if true then duplicate values (using the sortXPath for comparisions) will be removed from the List