Class DOMUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareEqualElements(Element node1, Element node2, boolean ignorePrefix) Override inherited behavior fromareEqualNodesandareEqualParentNodesto check on attributes.static booleanareEqualNodes(Node node1, Node node2, boolean ignorePrefix) Tests whether two nodes are equal.static booleanareEqualParentNodes(Node node1, Node node2, boolean ignorePrefix) Override inherited behavior fromareEqualNodesto support deep equal.static StringbuildToString(String tab, Node node) Build aNodeinstance into a string representation.static LocatorBeangetLocator(Element element, String locatorPrefix) Get aLocatorBeanfrom aElement.static org.slf4j.Loggerstatic DocumentsaxParseDocument(InputStream is, String systemId, String locatorPrefix) Use SAX to parse an XML document with line numbers.static ElementCreate a W3C DOM Element instance.static ElementConvert a QName instance into a W3C DOM Element instance.static NodeConvert a JAXBElement instance into a W3C DOM Node instance.static NodeConvert a JAXBElement instance into a W3C DOM Node instance.static NodeCreate a W3C DOM Node instance.static NodeConvert a QName instance into a W3C DOM Node instance.static StringtransformToString(Node node) Transform aNodeinstance into a formatted XML string.
-
Method Details
-
getLogger
public static org.slf4j.Logger getLogger() -
toElement
Convert a QName instance into a W3C DOM Element instance.- Parameters:
qname- The instance to be converted.- Returns:
- A W3C DOM Element representaion.
-
toElement
Create a W3C DOM Element instance.- Parameters:
namespaceURI- The node's namespace URI.localPart- The node's local part (name).prefix- The namespace prefix for this node.value- The elements's value.- Returns:
- A W3C DOM Node representaion.
-
toNode
Convert a QName instance into a W3C DOM Node instance.- Parameters:
qname- The instance to be converted.- Returns:
- A W3C DOM Node representaion.
-
toNode
Convert a JAXBElement instance into a W3C DOM Node instance.- Parameters:
jaxbElement- The instance to be converted.prefix- A flag to assign the prefix.- Returns:
- A W3C DOM Node representaion.
-
toNode
Convert a JAXBElement instance into a W3C DOM Node instance. Ignore the prefix.- Parameters:
jaxbElement- The instance to be converted.- Returns:
- A W3C DOM Node representaion.
-
toNode
Create a W3C DOM Node instance.- Parameters:
namespaceURI- The node's namespace URI.localPart- The node's local part (name).prefix- The namespace prefix for this node.value- The node's value.- Returns:
- A W3C DOM Node representaion.
-
areEqualNodes
Tests whether two nodes are equal.
This method tests for equality of nodes, not sameness (i.e., whether the two nodes are references to the same object), which can be tested with
Node.isSameNode. All nodes that are the same will also be equal, though the reverse may not be true.Two nodes are equal if and only if the following conditions are satisfied:
- The two nodes are of the same type.
- The following string attributes are equal (they are both
null, or they have the same length and are character for character identical):namespaceURIlocalName- Optional:
prefix: The prefix xmlns is used only to declare namespace bindings.nodeName: Concatenation ofprefix,":"andlocalName.
nodeValuebaseURI
- The
attributesNamedNodeMapsare equal (they are bothnull, or they have the same map size and for each node that exists in one map there is a node that exists in the other map and is equal, although not necessarily at the same index). - The
childNodesNodeListsare equal (they are bothnull, or they have the same length and contain equal nodes at the same index).
Note: Normalization can affect equality; to avoid this, nodes should be normalized before being compared.
For two
DocumentTypenodes to be equal, the following conditions must also be satisfied:- The following string attributes are equal:
publicIdsystemIdinternalSubset
- The
entitiesNamedNodeMapsare equal. - The
notationsNamedNodeMapsare equal.
On the other hand, the following do not affect equality:
- The
ownerDocumentattribute. - The
specifiedattribute forAttrnodes. - The
isWhitespaceInElementContentattribute forTextnodes. - As well as any user data or event listeners registered on the nodes.
Note: This method is derived from
com.sun.org.apache.xerces.internal.dom.NodeImpl.- Parameters:
node1- The first node to compare equality with.node2- The second node to compare equality with.ignorePrefix- When true, ignore theprefixrelated values.- Returns:
- If the nodes are equal,
true; otherwisefalse.
-
areEqualParentNodes
Override inherited behavior from
areEqualNodesto support deep equal.Reference: DOM Level 3 Working Draft - Experimental.
Note: This method is derived from
com.sun.org.apache.xerces.internal.dom.ParentNode.- Parameters:
node1- The first node to compare equality with.node2- The second node to compare equality with.ignorePrefix- When true, ignore theprefixrelated values.- Returns:
- If the nodes (and any subtrees) are equal,
true; otherwisefalse.
-
areEqualElements
Override inherited behavior from
areEqualNodesandareEqualParentNodesto check on attributes.Reference: DOM Level 3 Working Draft - Experimental.
Note: This method is derived from
com.sun.org.apache.xerces.internal.dom.ElementImpl- Parameters:
node1- The first element to compare equality with.node2- The second element to compare equality with.ignorePrefix- When true, ignore theprefixrelated values.- Returns:
- If the elements (and any subtrees) are equal,
true; otherwisefalse.
-
saxParseDocument
public static Document saxParseDocument(InputStream is, String systemId, String locatorPrefix) throws IOException, SAXException Use SAX to parse an XML document with line numbers.- Parameters:
is- An input stream for the source XML file.systemId- The systemId which is needed for resolving relative URIs.locatorPrefix- Custom prefix for publicId, systemId, lineNumber and columnNumber.- Returns:
- A W3C Document instance enhanced with line numbers per element.
- Throws:
IOException- When SAX cannot parse the input stream.SAXException- WhenSAXParserFactorycannot create a new SAX parser.- See Also:
-
getLocator
Get aLocatorBeanfrom aElement.- Parameters:
element- A DOM element with optional locator attributes.locatorPrefix- Custom prefix for publicId, systemId, lineNumber and columnNumber.- Returns:
- A
Locatorwith optional values or null.
-
transformToString
Transform aNodeinstance into a formatted XML string.- Parameters:
node- TheNodeinstance to be transformed.- Returns:
- A
Nodeinstance into a formatted XML string. - Throws:
TransformerException- When theNodeinstance cannot be transformed.
-
buildToString
Build aNodeinstance into a string representation.
-