Class XMLUtils

java.lang.Object
org.deltava.util.XMLUtils
Direct Known Subclasses:
KMLUtils

public class XMLUtils extends Object
A utility class for performing XML operations.
Since:
1.0
Version:
11.2
Author:
Luke
  • Constructor Details

    • XMLUtils

      protected XMLUtils()
  • Method Details

    • getChildText

      public static String getChildText(Element e, String... names)
      Finds a child element multiple levels down and returns its value.
      Parameters:
      e - the Element
      names - the ordered list of child names
      Returns:
      an Element, or null if not found
    • createElement

      public static Element createElement(String name, String value, boolean asCDATA)
      Creates a new XML document element.
      Parameters:
      name - the element name
      value - the element value
      asCDATA - TRUE if the value should be rendered as a CDATA, otherwise FALSE
      Returns:
      the XML element
    • createElement

      public static Element createElement(String name, String value)
      Creates a new XML document element.
      Parameters:
      name - the element name
      value - the element value
      Returns:
      the XML element
    • createIfPresent

      public static Element createIfPresent(String name, String value)
      Creates a new XML document element if the value is not null or empty.
      Parameters:
      name - the element name
      value - the element value
      Returns:
      the XML element, or null
    • createElement

      public static Element createElement(String name, String subElementName, String value)
      Creates a new XML document element, with an embedded child element.
      Parameters:
      name - the element name
      subElementName - the child element name
      value - the child element value
      Returns:
      the element
    • hasElement

      public static boolean hasElement(Element e, String name)
      Returns if an XML element contains a child element with a particular name.
      Parameters:
      e - the XML Element
      name - the child name
      Returns:
      TRUE if a child element with the specified name exists, otherwise FALSE
    • addIfPresent

      public static boolean addIfPresent(Element be, Element e)
      Adds a child XML document element to an existing element, if it is not null
      Parameters:
      be - the base element
      e - the child element
      Returns:
      if e is not null
    • setChildText

      public static void setChildText(Element e, String ceName, String value)
      Updates a child element's text. If the element does not exist, it will be created.
      Parameters:
      e - the parent Element
      ceName - the child element name
      value - the child element value
    • format

      public static String format(Document doc, String encoding)
      Converts an XML document into text using a specific character set.
      Parameters:
      doc - the document
      encoding - the string encoding
      Returns:
      the formatted XML document
    • stripInvalidUnicode

      public static String stripInvalidUnicode(String txt)
      Removes invalid unicode characters from an XML element.
      Parameters:
      txt - the string to process
      Returns:
      the string with invalid unicode removed