Package com.yahoo.text
Class XML
java.lang.Object
com.yahoo.text.XML
Static XML utility methods
- Author:
- Bjorn Borud, Vegard Havdal, bratseth, Steinar Knutsen
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionReturns the given attribute name from element, or empty if the element does not have it.static Element
Returns the first child with the given name, or null if nonegetChildren
(Element spec) Returns the child Element objects from a w3c dom spec.getChildren
(Element spec, String name) Returns the child Element objects with given name from a w3c dom specstatic Document
getDocument
(File xmlFile) Returns the parsed Document from an XML filestatic Document
getDocument
(Reader reader) Returns the parsed Document from an XML filestatic Document
getDocument
(String xmlString) Returns the Document of the string XML payload.static DocumentBuilder
Creates a new XML DocumentBuilder.static DocumentBuilder
getDocumentBuilder
(boolean namespaceAware) Creates a new XML DocumentBuilder.static DocumentBuilder
getDocumentBuilder
(String implementation, ClassLoader classLoader) Creates a new XML DocumentBuilder.static DocumentBuilder
getDocumentBuilder
(String implementation, ClassLoader classLoader, boolean namespaceAware) Creates a new XML DocumentBuilder.static String
getNodePath
(Node n, String sep) Returns the path to the given xml node, where each node name is separated by the given separator string.static String
Gets the string contents of the given Element.static boolean
isName
(CharSequence possibleName) Check whether the name of a tag or attribute conforms to XML 1.1 (Second Edition).static String
Replaces the characters that need to be escaped with their corresponding character entities.static String
Replaces the characters that need to be escaped with their corresponding character entities.static String
Replaces the characters that need to be escaped with their corresponding character entities.static String
Replaces the characters that need to be escaped with their corresponding character entities.static String
xmlEscape
(String string, boolean isAttribute, boolean escapeLowAscii, StringBuilder buffer) Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if escapeLowAscii istrue
ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.static String
xmlEscape
(String string, boolean isAttribute, boolean escapeLowAscii, StringBuilder buffer, int stripCodePoint) Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if escapeLowAscii istrue
ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.static String
Replaces the characters that need to be escaped with their corresponding character entities.static String
xmlEscape
(String string, boolean isAttribute, StringBuilder buffer) Replaces the following: all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute istrue
with character entities.
-
Constructor Details
-
XML
public XML()
-
-
Method Details
-
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities. -
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities. -
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities. -
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities. -
xmlEscape
public static String xmlEscape(String string, boolean isAttribute, boolean escapeLowAscii, char stripCharacter) Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
string
- the string possibly containing characters that need to be escaped in XMLisAttribute
- whether the input string to be used as an attributeescapeLowAscii
- whether ascii characters below 32 should be escaped as wellstripCharacter
- any occurrence of this character is removed from the string- Returns:
- the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr)
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
-
xmlEscape
public static String xmlEscape(String string, boolean isAttribute, boolean escapeLowAscii, StringBuilder buffer) Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
true
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
-
xmlEscape
public static String xmlEscape(String string, boolean isAttribute, boolean escapeLowAscii, StringBuilder buffer, int stripCodePoint) Replaces the following:- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
true
- ampersand (&)
- less than (<)
- larger than (>)
- double quotes (") if isAttribute is
true
- Parameters:
stripCodePoint
- any occurrence of this character is removed from the string
- all ascii codes less than 32 except 9 (tab), 10 (nl) and 13 (cr) if
escapeLowAscii is
-
getDocument
Returns the parsed Document from an XML file- Throws:
IllegalArgumentException
- if the file cannot be opened or parsed
-
getDocument
Returns the parsed Document from an XML file- Throws:
IllegalArgumentException
- if the file cannot be opened or parsed
-
getDocument
Returns the Document of the string XML payload. -
getDocumentBuilder
Creates a new XML DocumentBuilder.- Returns:
- a DocumentBuilder
- Throws:
RuntimeException
- if we fail to create one
-
getDocumentBuilder
Creates a new XML DocumentBuilder.- Parameters:
implementation
- which jaxp implementation should be usedclassLoader
- which class loader should be used when getting a new DocumentBuilder- Returns:
- a DocumentBuilder
- Throws:
RuntimeException
- if we fail to create one
-
getDocumentBuilder
Creates a new XML DocumentBuilder.- Parameters:
namespaceAware
- Whether the parser should be aware of xml namespaces- Returns:
- a DocumentBuilder
- Throws:
RuntimeException
- if we fail to create one
-
getDocumentBuilder
public static DocumentBuilder getDocumentBuilder(String implementation, ClassLoader classLoader, boolean namespaceAware) Creates a new XML DocumentBuilder.- Parameters:
implementation
- which jaxp implementation should be usedclassLoader
- which class loader should be used when getting a new DocumentBuildernamespaceAware
- Whether the parser should be aware of xml namespaces- Returns:
- a DocumentBuilder
- Throws:
RuntimeException
- if we fail to create one
-
getChildren
Returns the child Element objects from a w3c dom spec.- Returns:
- List of elements. Empty list (never null) if none found or if the given element is null
-
getChildren
Returns the child Element objects with given name from a w3c dom spec- Returns:
- List of elements. Empty list (never null) if none found or the given element is null
-
attribute
Returns the given attribute name from element, or empty if the element does not have it. -
getValue
Gets the string contents of the given Element. Returns "", never null if the element is null, or has no content -
getChild
Returns the first child with the given name, or null if none -
getNodePath
Returns the path to the given xml node, where each node name is separated by the given separator string.- Parameters:
n
- the xml node to find path tosep
- the separator string- Returns:
- the path to the xml node as a String
-
isName
Check whether the name of a tag or attribute conforms to XML 1.1 (Second Edition). This does not check against reserved names, it only checks the set of characters used.- Parameters:
possibleName
- a possibly valid XML name- Returns:
- true if the name may be used as an XML tag or attribute name
-