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 Details

    • XML

      public XML()
  • Method Details

    • xmlEscape

      public static String xmlEscape(String string)
      Replaces the characters that need to be escaped with their corresponding character entities.
    • xmlEscape

      public static String xmlEscape(String string, boolean isAttribute)
      Replaces the characters that need to be escaped with their corresponding character entities.
    • xmlEscape

      public static String xmlEscape(String string, boolean isAttribute, char stripCharacter)
      Replaces the characters that need to be escaped with their corresponding character entities.
    • xmlEscape

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

      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
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder(boolean namespaceAware)
      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 used
      classLoader - which class loader should be used when getting a new DocumentBuilder
      namespaceAware - Whether the parser should be aware of xml namespaces
      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
    • attribute

      public static Optional<String> attribute(String name, Element element)
      Returns the given attribute name from element, or empty if the element does not have it.
    • 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
    • getChildValue

      public static Optional<String> getChildValue(Element e, String name)
      Returns:
      the value of child with the given name, empty string if no value, or empty if no child with name
    • 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