Class DocumentHelper
DocumentHelper is a collection of helper methods for using
DOM4J.- Version:
- $Revision: 1.26 $
- Author:
- James Strachan
-
Method Summary
Modifier and TypeMethodDescriptionstatic AttributecreateAttribute(Element owner, String name, String value) static AttributecreateAttribute(Element owner, QName qname, String value) static CDATAcreateCDATA(String text) static CommentcreateComment(String text) static Documentstatic DocumentcreateDocument(Element rootElement) static ElementcreateElement(String name) static ElementcreateElement(QName qname) static EntitycreateEntity(String name, String text) static NamespacecreateNamespace(String prefix, String uri) static PatterncreatePattern(String xpathPattern) createPatternparses the given XPath expression to create an XSLT stylePatterninstance which can then be used in an XSLT processing model.static ProcessingInstructionstatic ProcessingInstructioncreateProcessingInstruction(String pi, Map<String, String> data) static QNamecreateQName(String localName) static QNamecreateQName(String localName, Namespace namespace) static TextcreateText(String text) static XPathcreateXPath(String xpathExpression) createXPathparses an XPath expression and creates a new XPathXPathinstance using the singletonDocumentFactory.static XPathcreateXPath(String xpathExpression, org.jaxen.VariableContext context) createXPathparses an XPath expression and creates a new XPathXPathinstance using the singletonDocumentFactory.static NodeFiltercreateXPathFilter(String xpathFilterExpression) createXPathFilterparses a NodeFilter from the given XPath filter expression using the singletonDocumentFactory.static ElementmakeElement(Branch source, String path) makeElementstatic DocumentparseTextparses the given text as an XML document and returns the newly created Document.selectNodes(String xpathFilterExpression, List<Node> nodes) selectNodes(String xpathFilterExpression, Node node) static voidsortsorts the given List of Nodes using an XPath expression as aComparator.static voidsortsorts the given List of Nodes using an XPath expression as aComparatorand optionally removing duplicates.
-
Method Details
-
createDocument
-
createDocument
-
createElement
-
createElement
-
createAttribute
-
createAttribute
-
createCDATA
-
createComment
-
createText
-
createEntity
-
createNamespace
-
createProcessingInstruction
-
createProcessingInstruction
-
createQName
-
createQName
-
createXPath
createXPathparses an XPath expression and creates a new XPathXPathinstance using the singletonDocumentFactory.- Parameters:
xpathExpression- is the XPath expression to create- Returns:
- a new
XPathinstance - Throws:
InvalidXPathException- if the XPath expression is invalid
-
createXPath
public static XPath createXPath(String xpathExpression, org.jaxen.VariableContext context) throws InvalidXPathException createXPathparses an XPath expression and creates a new XPathXPathinstance using the singletonDocumentFactory.- Parameters:
xpathExpression- is the XPath expression to createcontext- is the variable context to use when evaluating the XPath- Returns:
- a new
XPathinstance - Throws:
InvalidXPathException- if the XPath expression is invalid
-
createXPathFilter
createXPathFilterparses 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
NodeFilterinstance
-
createPattern
-
selectNodes
selectNodesperforms the given XPath expression on theListofNodeinstances 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
selectNodesperforms the given XPath expression on theListofNodeinstances 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
sortsorts 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
sortsorts the given List of Nodes using an XPath expression as aComparatorand 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
Loading external DTD and entities is disabled (if it is possible) for security reasons.parseTextparses 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
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/cwould 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.
-