Package org.apache.empire.xml
Class XMLUtil
java.lang.Object
org.apache.empire.xml.XMLUtil
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic ElementaddElement(Element parent, String name) Adds a child element to the parent.static ElementaddElement(Node parent, String name, String value) Adds a new child element to a parent.static ElementaddElementNS(Element parent, String prefix, String name) static ElementaddElementNS(Node parent, String prefix, String name, String value) Adds a new child element to a parent with a namespace.static booleanaddNamespaceURIs(Document doc, Map<String, String> nsMap) static booleanchangeTagName(Element elem, String newName) Changes the tag name of an element.static DocumentReturns a document newly created by the class's static DocumentBuilder.static ElementcreateDocument(String rootElemName) static Elementstatic ElementfindFirstChild(Node parent, String tagName) Finds the first (direct) child Element with a given tag name.static ElementfindFirstChildDeep(Document doc, String tagName) Returns the first element which name matchtes a given tag name.static ElementfindFirstChildDeep(Element parent, String tagName) Finds the first element which name matchtes a given tag name that is locacted anywhere below the given parent.static ElementfindFirstChildWithAttrib(Node parent, String tagName, String attrName, Object value) Finds the first (direct) child element with a given tag name and attribute.static StringgetChildText(Node parent, String childName) Returns the text value of a given child element.static DocumentBuilderReturns an initialzed, namespace aware DocumentBuilder.static StringgetElementText(Node elem) Retrieves the text of a given element.static ElementgetFirstChild(Node parent) Gets the first (direct) child Element.static StringgetNamespaceURI(Document doc, String prefix) static ElementgetNextSiblingElement(Element child, boolean sameName) Returns the next sibling Element for an element, optionally matching tag names.static ElementinsertElement(Node parent, String name, String value, Element pos) Inserts a new child element to a parent.static ElementinsertElement(Node parent, String name, Element pos) static ElementInserts a new child element to a parent.static ElementinsertElementNS(Node parent, String prefix, String name, Element pos) static booleansetElementText(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.
-
Field Details
-
log
protected static final org.slf4j.Logger log
-
-
Method Details
-
createDocument
Returns a document newly created by the class's static DocumentBuilder.- Returns:
- An empty DOM document.
-
getDocumentBuilder
Returns an initialzed, namespace aware DocumentBuilder.- Returns:
- The DocumentBuilder.
-
createDocument
-
createDocumentNS
-
addNamespaceURIs
-
getNamespaceURI
-
getFirstChild
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
Finds the first (direct) child Element with a given tag name.- Parameters:
parent- the parent element below which to search the childtagName- the (tag) name of the desired child element- Returns:
- the child element if an element of that name existed, or null otherwise
-
getNextSiblingElement
Returns the next sibling Element for an element, optionally matching tag names.- Parameters:
child- the element from which to search for a next siblingsameName- 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 childtagName- the (tag) name of the desired child elementattrName- the name of the attribute which value must match the given valuevalue- the attribute value to which elements are matched.- Returns:
- the child element if an element of that name existed, or null otherwise
-
findFirstChildDeep
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 childtagName- the (tag) name of the desired child element- Returns:
- the child element if an element of that name existed, or null otherwise
-
findFirstChildDeep
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 nametagName- the (tag) name of the desired child element- Returns:
- the child element if an element of that name existed, or null otherwise
-
getElementText
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
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 settext- the new text value of the element- Returns:
- true if the text could be set or false otherwise
-
addElement
Adds a new child element to a parent.- Parameters:
parent- the Element to which to append the childname- the (tag) name of the new childvalue- the text value of the new element. (can be null!)- Returns:
- the new child element
-
addElement
Adds a child element to the parent.- Parameters:
parent-name-- Returns:
- the newly created child element
-
addElementNS
Adds a new child element to a parent with a namespace.- Parameters:
parent- the Element to which to append the childprefix- the name of the namespace this element belongs toname- the (tag) name of the new childvalue- the text value of the new element. (can be null!)- Returns:
- the new child element
-
addElementNS
-
insertElement
Inserts a new child element to a parent.- Parameters:
parent- the Element to which to append the childname- the (tag) name of the new childvalue- 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
-
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 childprefix-name- the (tag) name of the new childvalue- 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
-
getChildText
Returns the text value of a given child element.- Parameters:
parent- the Element which contains the childchildName- 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
Changes the tag name of an element.- Parameters:
elem- Element which name should be changednewName- new tag name of the element- Returns:
- true if the name was changed successfully or false otherwise
-