public final class XmlUtil extends Object
Modifier and Type | Method and Description |
---|---|
static void |
addAttribute(Element element,
String attributeName,
Object value)
Add the specified attribute to the given element.
|
static void |
addElement(Element parentElement,
Element element)
Add the specified element to the end of the list of children of the
given parent element,
|
static Element |
addElement(Element parentElement,
String elementName)
Add an empty element to the specified parent element.
|
static Element |
addElement(Element parentElement,
String elementName,
Object elementValue)
Add an element with text content to the specified parent element.
|
static Element |
clone(Element element)
Return a deep clone of the specified element.
|
static Element |
findChild(Element element,
String childName)
Return a specific child element from the given element.
|
static List<Element> |
findChildren(Element element)
Return all children (full depth) of the specified element
in document order.
|
static List<Element> |
findChildren(Element element,
String childName)
Return all children elements of the given name from the specified element.
|
static Element |
findLastChild(Element element,
String childName)
Return a specific child element from the given element.
|
static String |
getAttribute(Element element,
String attributeName)
Return the attribute of a specified element as a string.
|
static Boolean |
getAttribute(Element element,
String attributeName,
Boolean defaultValue)
Return the attribute of a specific element as a boolean.
|
static Color |
getAttribute(Element element,
String attributeName,
Color defaultValue)
Return the attribute of a specific element as a color.
|
static Double |
getAttribute(Element element,
String attributeName,
Double defaultValue)
Return the attribute of a specific element as a long.
|
static Integer |
getAttribute(Element element,
String attributeName,
Integer defaultValue)
Return the attribute of a specified element as an integer.
|
static Long |
getAttribute(Element element,
String attributeName,
Long defaultValue)
Return the attribute of a specific element as a long.
|
static String |
getAttribute(Element element,
String attributeName,
Object defaultValue)
Return the attribute of a specific element as a string.
|
static String |
getAttribute(Element element,
String attributeName,
String defaultValue)
Return the attribute of a specified element as a string.
|
static Element |
getChild(Element element,
String childName)
Return a specified child element from the given element.
|
static String |
getChildAttribute(Element rootElement,
String elementName,
String attributeName)
Return the attribute of a specified child element as a string.
|
static Color |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
Color defaultValue)
Return the attribute of a specified element as a color.
|
static Double |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
Double defaultValue)
Return the attribute of a specified element as a double.
|
static Integer |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
Integer defaultValue)
Return the attribute of a specified element as an integer.
|
static Long |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
Long defaultValue)
Return the attribute of a specified element as a long integer.
|
static String |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
Object defaultValue)
Return the attribute of a specified element as a string.
|
static String |
getChildAttribute(Element rootElement,
String elementName,
String attributeName,
String defaultValue)
Return the attribute of a specified child element as a string.
|
static List<Element> |
getChildren(Element element)
Return all intermediate element children of the specified node.
|
static List<Element> |
getChildren(Element element,
String childName)
Return all intermediate children of the specified name of the
given element node.
|
static String |
getChildValue(Element element,
String childName)
Return the text content of the child of the specified element.
|
static Boolean |
getChildValue(Element element,
String childName,
Boolean defaultValue)
Return the text content of the child of the specified element as a boolean.
|
static <T extends Enum<?>> |
getChildValue(Element element,
String childName,
Class<?> enumClass,
T defaultValue)
Return the text content of the child of the specified element as an enumeration.
|
static Color |
getChildValue(Element element,
String childName,
Color defaultValue)
Return the text content of the child of the specified element as a color.
|
static Date |
getChildValue(Element element,
String childName,
Date defaultValue)
Return the text content of the child of the specified element as an
ISO 8601 date.
|
static Double |
getChildValue(Element element,
String childName,
Double defaultValue)
Return the text content of the child of the specified element as a double.
|
static Integer |
getChildValue(Element element,
String childName,
Integer defaultValue)
Return the text content of the child of the specified element as an integer.
|
static Long |
getChildValue(Element element,
String childName,
Long defaultValue)
Return the text content of the child of the specified element as a long.
|
static String |
getChildValue(Element element,
String childName,
Object defaultValue)
Return the text content of the child of the specified element as a string.
|
static Reference |
getChildValue(Element element,
String childName,
Reference defaultValue)
Return the text content of the child of the specified element as a
WITSML Reference.
|
static String |
getChildValue(Element element,
String childName,
String defaultValue)
Return the text content of the child of the specified element.
|
static Value |
getChildValue(Element element,
String childName,
Value defaultValue)
Return the text content of the child of the specified element as a
WITSML value.
|
static Element |
getParent(Element element)
Return the parent element of the specified element.
|
static String |
getValue(Element element)
Return the value of the specified element.
|
static String |
getValue(Element element,
String defaultValue)
Return the value of the specified element.
|
static int |
indexOf(Element element,
Element childElement)
Return the index of the specified child element in the given element.
|
static void |
insertElement(Element parentElement,
Element element,
int index)
Insert an element as child of the specified parent element and in the
given index position.
|
static boolean |
isWellFormed(String xml)
Check if the specified XML is well-formed.
|
static Document |
newDocument(File file)
Create an XML document from the specified file.
|
static Document |
newDocument(String xml)
Create an XML document from the specified XML string.
|
static String |
prettyPrint(Node node)
Pretty print the specified XML sub tree.
|
static String |
prettyPrint(String xml)
Pretty print the specified XML string.
|
static void |
removeChildren(Element element)
Remove all children form the specified element.
|
static void |
removeElement(Element element)
Remove the specified element from its parent.
|
static void |
setAttribute(Element element,
String attributeName,
Object value)
Set value of the specified attribute.
|
static void |
setValue(Element element,
String value)
Set the specified value in the given element.
|
static String |
toString(Node node)
Transform an XML node (document or element typically) to its equivalent
XML string.
|
static String |
xmlEncode(String s)
Escape characters for text appearing as XML data, between tags.
|
public static String xmlEncode(String s)
The following characters are replaced with corresponding character entities:
Character | Encoding |
---|---|
< | < |
> | > |
& | & |
" | " |
' | ' |
s
- String to encode. Non-null.IllegalArgumentException
- If s is null.public static Document newDocument(String xml) throws SAXException
xml
- XML string to create document from. Non-null.IllegalArgumentException
- If xml is null.SAXException
- If the string is not a proper XML document.public static Document newDocument(File file) throws IOException, SAXException
file
- File to create document from. Non-null.IllegalArgumentException
- If file is null.IOException
- If the fail cannot be accessed for some reason.SAXException
- If the file doesn't contain a proper XML document.public static boolean isWellFormed(String xml)
xml
- XML to check. Non-null.IllegalArgumentException
- If xml is null.public static String prettyPrint(String xml)
xml
- XML string to pretty print. Non-null.IllegalArgumentException
- If xml is null.public static String prettyPrint(Node node)
node
- Root of subtree to pretty print. Typically a document
or an element.IllegalArgumentException
- If node is null.public static Element getParent(Element element)
element
- Element to get parent of. Non-null.IllegalArgumentException
- If element is null.public static List<Element> getChildren(Element element)
element
- Element to get children of. Non-nullpublic static List<Element> getChildren(Element element, String childName)
element
- Element to get children of. Non-nullchildName
- Name of children to get. Non-null.IllegalArgumenException
- if element or childName is null.public static Element clone(Element element)
element
- Element to clone. Non-nullIllegalArgumentException
- If element is null.public static void addElement(Element parentElement, Element element)
parentElement
- Element to add to. Non-null.element
- Element to add. Non-null.IllegalArgumentException
- If parentElement or element is null.public static Element addElement(Element parentElement, String elementName, Object elementValue)
parentElement
- Parent element to add to. Non-null.elementName
- Name of element to add. Non-null.elementValue
- Value of the added element. Null if empty.IllegalArgumentException
- If parentElement or elementName is null.public static Element addElement(Element parentElement, String elementName)
parentElement
- Parent element to add to. Non-null.elementName
- Name of element to add. Non-null.IllegalArgumentException
- If parentElement or elementName is null.public static void insertElement(Element parentElement, Element element, int index)
parentElement
- Element to add to. Non-null.element
- Element to insert. Non-null.index
- Insertion index. 0 as first. If index is larger than the
current number of elements, it is added last. [0,>.IllegalArgumentException
- If parentElement or element is null, or
if index < 0.public static void removeElement(Element element)
element
- Element to remove. Non-null.IllegalArgumentException
- If element is null.public static void removeChildren(Element element)
element
- Element to remove from. Non-null.IllegalArgumentException
- If element is null.public static Element getChild(Element element, String childName)
element
- Element to search. Non-null.childName
- Name of child element to find. Non-null.IllegalArgumentException
- If element or childName is null.public static Element findChild(Element element, String childName)
element
- Root element of the search. Non-null.childName
- Name of child element to find. Non-null.IllegalArgumentException
- If element or childName is null.public static Element findLastChild(Element element, String childName)
element
- Root element of the search. Non-null.childName
- Name of child element to find. Non-null.IllegalArgumentException
- If element or childName is null.public static int indexOf(Element element, Element childElement)
element
- Parent element. Non-null.childElement
- Child element ot get index of. Non-null.public static List<Element> findChildren(Element element, String childName)
element
- Element to search. Non-null.childName
- Name of child elements to find. Non-null.IllegalArgumentException
- If element or childName is null.public static List<Element> findChildren(Element element)
element
- Root element to find children of. Non-null.public static void setValue(Element element, String value)
element
- Element to set. Non-null.value
- Value to set. May be null, in case an empty string is set.IllegalArgumentException
- If element is null.public static String getValue(Element element, String defaultValue)
element
- Element to get content from. Non-null.defaultValue
- Default value to return if content is absent. May be null.IllegalArgumentException
- If element is null.public static String getValue(Element element)
element
- Element to get content from. Non-null.IllegalArgumentException
- If element is null.public static String getChildValue(Element element, String childName, String defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found. May be null.IllegalArgumentException
- If element or childName is null.public static String getChildValue(Element element, String childName)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.IllegalArgumentException
- If element or childName is null.public static Double getChildValue(Element element, String childName, Double defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found or
not of floating point format. May be null.IllegalArgumentException
- If element or childName is null.public static Integer getChildValue(Element element, String childName, Integer defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found
or not of integral format. May be null.IllegalArgumentException
- If element or childName is null.public static Long getChildValue(Element element, String childName, Long defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found
or not of integral format. May be null.IllegalArgumentException
- If element or childName is null.public static Date getChildValue(Element element, String childName, Date defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found
or not of date format. May be null.IllegalArgumentException
- If element or childName is null.public static Value getChildValue(Element element, String childName, Value defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get content of. Non-null.defaultValue
- Value to return if the child element is not found
or is not of correct type. May be null.IllegalArgumentException
- If element or childName is null.public static Reference getChildValue(Element element, String childName, Reference defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get content of. Non-null.defaultValue
- Value to return if the child element is not found
or is not of correct type. May be null.IllegalArgumentException
- If element or childName is null.public static <T extends Enum<?>> T getChildValue(Element element, String childName, Class<?> enumClass, T defaultValue)
Implementation not: This method depend on the existence of a the method
public static T get(String);Which returns an enumeration value based on a string representation of the same.
element
- Parent element of child. Non-null.childName
- Name of child element to get content of. Non-null.enumClass
- The enumeration class in question. Non-null.defaultValue
- Value to return if the child element is not found
or is not of correct type. May be null.IllegalArgumentException
- If element, childName or defaultValue is null.public static String getChildValue(Element element, String childName, Object defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get content of. Non-null.defaultValue
- Value to return if the child element is not found
or is not of correct type. May be null.IllegalArgumentException
- If element or childName is null.public static Boolean getChildValue(Element element, String childName, Boolean defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found.IllegalArgumentException
- If element or childName is null.public static Color getChildValue(Element element, String childName, Color defaultValue)
element
- Parent element of child. Non-null.childName
- Name of child element to get text content of. Non-null.defaultValue
- Value to return if the child element is not found or not
of the expected format. May be null.IllegalArgumentException
- If element or childName is null.public static void addAttribute(Element element, String attributeName, Object value)
element
- Element of attribute to set. Non-null.attributeName
- Name of attribute to set. Non-null.value
- Value to set. May be null, in case an empty string is set.public static void setAttribute(Element element, String attributeName, Object value)
element
- Element of attribute to set. Non-null.attributeName
- Name of attribute to set. Non-null.value
- Value to set. May be null, in case an empty string is set.public static String getAttribute(Element element, String attributeName, String defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found.public static String getAttribute(Element element, String attributeName)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.public static Integer getAttribute(Element element, String attributeName, Integer defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found
or not convertible to an integer. May be null.public static Long getAttribute(Element element, String attributeName, Long defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found
or not convertible to an integer. May be null.public static Boolean getAttribute(Element element, String attributeName, Boolean defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found
or not convertible to a boolean. May be null.public static Double getAttribute(Element element, String attributeName, Double defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found
or not convertible to an integer. May be null.public static String getAttribute(Element element, String attributeName, Object defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found.public static Color getAttribute(Element element, String attributeName, Color defaultValue)
element
- Element to find attribute of. Non-null.attributeName
- Name of attribute to get value of.defaultValue
- Default value to report if attribute is not found
or not convertible to an integer. May be null.public static String getChildAttribute(Element rootElement, String elementName, String attributeName, String defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static String getChildAttribute(Element rootElement, String elementName, String attributeName)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static Integer getChildAttribute(Element rootElement, String elementName, String attributeName, Integer defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static Long getChildAttribute(Element rootElement, String elementName, String attributeName, Long defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static Double getChildAttribute(Element rootElement, String elementName, String attributeName, Double defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static String getChildAttribute(Element rootElement, String elementName, String attributeName, Object defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static Color getChildAttribute(Element rootElement, String elementName, String attributeName, Color defaultValue)
rootElement
- Element to search from. Non-null. Non-null.elementName
- Name of element to look for. Non-null.attributeName
- Name of attribute to get value of. Non-null.defaultValue
- Default value to report if attribute is not found.IllegalArgumentException
- If rootElement, elementName or attributeName is null.public static String toString(Node node)
node
- Node to get as string. Non-null.IllegalArgumentException
- If node is null.Copyright © 2023 Petroware AS - https://petroware.no