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 TypeMethodDescriptionstatic Element
Returns the first child with the given name, or null if nonegetChildren
(Element spec) Returns the child Element objects from a w3c dom specgetChildren
(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 payloadstatic DocumentBuilder
Creates a new XML DocumentBuilderstatic DocumentBuilder
getDocumentBuilder
(String implementation, ClassLoader classLoader) Creates a new XML DocumentBuilderstatic 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.- Parameters:
s1
- String possibly containing characters that need to be escaped in XML- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities.
-
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?stripCharacter
- any occurrence of this character is removed from the string- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?escapeLowAscii
- Should ascii characters below 32 be escaped as well- Returns:
- Returns the input string with special characters that need to be escaped replaced by character entities
-
xmlEscape
public static String xmlEscape(String s1, boolean isAttribute, boolean escapeLowAscii, char stripCharacter) Replaces the characters that need to be escaped with their corresponding character entities.- Parameters:
s1
- String possibly containing characters that need to be escaped in XMLisAttribute
- Is the input string to be used as an attribute?escapeLowAscii
- Should ascii characters below 32 be escaped as wellstripCharacter
- any occurrence of this character is removed from the string- Returns:
- 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
-
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
-
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
-