org.dom4j

Class DocumentHelper

public final class DocumentHelper extends Object

DocumentHelper is a collection of helper methods for using DOM4J.

Version: $Revision: 1.26 $

Author: James Strachan

Method Summary
static AttributecreateAttribute(Element owner, QName qname, String value)
static AttributecreateAttribute(Element owner, String name, String value)
static CDATAcreateCDATA(String text)
static CommentcreateComment(String text)
static DocumentcreateDocument()
static DocumentcreateDocument(Element rootElement)
static ElementcreateElement(QName qname)
static ElementcreateElement(String name)
static EntitycreateEntity(String name, String text)
static NamespacecreateNamespace(String prefix, String uri)
static PatterncreatePattern(String xpathPattern)

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

static ProcessingInstructioncreateProcessingInstruction(String pi, String d)
static ProcessingInstructioncreateProcessingInstruction(String pi, Map data)
static QNamecreateQName(String localName, Namespace namespace)
static QNamecreateQName(String localName)
static TextcreateText(String text)
static XPathcreateXPath(String xpathExpression)

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

static XPathcreateXPath(String xpathExpression, VariableContext context)

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

static NodeFiltercreateXPathFilter(String xpathFilterExpression)

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

static ElementmakeElement(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.
static DocumentparseText(String text)

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

static ListselectNodes(String xpathFilterExpression, List nodes)

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

static ListselectNodes(String xpathFilterExpression, Node node)

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

static voidsort(List list, String xpathExpression)

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

static voidsort(List list, String expression, boolean distinct)

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

Method Detail

createAttribute

public static Attribute createAttribute(Element owner, QName qname, String value)

createAttribute

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

createCDATA

public static CDATA createCDATA(String text)

createComment

public static Comment createComment(String text)

createDocument

public static Document createDocument()

createDocument

public static Document createDocument(Element rootElement)

createElement

public static Element createElement(QName qname)

createElement

public static Element createElement(String name)

createEntity

public static Entity createEntity(String name, String text)

createNamespace

public static Namespace createNamespace(String prefix, String uri)

createPattern

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 is the XPath pattern expression to create

Returns: a new Pattern instance

createProcessingInstruction

public static ProcessingInstruction createProcessingInstruction(String pi, String d)

createProcessingInstruction

public static ProcessingInstruction createProcessingInstruction(String pi, Map data)

createQName

public static QName createQName(String localName, Namespace namespace)

createQName

public static QName createQName(String localName)

createText

public static Text createText(String text)

createXPath

public static XPath createXPath(String xpathExpression)

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

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(String xpathExpression, VariableContext context)

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

Parameters: xpathExpression is the XPath expression to create context 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(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 is the XPath filter expression to create

Returns: a new NodeFilter instance

makeElement

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 is the Element or Document to start navigating from path 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.

parseText

public static Document parseText(String text)

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

selectNodes

public static List selectNodes(String xpathFilterExpression, List nodes)

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

Parameters: xpathFilterExpression is the XPath filter expression to evaluate nodes 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 List 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 is the XPath filter expression to evaluate node 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(List list, String xpathExpression)

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

Parameters: list is the list of Nodes to sort xpathExpression is the XPath expression used for comparison

sort

public static void sort(List 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 is the list of Nodes to sort expression is the XPath expression used for comparison distinct if true then duplicate values (using the sortXPath for comparisions) will be removed from the List

Copyright B) 2005 MetaStuff Ltd. All Rights Reserved. Hosted by

SourceForge