public class DOMUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
NS_URI_XMLNS
The namespaceURI represented by the prefix
xmlns . |
static java.lang.String |
WSDL_NS |
static java.lang.String |
WSDL_ROOT_ELEMENT |
static java.lang.String |
XSD_NS |
static java.lang.String |
XSD_ROOT_ELEMENT |
Constructor and Description |
---|
DOMUtils() |
Modifier and Type | Method and Description |
---|---|
static org.w3c.dom.Node |
cloneNode(org.w3c.dom.Document document,
org.w3c.dom.Node sourceNode)
Deep clone, but don't fry, the given node in the context of the given document.
|
static void |
copyNSContext(org.w3c.dom.Element source,
org.w3c.dom.Element dest) |
static int |
countKids(org.w3c.dom.Element elem,
short nodeType)
Count number of children of a certain type of the given element.
|
static byte[] |
domToBytes(org.w3c.dom.Element element) |
static java.lang.String |
domToString(org.w3c.dom.Node node)
Convert a DOM node to a stringified XML representation.
|
static java.lang.String |
domToStringLevel2(org.w3c.dom.Node node)
Convert a DOM node to a stringified XML representation.
|
static org.w3c.dom.Element |
findChildByName(org.w3c.dom.Element parent,
javax.xml.namespace.QName name) |
static org.w3c.dom.Element |
findChildByName(org.w3c.dom.Element parent,
javax.xml.namespace.QName name,
boolean recurse) |
static org.w3c.dom.Node |
findChildByType(org.w3c.dom.Element elem,
int type) |
static org.w3c.dom.Element |
findChildElementWithAttribute(org.w3c.dom.Element elem,
java.lang.String attrName,
java.lang.String attrValue)
Return the first child element of the given element which has the given
attribute with the given value.
|
static java.util.List<org.w3c.dom.Element> |
findChildrenByName(org.w3c.dom.Element parent,
javax.xml.namespace.QName name) |
static java.lang.String |
getAttribute(org.w3c.dom.Element el,
java.lang.String attrName)
Returns the value of an attribute of an element.
|
static java.lang.String |
getAttributeNS(org.w3c.dom.Element el,
java.lang.String namespaceURI,
java.lang.String localPart)
Returns the value of an attribute of an element.
|
static java.lang.String |
getChildCharacterData(org.w3c.dom.Element parentEl)
Concat all the text and cdata node children of this elem and return the
resulting text.
|
static org.w3c.dom.Document |
getDocument(org.w3c.dom.Node contextNode) |
static org.w3c.dom.Element |
getElementByID(org.w3c.dom.Element el,
java.lang.String id)
DOCUMENTME
|
static org.w3c.dom.Element |
getElementContent(org.w3c.dom.Node node) |
static org.w3c.dom.Element |
getFirstChildElement(org.w3c.dom.Element elem)
Return the first child element of the given element.
|
static org.w3c.dom.Element |
getFirstChildElement(org.w3c.dom.Node node) |
static java.util.Map<java.lang.String,java.lang.String> |
getMyNamespaces(org.w3c.dom.Element el) |
static NSContext |
getMyNSContext(org.w3c.dom.Element el)
Construct a
NSContext instance for the supplied element. |
static java.lang.String |
getNamespaceURIFromPrefix(org.w3c.dom.Node context,
java.lang.String prefix)
Given a prefix and a node, return the namespace URI that the prefix has
been associated with.
|
static org.w3c.dom.Element |
getNextSiblingElement(org.w3c.dom.Element elem)
Return the next sibling element of the given element.
|
static javax.xml.namespace.QName |
getNodeQName(org.w3c.dom.Node el) |
static javax.xml.namespace.QName |
getNodeQName(java.lang.String qualifiedName) |
static java.lang.String |
getNSPrefixFromNSAttr(org.w3c.dom.Attr a)
Fetch the non-null namespace prefix from a
Attr that declares
a namespace. |
static java.util.Map<java.lang.String,java.lang.String> |
getParentNamespaces(org.w3c.dom.Element el)
This method traverses the DOM and grabs namespace declarations
on parent elements with the intent of preserving them for children.
|
static javax.xml.namespace.QName |
getQualifiedAttributeValue(org.w3c.dom.Element el,
java.lang.String attrName)
DOCUMENTME
|
static java.lang.String |
getQualifiedName(javax.xml.namespace.QName qName) |
static java.lang.String |
getTextContent(org.w3c.dom.Node node) |
static void |
injectNamespaces(org.w3c.dom.Element domElement,
NSContext nscontext) |
static void |
injectNamespacesWithAllPrefixes(org.w3c.dom.Element domElement,
NSContext nscontext)
Adds namespaces including all prefixes.
|
static boolean |
isEmptyElement(org.w3c.dom.Element el)
Check that an element is empty, i.e., it contains no non-whitespace text or
elements as children.
|
static boolean |
isNSAttribute(org.w3c.dom.Attr a)
Test whether an attribute contains a namespace declaration.
|
static boolean |
isWsdlDocument(org.w3c.dom.Document d)
Perform a naive check to see if a document is a WSDL document
based on the root element name and namespace URI.
|
static boolean |
isXmlSchemaDocument(org.w3c.dom.Document d)
Perform a naive check to see if a document is an XML schema document
based on the root element name and namespace URI.
|
static org.w3c.dom.Document |
newDocument() |
static void |
pancakeNamespaces(org.w3c.dom.Element el) |
static org.w3c.dom.Document |
parse(org.xml.sax.InputSource inputSource)
Parse an XML document located using an
InputSource using the
pooled document builder. |
static org.w3c.dom.Document |
parse(java.io.InputStream inputStream)
Parse an XML stream using the pooled document builder.
|
static org.w3c.dom.Document |
parse(javax.xml.stream.XMLStreamReader reader) |
static java.lang.String |
prettyPrint(org.w3c.dom.Element e)
Deprecated.
relies on XMLSerializer which is a deprecated Xerces class, use domToString instead
|
static void |
removeAttributes(org.w3c.dom.Element target,
boolean flag)
Drop the attributes from an element, except possibly an
xmlns
attribute that declares its namespace. |
static void |
removeChildren(org.w3c.dom.Node target)
Remove the child nodes under another node.
|
static void |
serialize(org.w3c.dom.Element elmt,
java.io.OutputStream ostr) |
static org.w3c.dom.Document |
sourceToDOM(javax.xml.transform.Source inputSource)
Parse an XML document located using an
InputSource using the
pooled document builder. |
static org.w3c.dom.Element |
stringToDOM(byte[] bytes) |
static org.w3c.dom.Element |
stringToDOM(java.lang.String s)
Parse a String into a DOM.
|
static org.w3c.dom.Document |
toDocumentFromStream(javax.xml.transform.stream.StreamSource source) |
static org.w3c.dom.Document |
toDOMDocument(org.w3c.dom.Node node) |
static java.util.List<org.w3c.dom.Node> |
toList(java.lang.Object nl)
Somewhat eases the pain of dealing with both Lists and Nodelists by converting either
passed as parameter to a List.
|
public static final java.lang.String NS_URI_XMLNS
xmlns
.public static final java.lang.String WSDL_NS
public static final java.lang.String WSDL_ROOT_ELEMENT
public static final java.lang.String XSD_NS
public static final java.lang.String XSD_ROOT_ELEMENT
public static java.lang.String getAttribute(org.w3c.dom.Element el, java.lang.String attrName)
el
- Element whose attrib is looked forattrName
- name of attribute to look forpublic static java.lang.String prettyPrint(org.w3c.dom.Element e) throws java.io.IOException
java.io.IOException
public static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Node node)
public static java.lang.String getAttributeNS(org.w3c.dom.Element el, java.lang.String namespaceURI, java.lang.String localPart)
el
- Element whose attrib is looked fornamespaceURI
- namespace URI of attribute to look forlocalPart
- local part of attribute to look forpublic static java.lang.String getChildCharacterData(org.w3c.dom.Element parentEl)
parentEl
- the element whose cdata/text node values are to be
combined.public static org.w3c.dom.Element getElementByID(org.w3c.dom.Element el, java.lang.String id)
el
- DOCUMENTMEid
- DOCUMENTMEpublic static org.w3c.dom.Element getFirstChildElement(org.w3c.dom.Element elem)
elem
- Element whose child is to be returnedpublic static java.lang.String getNamespaceURIFromPrefix(org.w3c.dom.Node context, java.lang.String prefix)
context
- the starting node (looks up recursively from here)prefix
- the prefix to find an xmlns:prefix=uri forpublic static org.w3c.dom.Element getNextSiblingElement(org.w3c.dom.Element elem)
elem
- Element whose sibling element is to be returnedpublic static javax.xml.namespace.QName getQualifiedAttributeValue(org.w3c.dom.Element el, java.lang.String attrName) throws java.lang.IllegalArgumentException
el
- DOCUMENTMEattrName
- DOCUMENTMEjava.lang.IllegalArgumentException
- DOCUMENTMEpublic static int countKids(org.w3c.dom.Element elem, short nodeType)
elem
- the element whose kids are to be countednodeType
- DOCUMENTMEpublic static java.util.Map<java.lang.String,java.lang.String> getParentNamespaces(org.w3c.dom.Element el)
Element.getAttribute(java.lang.String)
is not desirable in this case, as it does not respect namespace prefix
bindings that may affect attribute values. (Namespaces in DOM are
uncategorically a mess, especially in the context of XML Schema.)el
- the starting elementMap
containing prefix bindings.public static NSContext getMyNSContext(org.w3c.dom.Element el)
NSContext
instance for the supplied element.el
- the Element
to gather the namespace context forNSContext
public static java.util.Map<java.lang.String,java.lang.String> getMyNamespaces(org.w3c.dom.Element el)
public static boolean isNSAttribute(org.w3c.dom.Attr a)
a
- an Attr
to test.true
if the Attr
is a namespace declarationpublic static java.lang.String getNSPrefixFromNSAttr(org.w3c.dom.Attr a)
Attr
that declares
a namespace. (The DOM APIs will return null
for a non-prefixed
declaration.a
- the Attr
with the declaration (must be non-null
""
if none was
declared, e.g., xmlns="foo"
.public static java.lang.String domToString(org.w3c.dom.Node node)
public static void serialize(org.w3c.dom.Element elmt, java.io.OutputStream ostr)
public static java.lang.String domToStringLevel2(org.w3c.dom.Node node)
public static org.w3c.dom.Element findChildElementWithAttribute(org.w3c.dom.Element elem, java.lang.String attrName, java.lang.String attrValue)
elem
- the element whose children are to be searchedattrName
- the attrib that must be presentattrValue
- the desired value of the attributepublic static org.w3c.dom.Element stringToDOM(java.lang.String s) throws org.xml.sax.SAXException, java.io.IOException
s
- DOCUMENTMEorg.xml.sax.SAXException
- DOCUMENTMEjava.io.IOException
- DOCUMENTMEpublic static boolean isWsdlDocument(org.w3c.dom.Document d)
d
- the Document
to checktrue
if the root element appears correctpublic static boolean isXmlSchemaDocument(org.w3c.dom.Document d)
d
- the Document
to checktrue
if the root element appears correctpublic static void pancakeNamespaces(org.w3c.dom.Element el)
el
- public static org.w3c.dom.Document newDocument()
public static org.w3c.dom.Document parse(java.io.InputStream inputStream) throws org.xml.sax.SAXException, java.io.IOException
inputStream
- input streamorg.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document parse(org.xml.sax.InputSource inputSource) throws org.xml.sax.SAXException, java.io.IOException
InputSource
using the
pooled document builder.org.xml.sax.SAXException
java.io.IOException
public static org.w3c.dom.Document sourceToDOM(javax.xml.transform.Source inputSource) throws java.io.IOException
InputSource
using the
pooled document builder.java.io.IOException
public static boolean isEmptyElement(org.w3c.dom.Element el)
el
- the elementtrue
if the element is empty, false
if not.public static javax.xml.namespace.QName getNodeQName(org.w3c.dom.Node el)
public static javax.xml.namespace.QName getNodeQName(java.lang.String qualifiedName)
public static void removeChildren(org.w3c.dom.Node target)
target
- the Node
to remove the children from.public static void removeAttributes(org.w3c.dom.Element target, boolean flag)
xmlns
attribute that declares its namespace.target
- the element whose attributes will be removed.flag
- preserve namespace declarationpublic static org.w3c.dom.Element findChildByName(org.w3c.dom.Element parent, javax.xml.namespace.QName name)
public static org.w3c.dom.Element findChildByName(org.w3c.dom.Element parent, javax.xml.namespace.QName name, boolean recurse)
public static org.w3c.dom.Node findChildByType(org.w3c.dom.Element elem, int type)
public static java.lang.String getTextContent(org.w3c.dom.Node node)
public static org.w3c.dom.Element getElementContent(org.w3c.dom.Node node)
public static void injectNamespaces(org.w3c.dom.Element domElement, NSContext nscontext)
public static void injectNamespacesWithAllPrefixes(org.w3c.dom.Element domElement, NSContext nscontext)
domElement
- An element wi which the namespace attributes should be added.nscontext
- A namespace context.public static void copyNSContext(org.w3c.dom.Element source, org.w3c.dom.Element dest)
public static org.w3c.dom.Document toDOMDocument(org.w3c.dom.Node node) throws javax.xml.transform.TransformerException
javax.xml.transform.TransformerException
public static org.w3c.dom.Document toDocumentFromStream(javax.xml.transform.stream.StreamSource source) throws java.io.IOException, org.xml.sax.SAXException
java.io.IOException
org.xml.sax.SAXException
public static org.w3c.dom.Document parse(javax.xml.stream.XMLStreamReader reader) throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
public static java.util.List<org.w3c.dom.Element> findChildrenByName(org.w3c.dom.Element parent, javax.xml.namespace.QName name)
public static java.util.List<org.w3c.dom.Node> toList(java.lang.Object nl)
nl
- a NodeList or a Listpublic static org.w3c.dom.Document getDocument(org.w3c.dom.Node contextNode)
public static java.lang.String getQualifiedName(javax.xml.namespace.QName qName)
public static org.w3c.dom.Node cloneNode(org.w3c.dom.Document document, org.w3c.dom.Node sourceNode)
adoptNode
or adoptNode
methods,
in that it doesn't assume that the given node has a parent or a owner document.document
- sourceNode
- public static org.w3c.dom.Element stringToDOM(byte[] bytes) throws org.xml.sax.SAXException, java.io.IOException
org.xml.sax.SAXException
java.io.IOException
public static byte[] domToBytes(org.w3c.dom.Element element)