public class XmlUtils
extends java.lang.Object
For Kotlin usage, many of these are exposed as more convenient extension
methods in DomExtensions
| Modifier and Type | Field and Description |
|---|---|
static char |
NS_SEPARATOR
Separator for xml namespace and localname
|
static java.lang.String |
XML_COMMENT_BEGIN |
static java.lang.String |
XML_COMMENT_END |
static java.lang.String |
XML_PROLOG |
| Constructor and Description |
|---|
XmlUtils() |
| Modifier and Type | Method and Description |
|---|---|
static void |
appendXmlAttributeValue(java.lang.StringBuilder sb,
java.lang.String attrValue)
Appends text to the given
StringBuilder and escapes it as required for a
DOM attribute node. |
static void |
appendXmlAttributeValue(java.lang.StringBuilder sb,
java.lang.String attrValue,
int start,
int end)
Appends text to the given
StringBuilder and escapes it as required for a
DOM attribute node. |
static void |
appendXmlTextValue(java.lang.StringBuilder sb,
java.lang.String textValue)
Appends text to the given
StringBuilder and escapes it as required for a
DOM text node. |
static void |
appendXmlTextValue(java.lang.StringBuilder sb,
java.lang.String textValue,
int start,
int end)
Appends text to the given
StringBuilder and escapes it as required for a DOM text
node. |
static void |
attachSourceFile(org.w3c.dom.Node node,
SourceFile sourceFile) |
static javax.xml.parsers.SAXParserFactory |
configureSaxFactory(javax.xml.parsers.SAXParserFactory factory,
boolean namespaceAware,
boolean checkDtd) |
static org.w3c.dom.Document |
createDocument(boolean namespaceAware)
Creates and returns a new empty document.
|
static javax.xml.parsers.SAXParser |
createSaxParser(javax.xml.parsers.SAXParserFactory factory) |
static javax.xml.parsers.SAXParser |
createSaxParser(javax.xml.parsers.SAXParserFactory factory,
boolean allowDocTypeDeclarations) |
static java.lang.String |
formatFloatAttribute(double value)
Formats the number and removes trailing zeros after the decimal dot and also the dot itself
if there were non-zero digits after it.
|
static java.lang.String |
fromXmlAttributeValue(java.lang.String escapedAttrValue)
Converts the given XML-attribute-safe value to a java string
|
static org.w3c.dom.Element |
getFirstSubTag(org.w3c.dom.Node parent)
Returns the first child element of the given node
|
static org.w3c.dom.Element |
getFirstSubTagByName(org.w3c.dom.Node parent,
java.lang.String name)
Returns the next sibling element from the given node that matches the given name
|
static org.w3c.dom.Element |
getNextTag(org.w3c.dom.Node node)
Returns the next sibling element from the given node
|
static org.w3c.dom.Element |
getNextTagByName(org.w3c.dom.Node node,
java.lang.String name)
Returns the next sibling element from the given node
|
static org.w3c.dom.Comment |
getPreviousComment(org.w3c.dom.Node element)
Returns the comment preceding the given element with no other elements in between, or null
if the element is not preceded by a comment.
|
static java.lang.String |
getPreviousCommentText(org.w3c.dom.Node element)
Returns the text of the comment preceding the given element with no other elements in
between, or null if the element is not preceded by a comment or if the comment is empty
or consists of only whitespace characters.
|
static org.w3c.dom.Element |
getPreviousTag(org.w3c.dom.Node node)
Returns the previous sibling element from the given node
|
static org.w3c.dom.Element |
getPreviousTagByName(org.w3c.dom.Node node,
java.lang.String name) |
static java.lang.String |
getRootTagName(java.io.File xmlFile)
Returns the name of the root element tag stored in the given file, or null if it can't be
determined.
|
static java.lang.String |
getRootTagName(java.lang.String xmlText)
Returns the name of the root element tag stored in the given file, or null if it can't be
determined.
|
static SourceFilePosition |
getSourceFilePosition(org.w3c.dom.Node node) |
static int |
getSubTagCount(org.w3c.dom.Node parent)
Returns the number of children sub tags of the given node.
|
static java.lang.Iterable<org.w3c.dom.Element> |
getSubTags(org.w3c.dom.Node parent)
Returns an iterator for the children elements of the given node.
|
static java.util.List<org.w3c.dom.Element> |
getSubTagsAsList(org.w3c.dom.Node parent)
Returns the children elements of the given node
|
static java.lang.Iterable<org.w3c.dom.Element> |
getSubTagsByName(org.w3c.dom.Node parent,
java.lang.String tagName)
Returns an iterator for the children elements of the given node matching the
given tag name.
|
static java.io.Reader |
getUtfReader(java.io.File file)
Returns a character reader for the given file, which must be a UTF encoded file.
|
static boolean |
hasElementChildren(org.w3c.dom.Node node)
Returns true if the given node has one or more element children
|
static boolean |
isProtoXml(byte[] bytes)
Checks if the given array of bytes is likely to represent XML in a proto format.
|
static boolean |
isProtoXml(java.io.InputStream stream)
Checks if the given input stream is likely to represent XML in a proto format.
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri)
Returns the namespace prefix matching the requested namespace URI.
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri,
boolean create)
Returns the namespace prefix matching the requested namespace URI.
|
static java.lang.String |
lookupNamespacePrefix(org.w3c.dom.Node node,
java.lang.String nsUri,
java.lang.String defaultPrefix,
boolean create)
Returns the namespace prefix matching the requested namespace URI.
|
static org.w3c.dom.Document |
parseDocument(java.io.Reader xml,
boolean namespaceAware)
Parses the given
Reader as a DOM document, using the JDK parser. |
static org.w3c.dom.Document |
parseDocument(java.lang.String xml,
boolean namespaceAware)
Parses the given XML string as a DOM document, using the JDK parser.
|
static org.w3c.dom.Document |
parseDocumentSilently(java.lang.String xml,
boolean namespaceAware)
Parses the given XML string as a DOM document, using the JDK parser.
|
static org.w3c.dom.Document |
parseUtfXmlFile(java.io.File file,
boolean namespaceAware)
Parses the given UTF file as a DOM document, using the JDK parser.
|
static java.lang.String |
stripBom(java.lang.String xml)
Strips out a leading UTF byte order mark, if present
|
static java.lang.String |
toXml(org.w3c.dom.Node node)
Dump an XML tree to string.
|
static java.lang.String |
toXml(org.w3c.dom.Node node,
java.util.Map<SourcePosition,SourceFilePosition> blame) |
static java.lang.String |
toXmlAttributeValue(java.lang.String attrValue)
Converts the given attribute value to an XML-attribute-safe value, meaning that
single and double quotes are replaced with their corresponding XML entities.
|
static java.lang.String |
toXmlTextValue(java.lang.String textValue)
Converts the given attribute value to an XML-text-safe value, meaning that
less than and ampersand characters are escaped.
|
public static final java.lang.String XML_COMMENT_BEGIN
public static final java.lang.String XML_COMMENT_END
public static final java.lang.String XML_PROLOG
public static final char NS_SEPARATOR
@NonNull
public static java.lang.String lookupNamespacePrefix(@NonNull
org.w3c.dom.Node node,
@NonNull
java.lang.String nsUri)
lookupNamespacePrefix(Node, String, boolean) instead.node - The current node. Must not be null.nsUri - The namespace URI of which the prefix is to be found,
e.g. SdkConstants.ANDROID_URI@NonNull
public static java.lang.String lookupNamespacePrefix(@NonNull
org.w3c.dom.Node node,
@NonNull
java.lang.String nsUri,
boolean create)
node - The current node. Must not be null.nsUri - The namespace URI of which the prefix is to be found, e.g.
SdkConstants.ANDROID_URIcreate - whether the namespace declaration should be created, if
necessarypublic static java.lang.String lookupNamespacePrefix(@Nullable
org.w3c.dom.Node node,
@Nullable
java.lang.String nsUri,
@Nullable
java.lang.String defaultPrefix,
boolean create)
node - The current node. Must not be null.nsUri - The namespace URI of which the prefix is to be found, e.g.
SdkConstants.ANDROID_URIdefaultPrefix - The default prefix (root) to use if the namespace is
not found. If null, do not create a new namespace if this URI
is not defined for the document.create - whether the namespace declaration should be created, if
necessary@NonNull
public static java.lang.String toXmlAttributeValue(@NonNull
java.lang.String attrValue)
attrValue - the value to be escaped@NonNull
public static java.lang.String fromXmlAttributeValue(@NonNull
java.lang.String escapedAttrValue)
escapedAttrValue - the escaped value@NonNull
public static java.lang.String toXmlTextValue(@NonNull
java.lang.String textValue)
textValue - the text value to be escapedpublic static void appendXmlAttributeValue(@NonNull
java.lang.StringBuilder sb,
@NonNull
java.lang.String attrValue)
StringBuilder and escapes it as required for a
DOM attribute node.sb - the string builderattrValue - the attribute value to be appended and escapedpublic static void appendXmlAttributeValue(@NonNull
java.lang.StringBuilder sb,
@NonNull
java.lang.String attrValue,
int start,
int end)
StringBuilder and escapes it as required for a
DOM attribute node.sb - the string builderattrValue - the attribute value to be appended and escapedstart - the starting offset in the text stringend - the ending offset in the text stringpublic static void appendXmlTextValue(@NonNull
java.lang.StringBuilder sb,
@NonNull
java.lang.String textValue)
StringBuilder and escapes it as required for a
DOM text node.sb - the string buildertextValue - the text value to be appended and escapedpublic static void appendXmlTextValue(@NonNull
java.lang.StringBuilder sb,
@NonNull
java.lang.String textValue,
int start,
int end)
StringBuilder and escapes it as required for a DOM text
node.sb - the string buildertextValue - the text value to be appended and escapedstart - the starting offset in the text stringend - the ending offset in the text stringpublic static boolean hasElementChildren(@NonNull
org.w3c.dom.Node node)
node - the node to test for element children@NonNull
public static java.io.Reader getUtfReader(@NonNull
java.io.File file)
throws java.io.IOException
The reader does not need to be closed by the caller (because the file is read in full in one shot and the resulting array is then wrapped in a byte array input stream, which does not need to be closed.)
java.io.IOException@NonNull
public static org.w3c.dom.Document parseDocument(@NonNull
java.lang.String xml,
boolean namespaceAware)
throws java.io.IOException,
org.xml.sax.SAXException
xml - the XML content to be parsed (must be well formed)namespaceAware - whether the parser is namespace awarejava.io.IOExceptionorg.xml.sax.SAXException@NonNull
public static org.w3c.dom.Document parseDocument(@NonNull
java.io.Reader xml,
boolean namespaceAware)
throws java.io.IOException,
org.xml.sax.SAXException
Reader as a DOM document, using the JDK parser. The parser does not
validate, and is optionally namespace aware.xml - a reader for the XML content to be parsed (must be well formed)namespaceAware - whether the parser is namespace awarejava.io.IOExceptionorg.xml.sax.SAXException@NonNull
public static org.w3c.dom.Document parseUtfXmlFile(@NonNull
java.io.File file,
boolean namespaceAware)
throws java.io.IOException,
org.xml.sax.SAXException
file - the UTF encoded file to parsenamespaceAware - whether the parser is namespace awarejava.io.IOExceptionorg.xml.sax.SAXException@NonNull public static org.w3c.dom.Document createDocument(boolean namespaceAware)
@NonNull
public static java.lang.String stripBom(@NonNull
java.lang.String xml)
@Nullable
public static org.w3c.dom.Document parseDocumentSilently(@NonNull
java.lang.String xml,
boolean namespaceAware)
xml - the XML content to be parsed (must be well formed)namespaceAware - whether the parser is namespace awarepublic static javax.xml.parsers.SAXParserFactory configureSaxFactory(@NonNull
javax.xml.parsers.SAXParserFactory factory,
boolean namespaceAware,
boolean checkDtd)
@NonNull
public static javax.xml.parsers.SAXParser createSaxParser(@NonNull
javax.xml.parsers.SAXParserFactory factory)
throws javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXException@NonNull
public static javax.xml.parsers.SAXParser createSaxParser(@NonNull
javax.xml.parsers.SAXParserFactory factory,
boolean allowDocTypeDeclarations)
throws javax.xml.parsers.ParserConfigurationException,
org.xml.sax.SAXException
javax.xml.parsers.ParserConfigurationExceptionorg.xml.sax.SAXExceptionpublic static java.lang.String toXml(@NonNull
org.w3c.dom.Node node)
XmlPrettyPrinter.prettyPrint(node) in
sdk-common.public static java.lang.String toXml(@NonNull
org.w3c.dom.Node node,
@Nullable
java.util.Map<SourcePosition,SourceFilePosition> blame)
public static void attachSourceFile(@NonNull
org.w3c.dom.Node node,
@NonNull
SourceFile sourceFile)
@NonNull public static SourceFilePosition getSourceFilePosition(@NonNull org.w3c.dom.Node node)
@NonNull public static java.lang.String formatFloatAttribute(double value)
value - the value to be formatted@Nullable
public static java.lang.String getRootTagName(@NonNull
java.io.File xmlFile)
@Nullable
public static java.lang.String getRootTagName(@NonNull
java.lang.String xmlText)
@NonNull
public static java.util.List<org.w3c.dom.Element> getSubTagsAsList(@NonNull
org.w3c.dom.Node parent)
parent - the parent node@NonNull
public static java.lang.Iterable<org.w3c.dom.Element> getSubTags(@Nullable
org.w3c.dom.Node parent)
getSubTagsAsList(Node) instead.
NOTE: The iterator() call can only be called once!
@NonNull
public static java.lang.Iterable<org.w3c.dom.Element> getSubTagsByName(@Nullable
org.w3c.dom.Node parent,
@NonNull
java.lang.String tagName)
If you want to access the children as a list, use
getSubTagsAsList(Node) instead.
NOTE: The iterator() call can only be called once!
@Nullable
public static org.w3c.dom.Element getFirstSubTag(@Nullable
org.w3c.dom.Node parent)
@Nullable
public static org.w3c.dom.Element getNextTag(@Nullable
org.w3c.dom.Node node)
@Nullable
public static org.w3c.dom.Element getPreviousTag(@Nullable
org.w3c.dom.Node node)
@Nullable
public static org.w3c.dom.Element getFirstSubTagByName(@Nullable
org.w3c.dom.Node parent,
@NonNull
java.lang.String name)
@Nullable
public static org.w3c.dom.Element getNextTagByName(@Nullable
org.w3c.dom.Node node,
@NonNull
java.lang.String name)
@Nullable
public static org.w3c.dom.Element getPreviousTagByName(@Nullable
org.w3c.dom.Node node,
@NonNull
java.lang.String name)
@Nullable
public static org.w3c.dom.Comment getPreviousComment(@NonNull
org.w3c.dom.Node element)
@Nullable
public static java.lang.String getPreviousCommentText(@NonNull
org.w3c.dom.Node element)
public static int getSubTagCount(@Nullable
org.w3c.dom.Node parent)
parent - the parent nodepublic static boolean isProtoXml(@NonNull
byte[] bytes)
bytes - the candidate XML contents to checkpublic static boolean isProtoXml(@NonNull
java.io.InputStream stream)
stream - the candidate XML stream to check