Class XML

java.lang.Object
com.yahoo.text.XML

public class XML extends Object
Static XML utility methods
Author:
Bjorn Borud, Vegard Havdal, bratseth, Steinar Knutsen
  • Constructor Summary

    Constructors
    Constructor
    Description
    XML()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static Element
    Returns the first child with the given name, or null if none
    static List<Element>
    Returns the child Element objects from a w3c dom spec
    static List<Element>
    getChildren(Element spec, String name)
    Returns the child Element objects with given name from a w3c dom spec
    static Document
    getDocument(File xmlFile)
    Returns the parsed Document from an XML file
    static Document
    Returns the parsed Document from an XML file
    static Document
    getDocument(String xmlString)
    Returns the Document of the string XML payload
    Creates a new XML DocumentBuilder
    getDocumentBuilder(String implementation, ClassLoader classLoader)
    Creates a new XML DocumentBuilder
    static String
    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
    xmlEscape(String s1, boolean isAttribute)
    Replaces the characters that need to be escaped with their corresponding character entities.
    static String
    xmlEscape(String s1, boolean isAttribute, boolean escapeLowAscii)
    Replaces the characters that need to be escaped with their corresponding character entities.
    static String
    xmlEscape(String s1, boolean isAttribute, boolean escapeLowAscii, char stripCharacter)
    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 is true ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute is true 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 is true ampersand (&) less than (<) larger than (>) double quotes (") if isAttribute is true with character entities.
    static String
    xmlEscape(String s1, boolean isAttribute, char stripCharacter)
    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 is true with character entities.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • XML

      public XML()
  • Method Details

    • xmlEscape

      public static String xmlEscape(String s1)
      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

      public static String xmlEscape(String s1, boolean isAttribute)
      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
      isAttribute - 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

      public static String xmlEscape(String s1, boolean isAttribute, 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 XML
      isAttribute - 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

      public static String xmlEscape(String s1, boolean isAttribute, boolean escapeLowAscii)
      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
      isAttribute - 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 XML
      isAttribute - Is the input string to be used as an attribute?
      escapeLowAscii - Should ascii characters below 32 be escaped as well
      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

      public 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 is true
      with character entities.
    • 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
      with character entities.
    • 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
      with character entities.
      Parameters:
      stripCodePoint - any occurrence of this character is removed from the string
    • getDocument

      public static Document getDocument(File xmlFile)
      Returns the parsed Document from an XML file
      Throws:
      IllegalArgumentException - if the file cannot be opened or parsed
    • getDocument

      public static Document getDocument(Reader reader)
      Returns the parsed Document from an XML file
      Throws:
      IllegalArgumentException - if the file cannot be opened or parsed
    • getDocument

      public static Document getDocument(String xmlString)
      Returns the Document of the string XML payload
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder()
      Creates a new XML DocumentBuilder
      Returns:
      a DocumentBuilder
      Throws:
      RuntimeException - if we fail to create one
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder(String implementation, ClassLoader classLoader)
      Creates a new XML DocumentBuilder
      Parameters:
      implementation - which jaxp implementation should be used
      classLoader - which class loader should be used when getting a new DocumentBuilder
      Returns:
      a DocumentBuilder
      Throws:
      RuntimeException - if we fail to create one
    • getChildren

      public static List<Element> getChildren(Element spec)
      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

      public static List<Element> getChildren(Element spec, String name)
      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

      public static String getValue(Element e)
      Gets the string contents of the given Element. Returns "", never null if the element is null, or has no content
    • getChild

      public static Element getChild(Element e, String name)
      Returns the first child with the given name, or null if none
    • getNodePath

      public 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.
      Parameters:
      n - The xml node to find path to
      sep - The separator string
      Returns:
      The path to the xml node as a String
    • isName

      public static boolean isName(CharSequence possibleName)
      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