Class XMLUtil

java.lang.Object
org.apache.empire.xml.XMLUtil

public class XMLUtil extends Object
This class provides a collection of static helper functions for common XML tasks. The class cannot be instanciated since all. methods provided are declared static.

  • Field Details

    • log

      protected static final org.slf4j.Logger log
  • Method Details

    • createDocument

      public static Document createDocument()
      Returns a document newly created by the class's static DocumentBuilder.
      Returns:
      An empty DOM document.
    • getDocumentBuilder

      public static DocumentBuilder getDocumentBuilder()
      Returns an initialzed, namespace aware DocumentBuilder.
      Returns:
      The DocumentBuilder.
    • createDocument

      public static Element createDocument(String rootElemName)
    • createDocumentNS

      public static Element createDocumentNS(String prefix, String rootElemName, Map<String,String> nsMap)
    • addNamespaceURIs

      public static boolean addNamespaceURIs(Document doc, Map<String,String> nsMap)
    • getNamespaceURI

      public static String getNamespaceURI(Document doc, String prefix)
    • getFirstChild

      public static Element getFirstChild(Node parent)
      Gets the first (direct) child Element.
      Parameters:
      parent - the parent element below which to search the child
      Returns:
      the first child element, or null otherwise
    • findFirstChild

      public static Element findFirstChild(Node parent, String tagName)
      Finds the first (direct) child Element with a given tag name.
      Parameters:
      parent - the parent element below which to search the child
      tagName - the (tag) name of the desired child element
      Returns:
      the child element if an element of that name existed, or null otherwise
    • getNextSiblingElement

      public static Element getNextSiblingElement(Element child, boolean sameName)
      Returns the next sibling Element for an element, optionally matching tag names.
      Parameters:
      child - the element from which to search for a next sibling
      sameName - true to retrive the next sibling element of the same name, of false if any name is allowed
      Returns:
      the next sibling element if one exists, or null otherwise
    • findFirstChildWithAttrib

      public static Element findFirstChildWithAttrib(Node parent, String tagName, String attrName, Object value)
      Finds the first (direct) child element with a given tag name and attribute.
      Parameters:
      parent - the parent element below which to search the child
      tagName - the (tag) name of the desired child element
      attrName - the name of the attribute which value must match the given value
      value - the attribute value to which elements are matched.
      Returns:
      the child element if an element of that name existed, or null otherwise
    • findFirstChildDeep

      public static Element findFirstChildDeep(Element parent, String tagName)
      Finds the first element which name matchtes a given tag name that is locacted anywhere below the given parent.
      Parameters:
      parent - the parent element below which to search the child
      tagName - the (tag) name of the desired child element
      Returns:
      the child element if an element of that name existed, or null otherwise
    • findFirstChildDeep

      public static Element findFirstChildDeep(Document doc, String tagName)
      Returns the first element which name matchtes a given tag name.
      Parameters:
      doc - the xml document in which to find an element of the given name
      tagName - the (tag) name of the desired child element
      Returns:
      the child element if an element of that name existed, or null otherwise
    • getElementText

      public static String getElementText(Node elem)
      Retrieves the text of a given element.
      Parameters:
      elem - the Element for which the text value is requested
      Returns:
      the text value of that element or null if the element has no text value
    • setElementText

      public static boolean setElementText(Node elem, Object text)
      Sets the text value of an Element. if current text of the element is replaced with the new text if text is null any current text value is deleted.
      Parameters:
      elem - the Element for which the text value should be set
      text - the new text value of the element
      Returns:
      true if the text could be set or false otherwise
    • addElement

      public static Element addElement(Node parent, String name, String value)
      Adds a new child element to a parent.
      Parameters:
      parent - the Element to which to append the child
      name - the (tag) name of the new child
      value - the text value of the new element. (can be null!)
      Returns:
      the new child element
    • addElement

      public static Element addElement(Element parent, String name)
      Adds a child element to the parent.
      Parameters:
      parent -
      name -
      Returns:
      the newly created child element
    • addElementNS

      public static Element addElementNS(Node parent, String prefix, String name, String value)
      Adds a new child element to a parent with a namespace.
      Parameters:
      parent - the Element to which to append the child
      prefix - the name of the namespace this element belongs to
      name - the (tag) name of the new child
      value - the text value of the new element. (can be null!)
      Returns:
      the new child element
    • addElementNS

      public static Element addElementNS(Element parent, String prefix, String name)
    • insertElement

      public static Element insertElement(Node parent, String name, String value, Element pos)
      Inserts a new child element to a parent.
      Parameters:
      parent - the Element to which to append the child
      name - the (tag) name of the new child
      value - the text value of the new element. (can be null!)
      pos - the inserted element will be placed before this element
      Returns:
      the new child element
    • insertElement

      public static Element insertElement(Node parent, String name, Element pos)
    • insertElementNS

      public static Element insertElementNS(Node parent, String prefix, String name, String value, Element pos)
      Inserts a new child element to a parent.
      Parameters:
      parent - the Element to which to append the child
      prefix -
      name - the (tag) name of the new child
      value - the text value of the new element. (can be null!)
      pos - pos the inserted element will be placed before this element
      Returns:
      the new child element
    • insertElementNS

      public static Element insertElementNS(Node parent, String prefix, String name, Element pos)
    • getChildText

      public static String getChildText(Node parent, String childName)
      Returns the text value of a given child element.
      Parameters:
      parent - the Element which contains the child
      childName - the (tag) name of the child
      Returns:
      the text value of the child or null if no child exists or the child does not have a text value
    • changeTagName

      public static boolean changeTagName(Element elem, String newName)
      Changes the tag name of an element.
      Parameters:
      elem - Element which name should be changed
      newName - new tag name of the element
      Returns:
      true if the name was changed successfully or false otherwise