Package com.helger.xml
Class XMLHelper
- java.lang.Object
-
- com.helger.xml.XMLHelper
-
@Immutable public final class XMLHelper extends Object
This class contains multiple XML utility methods.- Author:
- Philip Helger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidappend(Node aParentNode, Iterable<?> aNodesToAppend)static Nodeappend(Node aParentNode, Object aChild)static com.helger.commons.functional.IPredicate<? super Element>filterElementWithNamespace()static com.helger.commons.functional.IPredicate<? super Element>filterElementWithNamespace(String sNamespaceURI)static com.helger.commons.functional.IPredicate<? super Element>filterElementWithNamespaceAndLocalName(String sNamespaceURI, String sLocalName)static com.helger.commons.functional.IPredicate<? super Element>filterElementWithoutNamespace()static com.helger.commons.functional.IPredicate<? super Element>filterElementWithTagName(String sTagName)static com.helger.commons.functional.IPredicate<? super Element>filterElementWithTagNameNoNS(String sTagName)static com.helger.commons.functional.IPredicate<? super Node>filterNodeIsElement()static voidforAllAttributes(Element aSrcNode, BiConsumer<? super String,? super String> aConsumer)static voidforAllAttributes(Element aSrcNode, Consumer<? super Attr> aConsumer)static com.helger.commons.collection.impl.ICommonsList<Attr>getAllAttributesAsList(Element aSrcNode)static com.helger.commons.collection.impl.ICommonsOrderedMap<String,String>getAllAttributesAsMap(Element aSrcNode)static StringgetAttributeValue(Element aElement, String sAttrName)The latest version of XercesJ 2.9 returns an empty string for non existing attributes.static StringgetAttributeValue(Element aElement, String sAttrName, String sDefault)The latest version of XercesJ 2.9 returns an empty string for non existing attributes.static StringgetAttributeValueNS(Element aElement, String sNamespaceURI, String sAttrName)The latest version of XercesJ 2.9 returns an empty string for non existing attributes.static StringgetAttributeValueNS(Element aElement, String sNamespaceURI, String sAttrName, String sDefault)The latest version of XercesJ 2.9 returns an empty string for non existing attributes.static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIterator(Node aStartNode)Get an iterator over all child elements.static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIterator(Node aStartNode, String sTagName)Get an iterator over all child elements that have the desired tag name (but potentially a namespace URI).static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIteratorNoNS(Node aStartNode)Get an iterator over all child elements that have no namespace.static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIteratorNoNS(Node aStartNode, String sTagName)Get an iterator over all child elements that have no namespace and the desired tag name.static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIteratorNS(Node aStartNode, String sNamespaceURI)static com.helger.commons.collection.iterate.IIterableIterator<Element>getChildElementIteratorNS(Node aStartNode, String sNamespaceURI, String sLocalName)static intgetDirectChildElementCount(Element aParent)static intgetDirectChildElementCount(Element aParent, String sTagName)static intgetDirectChildElementCountNoNS(Element aParent)static intgetDirectChildElementCountNoNS(Element aParent, String sTagName)static intgetDirectChildElementCountNS(Element aParent, String sNamespaceURI)static intgetDirectChildElementCountNS(Element aParent, String sNamespaceURI, String sLocalName)static ElementgetDocumentElement(Node aNode)static StringgetElementName(Node aNode)static ElementgetFirstChildElement(Node aStartNode)Get the first direct child element of the passed element.static ElementgetFirstChildElementOfName(Node aStartNode, String sTagName)Search all child nodes of the given for the first element that has the specified tag name.static ElementgetFirstChildElementOfName(Node aStartNode, String sNamespaceURI, String sLocalName)Search all child nodes of the given for the first element that has the specified tag name.static StringgetFirstChildText(Node aStartNode)Get the content of the first Text child element of the passed element.static intgetLength(NodeList aNL)static StringgetLocalNameOrName(Attr aAttr)static StringgetLocalNameOrTagName(Element aElement)static StringgetNamespaceURI(Node aNode)static DocumentgetOwnerDocument(Node aNode)Get the owner document of the passed node.static StringgetPathToNode(Node aNode)Shortcut forgetPathToNode(Node, String)using "/" as the separator.static StringgetPathToNode(Node aNode, String sSep)Get the path from root node to the passed node.static StringgetPathToNode2(Node aNode)Shortcut forgetPathToNode2(Node,String)using "/" as the separator.static StringgetPathToNode2(Node aNode, String sSep)Get the path from root node to the passed node.static StringgetPrefix(Element aElement)Get the namespace prefix of the passed element in a safe way.static QNamegetQName(Element aElement)Get the QName of the passed element.static QNamegetXMLNSAttrQName(String sNSPrefix)Get the full qualified attribute name to use for the given namespace prefix.static booleanhasChildElementNodes(Node aStartNode)Check if the passed node has at least one direct child element or not.static booleanhasNamespaceURI(Node aNode, String sNamespaceURI)static booleanhasNoNamespaceURI(Node aNode)static booleanisEmpty(NodeList aNL)static booleanisInlineNode(Node aNode)Check if the passed node is a text node.static voidremoveAllChildElements(Element aElement)Remove all child nodes of the given node.
-
-
-
Method Detail
-
getOwnerDocument
@Nullable public static Document getOwnerDocument(@Nullable Node aNode)
Get the owner document of the passed node. If the node itself is a document, only a cast is performed.- Parameters:
aNode- The node to get the document from. May benull.- Returns:
nullif the passed node wasnull.
-
getLocalNameOrTagName
@Nonnull public static String getLocalNameOrTagName(@Nonnull Element aElement)
-
hasNamespaceURI
public static boolean hasNamespaceURI(@Nullable Node aNode, @Nullable String sNamespaceURI)
-
isInlineNode
public static boolean isInlineNode(@Nullable Node aNode)
Check if the passed node is a text node. This includes all nodes derived fromText(Text and CData) orEntityReferencenodes.- Parameters:
aNode- The node to be checked.- Returns:
trueif the passed node is a text node,falseotherwise.
-
getLength
@Nonnegative public static int getLength(@Nullable NodeList aNL)
-
filterNodeIsElement
@Nonnull public static com.helger.commons.functional.IPredicate<? super Node> filterNodeIsElement()
-
filterElementWithNamespace
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespace()
-
filterElementWithoutNamespace
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithoutNamespace()
-
filterElementWithNamespace
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespace(@Nullable String sNamespaceURI)
-
filterElementWithNamespaceAndLocalName
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithNamespaceAndLocalName(@Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
-
filterElementWithTagName
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithTagName(@Nonnull @Nonempty String sTagName)
-
filterElementWithTagNameNoNS
@Nonnull public static com.helger.commons.functional.IPredicate<? super Element> filterElementWithTagNameNoNS(@Nonnull @Nonempty String sTagName)
-
getFirstChildElement
@Nullable public static Element getFirstChildElement(@Nullable Node aStartNode)
Get the first direct child element of the passed element.- Parameters:
aStartNode- The element to start searching. May benull.- Returns:
nullif the passed element does not have any direct child element.
-
hasChildElementNodes
public static boolean hasChildElementNodes(@Nullable Node aStartNode)
Check if the passed node has at least one direct child element or not.- Parameters:
aStartNode- The parent element to be searched. May benull.- Returns:
trueif the passed node has at least one child element,falseotherwise.
-
getFirstChildElementOfName
@Nullable public static Element getFirstChildElementOfName(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
Search all child nodes of the given for the first element that has the specified tag name.- Parameters:
aStartNode- The parent element to be searched. May benull.sTagName- The tag name to search.- Returns:
nullif the parent element has no such child element.
-
getFirstChildElementOfName
@Nullable public static Element getFirstChildElementOfName(@Nullable Node aStartNode, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
Search all child nodes of the given for the first element that has the specified tag name.- Parameters:
aStartNode- The parent element to be searched. May benull.sNamespaceURI- Namespace URI to search. May benull.sLocalName- The tag name to search.- Returns:
nullif the parent element has no such child element.
-
getDirectChildElementCount
@Nonnegative public static int getDirectChildElementCount(@Nullable Element aParent)
-
getDirectChildElementCountNoNS
@Nonnegative public static int getDirectChildElementCountNoNS(@Nullable Element aParent)
-
getDirectChildElementCount
@Nonnegative public static int getDirectChildElementCount(@Nullable Element aParent, @Nonnull @Nonempty String sTagName)
-
getDirectChildElementCountNoNS
@Nonnegative public static int getDirectChildElementCountNoNS(@Nullable Element aParent, @Nonnull @Nonempty String sTagName)
-
getDirectChildElementCountNS
@Nonnegative public static int getDirectChildElementCountNS(@Nullable Element aParent, @Nullable String sNamespaceURI)
-
getDirectChildElementCountNS
@Nonnegative public static int getDirectChildElementCountNS(@Nullable Element aParent, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
-
getChildElementIterator
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator(@Nullable Node aStartNode)
Get an iterator over all child elements.- Parameters:
aStartNode- the parent element- Returns:
- a non-null Iterator
-
getChildElementIteratorNoNS
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS(@Nullable Node aStartNode)
Get an iterator over all child elements that have no namespace.- Parameters:
aStartNode- the parent element- Returns:
- a non-null Iterator
-
getChildElementIteratorNoNS
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
Get an iterator over all child elements that have no namespace and the desired tag name.- Parameters:
aStartNode- the parent elementsTagName- the name of the tag that is desired- Returns:
- a non-null Iterator
- Throws:
IllegalArgumentException- if the passed tag name is null or empty
-
getChildElementIterator
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
Get an iterator over all child elements that have the desired tag name (but potentially a namespace URI).- Parameters:
aStartNode- the parent elementsTagName- the name of the tag that is desired- Returns:
- a non-null Iterator
- Throws:
IllegalArgumentException- if the passed tag name is null or empty
-
getChildElementIteratorNS
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNS(@Nullable Node aStartNode, @Nullable String sNamespaceURI)
-
getChildElementIteratorNS
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNS(@Nullable Node aStartNode, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
-
getPathToNode
@Nonnull public static String getPathToNode(@Nonnull Node aNode)
Shortcut forgetPathToNode(Node, String)using "/" as the separator.- Parameters:
aNode- The node to check.- Returns:
- A non-
nullpath.
-
getPathToNode
@Nonnull public static String getPathToNode(@Nonnull Node aNode, @Nonnull String sSep)
Get the path from root node to the passed node. This includes all nodes up to the document node!- Parameters:
aNode- The node to start. May not benull.sSep- The separator string to use. May not benull.- Returns:
- The path to the node.
-
getPathToNode2
@Nonnull public static String getPathToNode2(@Nonnull Node aNode)
Shortcut forgetPathToNode2(Node,String)using "/" as the separator.- Parameters:
aNode- The node to check.- Returns:
- A non-
nullpath.
-
getPathToNode2
@Nonnull public static String getPathToNode2(@Nonnull Node aNode, @Nonnull String sSep)
Get the path from root node to the passed node. This includes all nodes but excluding the document node!- Parameters:
aNode- The node to start. May not benull.sSep- The separator string to use. May not benull.- Returns:
- The path to the node.
-
removeAllChildElements
public static void removeAllChildElements(@Nonnull Element aElement)
Remove all child nodes of the given node.- Parameters:
aElement- The element whose children are to be removed.
-
getFirstChildText
@Nullable public static String getFirstChildText(@Nullable Node aStartNode)
Get the content of the first Text child element of the passed element.- Parameters:
aStartNode- the element to scan for a TextNode child- Returns:
nullif the element contains no text node as child
-
getAttributeValue
@Nullable public static String getAttributeValue(@Nonnull Element aElement, @Nonnull String sAttrName)
The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns null for non existing attributes.- Parameters:
aElement- the source element to get the attribute fromsAttrName- the name of the attribute to query- Returns:
nullif the attribute does not exists, the string value otherwise
-
getAttributeValue
@Nullable public static String getAttributeValue(@Nonnull Element aElement, @Nonnull String sAttrName, @Nullable String sDefault)
The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns a default value for non existing attributes.- Parameters:
aElement- the source element to get the attribute from. May not benull.sAttrName- the name of the attribute to query. May not benull.sDefault- the value to be returned if the attribute is not present.- Returns:
- the default value if the attribute does not exists, the string value otherwise
-
getAttributeValueNS
@Nullable public static String getAttributeValueNS(@Nonnull Element aElement, @Nullable String sNamespaceURI, @Nonnull String sAttrName)
The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns null for non existing attributes.- Parameters:
aElement- the source element to get the attribute fromsNamespaceURI- The namespace URI of the attribute to retrieve. May benull.sAttrName- the name of the attribute to query- Returns:
nullif the attribute does not exists, the string value otherwise
-
getAttributeValueNS
@Nullable public static String getAttributeValueNS(@Nonnull Element aElement, @Nullable String sNamespaceURI, @Nonnull String sAttrName, @Nullable String sDefault)
The latest version of XercesJ 2.9 returns an empty string for non existing attributes. To differentiate between empty attributes and non-existing attributes, this method returns a default value for non existing attributes.- Parameters:
aElement- the source element to get the attribute from. May not benull.sNamespaceURI- The namespace URI of the attribute to retrieve. May benull.sAttrName- the name of the attribute to query. May not benull.sDefault- the value to be returned if the attribute is not present.- Returns:
- the default value if the attribute does not exists, the string value otherwise
-
getAllAttributesAsList
@Nonnull @ReturnsMutableCopy public static com.helger.commons.collection.impl.ICommonsList<Attr> getAllAttributesAsList(@Nullable Element aSrcNode)
-
getAllAttributesAsMap
@Nonnull @ReturnsMutableCopy public static com.helger.commons.collection.impl.ICommonsOrderedMap<String,String> getAllAttributesAsMap(@Nullable Element aSrcNode)
-
forAllAttributes
public static void forAllAttributes(@Nullable Element aSrcNode, @Nonnull Consumer<? super Attr> aConsumer)
-
forAllAttributes
public static void forAllAttributes(@Nullable Element aSrcNode, @Nonnull BiConsumer<? super String,? super String> aConsumer)
-
getXMLNSAttrQName
@Nonnull public static QName getXMLNSAttrQName(@Nullable String sNSPrefix)
Get the full qualified attribute name to use for the given namespace prefix. The result will e.g. bexmlnsor{http://www.w3.org/2000/xmlns/}xmlns:foo.- Parameters:
sNSPrefix- The namespace prefix to build the attribute name from. May benullor empty.- Returns:
- If the namespace prefix is empty (if it equals
XMLConstants.DEFAULT_NS_PREFIXornull) than "xmlns" is returned, else "xmlns:prefix" is returned.
-
getPrefix
@Nonnull public static String getPrefix(@Nullable Element aElement)
Get the namespace prefix of the passed element in a safe way.- Parameters:
aElement- The element to be queried. May benull.- Returns:
XMLConstants.DEFAULT_NS_PREFIXor the provided prefix. Nevernull.- Since:
- 8.4.1
-
getQName
@Nonnull public static QName getQName(@Nonnull Element aElement)
Get the QName of the passed element. If the passed element has no namespace URI, only the tag name is used. Otherwise namespace URI, local name and prefix are used.- Parameters:
aElement- The element to be used. May not benull.- Returns:
- The created
QName. - Since:
- 8.4.1
-
-