@Immutable public final class XMLHelper extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
XMLHelper.PathToNodeBuilder
Builder class for the different possibilities to get the path of a node
|
| Modifier and Type | Method and Description |
|---|---|
static void |
append(Node aParentNode,
Iterable<?> aNodesToAppend) |
static Node |
append(Node aParentNode,
Object aChild) |
static Predicate<? super Element> |
filterElementWithNamespace() |
static Predicate<? super Element> |
filterElementWithNamespace(String sNamespaceURI) |
static Predicate<? super Element> |
filterElementWithNamespaceAndLocalName(String sNamespaceURI,
String sLocalName) |
static Predicate<? super Element> |
filterElementWithoutNamespace() |
static Predicate<? super Element> |
filterElementWithTagName(String sTagName) |
static Predicate<? super Element> |
filterElementWithTagNameNoNS(String sTagName) |
static Predicate<? super Node> |
filterNodeIsElement() |
static void |
forAllAttributes(Element aSrcNode,
BiConsumer<? super String,? super String> aConsumer) |
static void |
forAllAttributes(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 String |
getAttributeValue(Element aElement,
String sAttrName)
The latest version of XercesJ 2.9 returns an empty string for non existing
attributes.
|
static String |
getAttributeValue(Element aElement,
String sAttrName,
String sDefault)
The latest version of XercesJ 2.9 returns an empty string for non existing
attributes.
|
static String |
getAttributeValueNS(Element aElement,
String sNamespaceURI,
String sAttrName)
The latest version of XercesJ 2.9 returns an empty string for non existing
attributes.
|
static String |
getAttributeValueNS(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 Element |
getChildElementOfNames(Element aStartElement,
String... aTagNames)
Find a direct child using multiple levels, starting from a given start
element.
|
static int |
getDirectChildElementCount(Element aParent) |
static int |
getDirectChildElementCount(Element aParent,
String sTagName) |
static int |
getDirectChildElementCountNoNS(Element aParent) |
static int |
getDirectChildElementCountNoNS(Element aParent,
String sTagName) |
static int |
getDirectChildElementCountNS(Element aParent,
String sNamespaceURI) |
static int |
getDirectChildElementCountNS(Element aParent,
String sNamespaceURI,
String sLocalName) |
static Element |
getDocumentElement(Node aNode) |
static String |
getElementName(Node aNode) |
static Element |
getFirstChildElement(Node aStartNode)
Get the first direct child element of the passed element.
|
static Element |
getFirstChildElementOfName(Node aStartNode,
String sTagName)
Search all child nodes of the given for the first element that has the
specified tag name.
|
static Element |
getFirstChildElementOfName(Node aStartNode,
String sNamespaceURI,
String sLocalName)
Search all child nodes of the given for the first element that has the
specified tag name.
|
static String |
getFirstChildText(Node aStartNode)
Get the content of the first Text child element of the passed element.
|
static int |
getLength(NodeList aNL) |
static String |
getLocalNameOrName(Attr aAttr) |
static String |
getLocalNameOrTagName(Element aElement) |
static String |
getNamespaceURI(Node aNode) |
static Document |
getOwnerDocument(Node aNode)
Get the owner document of the passed node.
|
static String |
getPathToNode(Node aNode)
Shortcut for
getPathToNode(Node, String) using "/" as the
separator. |
static String |
getPathToNode(Node aNode,
String sSep)
Get the path from root node to the passed node.
|
static String |
getPathToNode2(Node aNode)
Shortcut for
getPathToNode2(Node,String) using "/" as the
separator. |
static String |
getPathToNode2(Node aNode,
String sSep)
Get the path from root node to the passed node.
|
static String |
getPrefix(Element aElement)
Get the namespace prefix of the passed element in a safe way.
|
static QName |
getQName(Element aElement)
Get the QName of the passed element.
|
static QName |
getXMLNSAttrQName(String sNSPrefix)
Get the full qualified attribute name to use for the given namespace
prefix.
|
static boolean |
hasChildElementNodes(Node aStartNode)
Check if the passed node has at least one direct child element or not.
|
static boolean |
hasNamespaceURI(Node aNode,
String sNamespaceURI) |
static boolean |
hasNoNamespaceURI(Node aNode) |
static boolean |
hasSameElementName(Element aFirst,
Element aSecond) |
static boolean |
isEmpty(NodeList aNL) |
static boolean |
isInlineNode(Node aNode)
Check if the passed node is a text node.
|
static void |
iterateChildren(Node aParent,
Consumer<? super Node> aConsumer)
Iterate all child nodes of the provided element NOT recursive.
|
static XMLHelper.PathToNodeBuilder |
pathToNodeBuilder() |
static void |
recursiveIterateChildren(Node aParent,
Consumer<? super Node> aConsumer)
Recursively iterate all children of the provided element.
|
static void |
removeAllChildElements(Element aElement)
Remove all child nodes of the given node.
|
@Nullable public static Document getOwnerDocument(@Nullable Node aNode)
aNode - The node to get the document from. May be null.null if the passed node was null.@Nonnull public static String getLocalNameOrTagName(@Nonnull Element aElement)
public static boolean hasNamespaceURI(@Nullable Node aNode, @Nullable String sNamespaceURI)
public static boolean isInlineNode(@Nullable Node aNode)
Text (Text and CData) or EntityReference nodes.aNode - The node to be checked.true if the passed node is a text node,
false otherwise.@Nonnegative public static int getLength(@Nullable NodeList aNL)
@Nonnull public static Predicate<? super Element> filterElementWithNamespace()
@Nonnull public static Predicate<? super Element> filterElementWithoutNamespace()
@Nonnull public static Predicate<? super Element> filterElementWithNamespace(@Nullable String sNamespaceURI)
@Nonnull public static Predicate<? super Element> filterElementWithNamespaceAndLocalName(@Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
@Nonnull public static Predicate<? super Element> filterElementWithTagName(@Nonnull @Nonempty String sTagName)
@Nonnull public static Predicate<? super Element> filterElementWithTagNameNoNS(@Nonnull @Nonempty String sTagName)
@Nullable public static Element getFirstChildElement(@Nullable Node aStartNode)
aStartNode - The element to start searching. May be null.null if the passed element does not have any direct
child element.public static boolean hasChildElementNodes(@Nullable Node aStartNode)
aStartNode - The parent element to be searched. May be null.true if the passed node has at least one child
element, false otherwise.@Nullable public static Element getFirstChildElementOfName(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
aStartNode - The parent element to be searched. May be null.sTagName - The tag name to search.null if the parent element has no such child element.@Nullable public static Element getFirstChildElementOfName(@Nullable Node aStartNode, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
aStartNode - The parent element to be searched. May be null.sNamespaceURI - Namespace URI to search. May be null.sLocalName - The tag name to search.null if the parent element has no such child element.@Nullable public static Element getChildElementOfNames(@Nullable Element aStartElement, @Nonnull String... aTagNames)
aStartElement - The element to start from. May be null.aTagNames - The child elements to be found in order. May neither be
null nor empty and may not contain null
elements.null if no such child element was found, of if the
start element was null.getFirstChildElementOfName(Node, String)@Nonnegative public static int getDirectChildElementCount(@Nullable Element aParent)
@Nonnegative public static int getDirectChildElementCountNoNS(@Nullable Element aParent)
@Nonnegative public static int getDirectChildElementCount(@Nullable Element aParent, @Nonnull @Nonempty String sTagName)
@Nonnegative public static int getDirectChildElementCountNoNS(@Nullable Element aParent, @Nonnull @Nonempty String sTagName)
@Nonnegative public static int getDirectChildElementCountNS(@Nullable Element aParent, @Nullable String sNamespaceURI)
@Nonnegative public static int getDirectChildElementCountNS(@Nullable Element aParent, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator(@Nullable Node aStartNode)
aStartNode - the parent element@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS(@Nullable Node aStartNode)
aStartNode - the parent element@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNoNS(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
aStartNode - the parent elementsTagName - the name of the tag that is desiredIllegalArgumentException - if the passed tag name is null or empty@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIterator(@Nullable Node aStartNode, @Nonnull @Nonempty String sTagName)
aStartNode - the parent elementsTagName - the name of the tag that is desiredIllegalArgumentException - if the passed tag name is null or empty@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNS(@Nullable Node aStartNode, @Nullable String sNamespaceURI)
@Nonnull public static com.helger.commons.collection.iterate.IIterableIterator<Element> getChildElementIteratorNS(@Nullable Node aStartNode, @Nullable String sNamespaceURI, @Nonnull @Nonempty String sLocalName)
public static boolean hasSameElementName(@Nonnull Element aFirst, @Nonnull Element aSecond)
@Nonnull public static XMLHelper.PathToNodeBuilder pathToNodeBuilder()
@Nonnull public static String getPathToNode(@Nonnull Node aNode)
getPathToNode(Node, String) using "/" as the
separator.aNode - The node to check.null path.@Nonnull public static String getPathToNode(@Nonnull Node aNode, @Nonnull String sSep)
aNode - The node to start. May not be null.sSep - The separator string to use. May not be null.@Nonnull public static String getPathToNode2(@Nonnull Node aNode)
getPathToNode2(Node,String) using "/" as the
separator.aNode - The node to check.null path.@Nonnull public static String getPathToNode2(@Nonnull Node aNode, @Nonnull String sSep)
aNode - The node to start. May not be null.sSep - The separator string to use. May not be null.public static void removeAllChildElements(@Nonnull Element aElement)
aElement - The element whose children are to be removed.@Nullable public static String getFirstChildText(@Nullable Node aStartNode)
aStartNode - the element to scan for a TextNode childnull if the element contains no text node as child@Nullable public static String getAttributeValue(@Nonnull Element aElement, @Nonnull String sAttrName)
aElement - the source element to get the attribute fromsAttrName - the name of the attribute to querynull if the attribute does not exists, the string
value otherwise@Nullable public static String getAttributeValue(@Nonnull Element aElement, @Nonnull String sAttrName, @Nullable String sDefault)
aElement - the source element to get the attribute from. May not be
null.sAttrName - the name of the attribute to query. May not be null.sDefault - the value to be returned if the attribute is not present.@Nullable public static String getAttributeValueNS(@Nonnull Element aElement, @Nullable String sNamespaceURI, @Nonnull String sAttrName)
aElement - the source element to get the attribute fromsNamespaceURI - The namespace URI of the attribute to retrieve. May be
null.sAttrName - the name of the attribute to querynull if the attribute does not exists, the string
value otherwise@Nullable public static String getAttributeValueNS(@Nonnull Element aElement, @Nullable String sNamespaceURI, @Nonnull String sAttrName, @Nullable String sDefault)
aElement - the source element to get the attribute from. May not be
null.sNamespaceURI - The namespace URI of the attribute to retrieve. May be
null.sAttrName - the name of the attribute to query. May not be null.sDefault - the value to be returned if the attribute is not present.@Nonnull @ReturnsMutableCopy public static com.helger.commons.collection.impl.ICommonsList<Attr> getAllAttributesAsList(@Nullable Element aSrcNode)
@Nonnull @ReturnsMutableCopy public static com.helger.commons.collection.impl.ICommonsOrderedMap<String,String> getAllAttributesAsMap(@Nullable Element aSrcNode)
public static void forAllAttributes(@Nullable Element aSrcNode, @Nonnull Consumer<? super Attr> aConsumer)
public static void forAllAttributes(@Nullable Element aSrcNode, @Nonnull BiConsumer<? super String,? super String> aConsumer)
@Nonnull public static QName getXMLNSAttrQName(@Nullable String sNSPrefix)
xmlns or
{http://www.w3.org/2000/xmlns/}xmlns:foo.sNSPrefix - The namespace prefix to build the attribute name from. May be
null or empty.XMLConstants.DEFAULT_NS_PREFIX or null) than
"xmlns" is returned, else "xmlns:prefix" is returned.@Nonnull public static String getPrefix(@Nullable Element aElement)
aElement - The element to be queried. May be null.XMLConstants.DEFAULT_NS_PREFIX or the provided prefix.
Never null.@Nonnull public static QName getQName(@Nonnull Element aElement)
aElement - The element to be used. May not be null.QName.public static void iterateChildren(@Nonnull Node aParent, @Nonnull Consumer<? super Node> aConsumer)
aParent - The parent node to start from. May not be null.aConsumer - The Consumer to be invoked for every node. May not be
null.public static void recursiveIterateChildren(@Nonnull Node aParent, @Nonnull Consumer<? super Node> aConsumer)
aParent - The parent node to start from. May not be null.aConsumer - The Consumer to be invoked for every node. May not be
null.Copyright © 2014–2023 Philip Helger. All rights reserved.