public class DomUtils extends Object
Modifier and Type | Field and Description |
---|---|
protected static DocumentBuilderFactory |
factory |
protected static org.apache.log4j.Logger |
logger |
protected static DocumentBuilderFactory |
validatingFactory |
Constructor and Description |
---|
DomUtils() |
Modifier and Type | Method and Description |
---|---|
static Element |
closestCommonAncestor(Node first,
Node last) |
static void |
compareNodes(Node expected,
Node actual,
boolean trimEmptyTextNodes)
Compare two DOM trees.
|
static org.w3c.dom.traversal.NodeIterator |
createNodeIterator(Document doc,
Node root,
String... tagNames) |
static org.w3c.dom.traversal.NodeIterator |
createNodeIterator(Node root,
String... tagNames) |
static org.w3c.dom.traversal.TreeWalker |
createTreeWalker(Document doc,
Node root,
String... tagNames) |
static org.w3c.dom.traversal.TreeWalker |
createTreeWalker(Node root,
String... tagNames) |
static void |
document2File(Document document,
File file)
Write a DOM document into a file.
|
static void |
document2Stream(Document document,
OutputStream target)
Write a DOM document into an output stream.
|
static String |
document2String(Document document)
Write a DOM document into a string.
|
static Element |
encloseNodesWithNewElement(Node first,
Node last,
String newElementName)
Create a new Element placed in the document tree such that it encloses two existing Nodes.
|
static Node |
getAncestor(Node node,
String ancestorName)
If
node has an ancestor with name ancestorName , return that ancestor. |
static Node |
getAncestor(Node node,
String[] ancestorNames)
If
node has an ancestor with one of the names in ancestorNames , return the closest of these
ancestors. |
static String |
getAttributeFromClosestAncestorOfAnyKind(Node node,
String attributeName)
Climb through
node 's ancestors, looking for one with an attribute named attributeName ,
irrespective of the respective ancestorName , and return the attribute's value |
static Element |
getClosestAncestorWithAttribute(Node node,
String ancestorName,
String attributeName)
Search upwards through the ancestors of
node with name ancestorName , and return the first
ancestor for which an attribute named attributeName is present. |
static Element |
getFirstChildElement(Element e)
Get the first child of
e which is an element, or null if there is no such element. |
static Element |
getFirstElementByTagName(Node n,
String name)
Get the first child element with the given tag name, or
null if there is no such element. |
static Node |
getHighestLevelAncestor(Node node,
String ancestorName)
If
node has ancestors with name ancestorName , return the one closest to the root. |
static Element |
getLastChildElement(Element e)
Get the last child of
e which is an element, or null if there is no such element. |
static Element |
getLastElementByTagName(Element e,
String name)
Get the last descendant element with the given tag name, or
null if there is no such element. |
static Element |
getNextOfItsKindIn(Element element,
Element root)
Find the Element with the same tag name as
element following element within the same subtree
under root . |
static Element |
getNextSiblingElement(Element e)
Get the next sibling of
e which is an element, or null if there is no such element. |
static Element |
getNextSiblingElementByTagName(Element e,
String name)
Get the next sibling of
e which is an element and has tag name name , or null if
there is no such element. |
static List<Node> |
getNodeListAsList(NodeList nl) |
static String |
getPlainTextBelow(Node n)
Return the concatenation of the values of all text nodes below the given node.
|
static Element |
getPreviousOfItsKindIn(Element element,
Element root)
Find the Element with the same tag name as
element preceding element within the same subtree
under root . |
static Element |
getPreviousSiblingElement(Element e)
Get the previous sibling of
e which is an element, or null if there is no such element. |
static Element |
getPreviousSiblingElementByTagName(Element e,
String name)
Get the previous sibling of
e which is an element and has tag name name , or null if
there is no such element. |
static boolean |
hasAncestor(Node node,
String ancestorName)
Verify if
node has an ancestor with name ancestorName |
static void |
insertAfter(Node newNode,
Node refNode)
Analogous to the Node.insertBefore() method, insert a newNode after a refNode.
|
static boolean |
isAncestor(Node ancestor,
Node node)
Verify if
ancestor is an ancestor of node |
static boolean |
isFirstOfItsKindIn(Node node,
Node ancestor) |
static boolean |
isFirstOfItsKindIn(Node node,
String ancestorName) |
static boolean |
isLastOfItsKindIn(Node node,
Node ancestor) |
static boolean |
isLastOfItsKindIn(Node node,
String ancestorName) |
static Document |
parseDocument(File file)
DOM-parse the given input data.
|
static Document |
parseDocument(File file,
boolean validating)
Parse XML data into a DOM representation, taking local resources and Schemas into account.
|
static Document |
parseDocument(InputStream inputData)
DOM-parse the given input data.
|
static Document |
parseDocument(InputStream is,
boolean validating)
Parse XML data into a DOM representation, taking local resources and Schemas into account.
|
static Document |
parseDocument(Reader inputData)
DOM-parse the given input data.
|
static Document |
parseDocument(Reader inputData,
boolean validating)
Parse XML data into a DOM representation, taking local resources and Schemas into account.
|
static Document |
parseDocument(String inputData)
DOM-parse the given input data.
|
static Document |
parseDocument(String inputData,
boolean validating)
Parse XML data into a DOM representation, taking local resources and Schemas into account.
|
static void |
replaceElement(Element oldElement,
NodeList newNodes) |
static String |
serializeToString(Document doc)
Serialize a Document to a String.
|
static void |
trimAllTextNodes(Node root)
Go through all text nodes below this node, and replace their text with a trimmed version of their text.
|
static void |
trimEmptyTextNodes(Node node)
Remove any empty text nodes below node.
|
protected static DocumentBuilderFactory factory
protected static DocumentBuilderFactory validatingFactory
protected static org.apache.log4j.Logger logger
public static Document parseDocument(String inputData, boolean validating) throws ParserConfigurationException, SAXException, IOException
inputData
- a string representation of the XML data to be parsed.validating
- whether to Schema-validate the XML dataParserConfigurationException
- if no parser could be createdSAXException
- if there was a parse errorIOException
- if there was a problem reading from the stringpublic static Document parseDocument(Reader inputData, boolean validating) throws ParserConfigurationException, SAXException, IOException
inputData
- a reader from which the XML data is to be read.validating
- whether to Schema-validate the XML dataParserConfigurationException
- if no parser could be createdSAXException
- if there was a parse errorIOException
- if there was a problem reading from the readerpublic static Document parseDocument(File file, boolean validating) throws ParserConfigurationException, SAXException, IOException
file
- a file from which the XML data is to be read.validating
- whether to Schema-validate the XML dataParserConfigurationException
- if no parser could be createdSAXException
- if there was a parse errorIOException
- if there was a problem reading from the filepublic static Document parseDocument(InputStream is, boolean validating) throws ParserConfigurationException, SAXException, IOException
is
- an input stream from which the XML data is to be read.validating
- whether to Schema-validate the XML dataParserConfigurationException
- if no parser could be createdSAXException
- if there was a parse errorIOException
- if there was a problem reading from the streampublic static Document parseDocument(String inputData) throws ParserConfigurationException, SAXException, IOException
inputData
- inputDataParserConfigurationException
- ParserConfigurationExceptionSAXException
- SAXExceptionIOException
- IOExceptionpublic static Document parseDocument(Reader inputData) throws ParserConfigurationException, SAXException, IOException
inputData
- inputDataParserConfigurationException
- ParserConfigurationExceptionSAXException
- SAXExceptionIOException
- IOExceptionpublic static Document parseDocument(File file) throws ParserConfigurationException, SAXException, IOException
file
- fileParserConfigurationException
- ParserConfigurationExceptionSAXException
- SAXExceptionIOException
- IOExceptionpublic static Document parseDocument(InputStream inputData) throws ParserConfigurationException, SAXException, IOException
inputData
- inputDataParserConfigurationException
- ParserConfigurationExceptionSAXException
- SAXExceptionIOException
- IOExceptionpublic static boolean isAncestor(Node ancestor, Node node)
ancestor
is an ancestor of node
ancestor
- ancestornode
- nodepublic static boolean hasAncestor(Node node, String ancestorName)
node
has an ancestor with name ancestorName
node
- nodeancestorName
- ancestorNamepublic static Node getAncestor(Node node, String ancestorName)
node
has an ancestor with name ancestorName
, return that ancestor. Else return
null
.node
- nodeancestorName
- ancestorNamepublic static Node getAncestor(Node node, String[] ancestorNames)
node
has an ancestor with one of the names in ancestorNames
, return the closest of these
ancestors. Else return null
.node
- nodeancestorNames
- ancestorNamespublic static Element getClosestAncestorWithAttribute(Node node, String ancestorName, String attributeName)
node
with name ancestorName
, and return the first
ancestor for which an attribute named attributeName
is present. Return null
if no such ancestor
exists.node
- nodeancestorName
- ancestorNameattributeName
- attributeNamepublic static String getAttributeFromClosestAncestorOfAnyKind(Node node, String attributeName)
node
's ancestors, looking for one with an attribute named attributeName
,
irrespective of the respective ancestorName
, and return the attribute's valuenode
- nodeattributeName
- attributeNamepublic static Node getHighestLevelAncestor(Node node, String ancestorName)
node
has ancestors with name ancestorName
, return the one closest to the root. If there is no
ancestor with that name, return null
.node
- nodeancestorName
- ancestorNamepublic static Element getNextSiblingElement(Element e)
e
which is an element, or null
if there is no such element.e
- epublic static Element getPreviousSiblingElement(Element e)
e
which is an element, or null
if there is no such element.e
- epublic static Element getNextSiblingElementByTagName(Element e, String name)
e
which is an element and has tag name name
, or null
if
there is no such element.e
- ename
- namepublic static Element getPreviousSiblingElementByTagName(Element e, String name)
e
which is an element and has tag name name
, or null
if
there is no such element.e
- ename
- namepublic static Element getFirstChildElement(Element e)
e
which is an element, or null
if there is no such element.e
- epublic static Element getLastChildElement(Element e)
e
which is an element, or null
if there is no such element.e
- epublic static Element getFirstElementByTagName(Node n, String name)
null
if there is no such element.n
- nname
- namepublic static Element getLastElementByTagName(Element e, String name)
null
if there is no such element.e
- ename
- namepublic static Element getPreviousOfItsKindIn(Element element, Element root)
element
preceding element
within the same subtree
under root
. Precondition: root
must be an ancestor of element
.element
- elementroot
- rootnull
if there is no such Element.public static Element getNextOfItsKindIn(Element element, Element root)
element
following element
within the same subtree
under root
. Precondition: root
must be an ancestor of element
.element
- elementroot
- rootnull
if there is no such Element.public static Element encloseNodesWithNewElement(Node first, Node last, String newElementName)
Algorithm:
a
childA
) and last (childZ
) of a
's children that need to be
enclosed by the new ElementchildA
and childZ
into the new Element.first
- firstlast
- lastnewElementName
- new element namepublic static String getPlainTextBelow(Node n)
n
- npublic static void insertAfter(Node newNode, Node refNode) throws DOMException
newNode
- new noderefNode
- ref nodeDOMException
- DOMExceptionpublic static void trimAllTextNodes(Node root)
root
- rootpublic static String serializeToString(Document doc)
doc
- docpublic static String document2String(Document document) throws MaryConfigurationException
document
- the document to be writtenMaryConfigurationException
- if the DOM document cannot be serializedpublic static void document2File(Document document, File file) throws MaryConfigurationException, IOException
document
- the document to be writtenfile
- where to write it.MaryConfigurationException
- if the DOM document cannot be serializedIOException
- if there is a problem accessing the filepublic static void document2Stream(Document document, OutputStream target) throws MaryConfigurationException
document
- the document to be writtentarget
- where to write it.MaryConfigurationException
- if the DOM document cannot be serializedpublic static org.w3c.dom.traversal.TreeWalker createTreeWalker(Document doc, Node root, String... tagNames)
public static org.w3c.dom.traversal.TreeWalker createTreeWalker(Node root, String... tagNames)
public static org.w3c.dom.traversal.NodeIterator createNodeIterator(Document doc, Node root, String... tagNames)
public static org.w3c.dom.traversal.NodeIterator createNodeIterator(Node root, String... tagNames)
public static void trimEmptyTextNodes(Node node)
node
- the node below which to trim.public static void compareNodes(Node expected, Node actual, boolean trimEmptyTextNodes) throws Exception
expected
- expectedactual
- actualtrimEmptyTextNodes
- whether or not to remove any empty text nodesException
- ExceptionCopyright © 2000–2016 DFKI GmbH. All rights reserved.