public final class XMLHelper extends Object
Modifier and Type | Field and Description |
---|---|
static String |
LIST_DELIMITERS
A string which contains the valid delimiters for the XML Schema 'list' type.
|
Modifier and Type | Method and Description |
---|---|
static void |
adoptElement(Element adoptee,
Document adopter)
Adopts an element into a document if the child is not already in the document.
|
static void |
appendChildElement(Element parentElement,
Element childElement)
Appends the child Element to the parent Element, adopting the child Element into the parent's Document if needed.
|
static void |
appendNamespaceDeclaration(Element domElement,
String namespaceURI,
String prefix)
Adds a namespace declaration (xmlns:) attribute to the given element.
|
static void |
appendTextContent(Element domElement,
String textContent)
Creates a text node with the given content and appends it as child to the given element.
|
static Attr |
constructAttribute(Document owningDocument,
QName attributeName)
Constructs an attribute owned by the given document with the given name.
|
static Attr |
constructAttribute(Document document,
String namespaceURI,
String localName,
String prefix)
Constructs an attribute owned by the given document with the given name.
|
static Element |
constructElement(Document document,
QName elementName)
Constructs an element, rooted in the given document, with the given name.
|
static Element |
constructElement(Document document,
String namespaceURI,
String localName,
String prefix)
Constructs an element, rooted in the given document, with the given information.
|
static QName |
constructQName(String qname,
Element owningElement)
Constructs a QName from a string (attribute element content) value.
|
static QName |
constructQName(String namespaceURI,
String localName,
String prefix)
Constructs a QName.
|
static QName |
constructQName(String qname,
XMLObject owningObject)
Constructs a QName from a string (attribute or element content) value.
|
static long |
durationToLong(String duration)
Converts a lexical duration, as defined by XML Schema 1.0, into milliseconds.
|
static Boolean |
getAttributeValueAsBoolean(Attr attribute)
Parses the attribute's value.
|
static List<String> |
getAttributeValueAsList(Attr attribute)
Gets the value of a list-type attribute as a list.
|
static QName |
getAttributeValueAsQName(Attr attribute)
Constructs a QName from an attributes value.
|
static Map<QName,List<Element>> |
getChildElements(Element root)
Gets the child elements of the given element in a single iteration.
|
static List<Element> |
getChildElementsByTagName(Element root,
String localName)
Gets the child nodes with the given local tag name.
|
static List<Element> |
getChildElementsByTagNameNS(Element root,
String namespaceURI,
String localName)
Gets the child nodes with the given namespace qualified tag name.
|
static DatatypeFactory |
getDataTypeFactory()
Gets a static instance of a JAXP DatatypeFactory.
|
static Element |
getElementAncestor(Node currentNode)
Gets the ancestor element node to the given node.
|
static List<String> |
getElementContentAsList(Element element)
Gets the value of a list-type element as a list.
|
static QName |
getElementContentAsQName(Element element)
Constructs a QName from an element's adjacent Text child nodes.
|
static Element |
getFirstChildElement(Node n)
Gets the first child Element of the node, skipping any Text nodes such as whitespace.
|
static Attr |
getIdAttribute(Element domElement)
Gets the ID attribute of a DOM element.
|
static Locale |
getLanguage(Element element)
Gets the lcoale currently active for the element.
|
static DOMImplementationLS |
getLSDOMImpl(Node node)
Get the DOM Level 3 Load/Save
DOMImplementationLS for the given node. |
static LSSerializer |
getLSSerializer(DOMImplementationLS domImplLS,
Map<String,Object> serializerParams)
Obtain a the DOM, level 3, Load/Save serializer
LSSerializer instance from the
given DOMImplementationLS instance. |
static Element |
getNextSiblingElement(Node n)
Gets the next sibling Element of the node, skipping any Text nodes such as whitespace.
|
static QName |
getNodeQName(Node domNode)
Gets the QName for the given DOM node.
|
static QName |
getXSIType(Element e)
Gets the XSI type for a given element if it has one.
|
static boolean |
hasXSIType(Element e)
Checks if the given element has an xsi:type defined for it.
|
static boolean |
isElementNamed(Element e,
String ns,
String localName)
Shortcut for checking a DOM element node's namespace and local name.
|
static String |
longToDuration(long duration)
Converts a duration in milliseconds to a lexical duration, as defined by XML Schema 1.0.
|
static String |
lookupNamespaceURI(Element startingElement,
Element stopingElement,
String prefix)
Looks up the namespace URI associated with the given prefix starting at the given element.
|
static String |
lookupNamespaceURI(Element startingElement,
String prefix)
Looks up the namespace URI associated with the given prefix starting at the given element.
|
static String |
lookupPrefix(Element startingElement,
Element stopingElement,
String namespaceURI)
Looks up the namespace prefix associated with the given URI starting at the given element.
|
static String |
lookupPrefix(Element startingElement,
String namespaceURI)
Looks up the namespace prefix associated with the given URI starting at the given element.
|
static void |
marshallAttribute(QName attributeName,
List<String> attributeValues,
Element domElement,
boolean isIDAttribute)
Marshall an attribute name and value to a DOM Element.
|
static void |
marshallAttribute(QName attributeName,
String attributeValue,
Element domElement,
boolean isIDAttribute)
Marshall an attribute name and value to a DOM Element.
|
static void |
marshallAttributeMap(AttributeMap attributeMap,
Element domElement)
Marshall the attributes represented by the indicated AttributeMap into the indicated DOM Element.
|
static String |
nodeToString(Node node)
Converts a Node into a String using the DOM, level 3, Load/Save serializer.
|
static String |
prettyPrintXML(Node node)
Pretty prints the XML node.
|
static String |
qnameToContentString(QName qname)
Converts a QName into a string that can be used for attribute values or element content.
|
static void |
rootNamespaces(Element domElement)
Ensures that all the visibly used namespaces referenced by the given Element or its descendants are declared by
the given Element or one of its descendants.
|
static void |
unmarshallToAttributeMap(AttributeMap attributeMap,
Attr attribute)
Unmarshall a DOM Attr to an AttributeMap.
|
static void |
writeNode(Node node,
OutputStream output)
Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer.
|
static void |
writeNode(Node node,
OutputStream output,
Map<String,Object> serializerParams)
Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer.
|
static void |
writeNode(Node node,
Writer output)
Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer.
|
static void |
writeNode(Node node,
Writer output,
Map<String,Object> serializerParams)
Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer.
|
public static final String LIST_DELIMITERS
public static DatatypeFactory getDataTypeFactory()
public static boolean hasXSIType(Element e)
e
- the DOM elementpublic static QName getXSIType(Element e)
e
- the elementpublic static Attr getIdAttribute(Element domElement)
domElement
- the DOM elementpublic static QName getNodeQName(Node domNode)
domNode
- the DOM nodepublic static Locale getLanguage(Element element)
element
- element to retrieve local information forpublic static Attr constructAttribute(Document owningDocument, QName attributeName)
owningDocument
- the owning documentattributeName
- the name of that attributepublic static Attr constructAttribute(Document document, String namespaceURI, String localName, String prefix)
document
- the owning documentnamespaceURI
- the URI for the namespace the attribute is inlocalName
- the local nameprefix
- the prefix of the namespace that attribute is inpublic static QName getAttributeValueAsQName(Attr attribute)
attribute
- the attribute with a QName valuepublic static Boolean getAttributeValueAsBoolean(Attr attribute)
attribute
- attribute whose value will be converted to a booleanpublic static List<String> getAttributeValueAsList(Attr attribute)
attribute
- attribute whose value will be turned into a listpublic static void marshallAttribute(QName attributeName, String attributeValue, Element domElement, boolean isIDAttribute)
attributeName
- the attribute name in QName formattributeValue
- the attribute valuedomElement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed
attributepublic static void marshallAttribute(QName attributeName, List<String> attributeValues, Element domElement, boolean isIDAttribute)
attributeName
- the attribute name in QName formattributeValues
- the attribute valuesdomElement
- the target element to which to marshallisIDAttribute
- flag indicating whether the attribute being marshalled should be handled as an ID-typed
attributepublic static void marshallAttributeMap(AttributeMap attributeMap, Element domElement)
attributeMap
- the AttributeMapdomElement
- the target Elementpublic static void unmarshallToAttributeMap(AttributeMap attributeMap, Attr attribute)
attributeMap
- the target AttributeMapattribute
- the target DOM Attrpublic static QName getElementContentAsQName(Element element)
element
- the element with a QName valuepublic static List<String> getElementContentAsList(Element element)
element
- element whose value will be turned into a listpublic static QName constructQName(String namespaceURI, String localName, String prefix)
namespaceURI
- the namespace of the QNamelocalName
- the local name of the QNameprefix
- the prefix of the QName, may be nullpublic static QName constructQName(String qname, XMLObject owningObject)
qname
- the QName stringowningObject
- XMLObject, with cached DOM, owning the QNamepublic static QName constructQName(String qname, Element owningElement)
qname
- the QName stringowningElement
- parent DOM element of the Node which contains the QName valuepublic static Element constructElement(Document document, QName elementName)
document
- the document containing the elementelementName
- the name of the element, must contain a local name, may contain a namespace URI and prefixpublic static Element constructElement(Document document, String namespaceURI, String localName, String prefix)
document
- the document containing the elementnamespaceURI
- the URI of the namespace the element is inlocalName
- the element's local nameprefix
- the prefix of the namespace the element is inpublic static void appendChildElement(Element parentElement, Element childElement)
parentElement
- the parent ElementchildElement
- the child Elementpublic static void adoptElement(Element adoptee, Document adopter)
adoptee
- the element to be adoptedadopter
- the document into which the element is adoptedpublic static void appendTextContent(Element domElement, String textContent)
domElement
- the element to recieve the text nodetextContent
- the content for the text nodepublic static void appendNamespaceDeclaration(Element domElement, String namespaceURI, String prefix)
domElement
- the element to add the attribute tonamespaceURI
- the URI of the namespaceprefix
- the prefix for the namespacepublic static String lookupNamespaceURI(Element startingElement, String prefix)
Node.lookupNamespaceURI(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.startingElement
- the starting elementprefix
- the prefix to look uppublic static String lookupNamespaceURI(Element startingElement, Element stopingElement, String prefix)
Node.lookupNamespaceURI(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.startingElement
- the starting elementstopingElement
- the ancestor of the starting element that serves as the upper-bound, inclusive, for the
searchprefix
- the prefix to look uppublic static String lookupPrefix(Element startingElement, String namespaceURI)
Node.lookupPrefix(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.startingElement
- the starting elementnamespaceURI
- the uri to look uppublic static String lookupPrefix(Element startingElement, Element stopingElement, String namespaceURI)
Node.lookupPrefix(java.lang.String)
in that it only those namespaces declared by an xmlns
attribute are inspected. The Node method also checks the namespace a particular node was created in by way of a
call like Document.createElementNS(java.lang.String, java.lang.String)
even if the resulting element
doesn't have an namespace delcaration attribute.startingElement
- the starting elementstopingElement
- the ancestor of the starting element that serves as the upper-bound, inclusive, for the
searchnamespaceURI
- the uri to look uppublic static List<Element> getChildElementsByTagNameNS(Element root, String namespaceURI, String localName)
getChildElements(Element)
.root
- element to retrieve the children fromnamespaceURI
- namespace URI of the child elementlocalName
- local, tag, name of the child elementpublic static List<Element> getChildElementsByTagName(Element root, String localName)
getChildElements(Element)
.root
- element to retrieve the children fromlocalName
- local, tag, name of the child elementpublic static Map<QName,List<Element>> getChildElements(Element root)
root
- element to get the child elements ofpublic static Element getElementAncestor(Node currentNode)
currentNode
- the node to retrive the ancestor forpublic static String nodeToString(Node node)
node
- the node to be written to a stringpublic static String prettyPrintXML(Node node)
node
- xml node to printpublic static void writeNode(Node node, Writer output)
node
- the node to write outoutput
- the writer to write the XML topublic static void writeNode(Node node, Writer output, Map<String,Object> serializerParams)
node
- the node to write outoutput
- the writer to write the XML toserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.public static void writeNode(Node node, OutputStream output)
node
- the node to write outoutput
- the output stream to write the XML topublic static void writeNode(Node node, OutputStream output, Map<String,Object> serializerParams)
node
- the node to write outoutput
- the output stream to write the XML toserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.public static LSSerializer getLSSerializer(DOMImplementationLS domImplLS, Map<String,Object> serializerParams)
LSSerializer
instance from the
given DOMImplementationLS
instance.
The serializer instance will be configured with the parameters passed as the serializerParams
argument. It will also be configured with an LSSerializerFilter
that shows all nodes to the filter,
and accepts all nodes shown.
domImplLS
- the DOM Level 3 Load/Save implementation to useserializerParams
- parameters to pass to the DOMConfiguration
of the serializer
instance, obtained via LSSerializer.getDomConfig()
. May be null.public static DOMImplementationLS getLSDOMImpl(Node node)
DOMImplementationLS
for the given node.node
- the node to evaluatepublic static String qnameToContentString(QName qname)
qname
- the QName to convert to a stringpublic static void rootNamespaces(Element domElement) throws XMLParserException
domElement
- the element to act as the root of the namespace declarationsXMLParserException
- thrown if a namespace prefix is encountered that can't be resolved to a namespace URIpublic static boolean isElementNamed(Element e, String ns, String localName)
e
- An element to compare againstns
- An XML namespace to comparelocalName
- A local name to comparepublic static Element getFirstChildElement(Node n)
n
- The parent in which to search for childrenpublic static Element getNextSiblingElement(Node n)
n
- The sibling to start withpublic static long durationToLong(String duration)
duration
- lexical duration representationpublic static String longToDuration(long duration)
duration
- the durationCopyright © 2018 JBoss by Red Hat. All rights reserved.