Package com.tccc.kos.commons.xml
Class XmlUtil
java.lang.Object
com.tccc.kos.commons.xml.XmlUtil
A few common utility methods for working with XML.
- Since:
- 1.0
- Version:
- 2023-03-01
-
Method Summary
Modifier and TypeMethodDescriptionstatic Element
Sets a string tag value in the specified element.static Element
Sets a string tag value in the specified element.static boolean
Gets the specified XML attribute or child tag as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.static boolean
getBoolAttr
(Element el, String attr, boolean def, boolean required) Gets the specified XML attribute as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.static boolean
getBoolAttr
(Element el, String attr, boolean def, boolean required, Namespace ns) Gets the specified XML attribute as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.static boolean
getBoolTag
(Element el, String tag, boolean def, boolean required) Gets the specified XML tag as a boolean, returning the default value if the tag is not found or doesn't parse correctly.static boolean
getBoolTag
(Element el, String tag, boolean def, boolean required, Namespace ns) Gets the specified XML tag as a boolean, returning the default value if the tag is not found or doesn't parse correctly.static double
Gets the specified XML attribute or child tag as a double, returning the default value if the attribute is not found or doesn't parse correctly.static double
getDoubleAttr
(Element el, String attr, double def, boolean required) Gets the specified XML attribute as a double, returning the default value if the attribute is not found or doesn't parse correctly.static double
getDoubleAttr
(Element el, String attr, double def, boolean required, Namespace ns) Gets the specified XML attribute as a double, returning the default value if the attribute is not found or doesn't parse correctly.static double
getDoubleTag
(Element el, String tag, double def, boolean required) Gets the specified XML tag as a double, returning the default value if the tag is not found or doesn't parse correctly.static double
getDoubleTag
(Element el, String tag, double def, boolean required, Namespace ns) Gets the specified XML tag as a double, returning the default value if the tag is not found or doesn't parse correctly.static Element
getElement
(Element el, String key, boolean required) Gets the specified element.static Element
getElement
(Element el, String key, boolean required, Namespace ns) Gets the specified element.getElements
(Element el, String key, boolean required) Gets a list of elements that match the key.getElements
(Element el, String key, boolean required, Namespace ns) Gets a list of elements that match the key.static int
Gets the specified XML attribute or child tag as an int, returning the default value if the attribute is not found or doesn't parse correctly.static int
getIntAttr
(Element el, String attr, int def, boolean required) Gets the specified XML attribute as an int, returning the default value if the attribute is not found or doesn't parse correctly.static int
getIntAttr
(Element el, String attr, int def, boolean required, Namespace ns) Gets the specified XML attribute as an int, returning the default value if the attribute is not found or doesn't parse correctly.static int
Gets the specified XML tag as an int, returning the default value if the tag is not found or doesn't parse correctly.static int
Gets the specified XML tag as an int, returning the default value if the tag is not found or doesn't parse correctly.static long
Gets the specified XML attribute or child tag as a long, returning the default value if the attribute is not found or doesn't parse correctly.static long
getLongAttr
(Element el, String attr, long def, boolean required) Gets the specified XML attribute as a long, returning the default value if the attribute is not found or doesn't parse correctly.static long
getLongAttr
(Element el, String attr, long def, boolean required, Namespace ns) Gets the specified XML attribute as a long, returning the default value if the attribute is not found or doesn't parse correctly.static long
getLongTag
(Element el, String tag, long def, boolean required) Gets the specified XML tag as a long, returning the default value if the tag is not found or doesn't parse correctly.static long
getLongTag
(Element el, String tag, long def, boolean required, Namespace ns) Gets the specified XML tag as a long, returning the default value if the tag is not found or doesn't parse correctly.static String
Try to get the specified key as an attribute and then a tag.static String
getStrAttr
(Element el, String attr, String def, boolean required) Gets the specified XML attribute as a string, returning the default value if the attribute is not found.static String
getStrAttr
(Element el, String attr, String def, boolean required, Namespace ns) Gets the specified XML attribute as a string, returning the default value if the attribute is not found.static String
Gets the specified XML tag as a string, returning the default value if the tag is not found.static String
Gets the specified XML tag as a string, returning the default value if the tag is not found.static String
getTrimStr
(Element el, String key, String def, boolean required) Try to get the specified key as an attribute and then a tag.static Date
Gets the specified XML attribute or child tag as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.static Date
getUTCAttr
(Element el, String attr, long def, boolean required) Gets the specified XML attribute as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.static Date
getUTCAttr
(Element el, String attr, long def, boolean required, Namespace ns) Gets the specified XML attribute as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.static Date
Gets the specified XML tag as a UTC value and return it as a date, returning the default value if the tag is not found or doesn't parse correctly.static Element
Parses a file into an XML element.static Element
load
(InputStream is) Parses an XML stream into an XML element.static Document
loadDocument
(File file) Parses a file into an XML element.static Document
Parses an XML stream into an XML document.static Element
Parses XML stored in a string into an XML document.static void
Outputs XML to the specified file.static void
save
(Element el, OutputStream os) Wrapper for write().static void
Sets a boolean attribute value in the specified element.static void
Sets a double attribute value in the specified element.static void
Sets an int attribute value in the specified element.static void
Sets a long attribute value in the specified element.static void
Sets a string attribute value in the specified element.static void
Sets a date attribute value in the specified element.static Element
setNonEmptyTag
(Element el, String tag, String val) Sets a string tag value in the specified element.static Element
Sets a boolean tag value in the specified element.static Element
Sets a double tag value in the specified element.static Element
Sets an int tag value in the specified element.static Element
Sets a long tag value in the specified element.static Element
Sets a string tag value in the specified element.static Element
Sets a date tag value in the specified element.static String[]
This is basically a wrapper around StringTokenizer that makes it look more like string.split() which isn't available 1.3.1.static String
Outputs XML back into string form.static void
write
(Element el, OutputStream os) Outputs XML to the specified output stream.
-
Method Details
-
getStr
Try to get the specified key as an attribute and then a tag.- Parameters:
el
- the XML elementkey
- the key namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching string
-
getTrimStr
Try to get the specified key as an attribute and then a tag.- Parameters:
el
- the XML elementkey
- the key namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching trimmed string
-
getInt
Gets the specified XML attribute or child tag as an int, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementkey
- the attribute/tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching int
-
getLong
Gets the specified XML attribute or child tag as a long, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementkey
- the attribute/tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching long
-
getUTC
Gets the specified XML attribute or child tag as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementkey
- the attribute/tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching date
-
getDouble
Gets the specified XML attribute or child tag as a double, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementkey
- the attribute/tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching double
-
getBool
Gets the specified XML attribute or child tag as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementkey
- the attribute/tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching boolean
-
getElement
Gets the specified element.- Parameters:
el
- the XML elementkey
- the tag namerequired
- if true, throw an exception if not found- Returns:
- the matching element
-
getElement
Gets the specified element.- Parameters:
el
- the XML elementkey
- the tag namerequired
- if true, throw an exception if not foundns
- namespace- Returns:
- the matching element
-
getElements
Gets a list of elements that match the key.- Parameters:
el
- the XML elementkey
- the tag namerequired
- if true, throw an exception if not found- Returns:
- the matching elements
-
getElements
Gets a list of elements that match the key.- Parameters:
el
- the XML elementkey
- the tag namerequired
- if true, throw an exception if not foundns
- namespace- Returns:
- the matching elements
-
getStrAttr
public static String getStrAttr(Element el, String attr, String def, boolean required, Namespace ns) Gets the specified XML attribute as a string, returning the default value if the attribute is not found. If the required flag is set and the attribute isn't found, throw an exception.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching string
-
getIntAttr
Gets the specified XML attribute as an int, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching int
-
getLongAttr
Gets the specified XML attribute as a long, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching long
-
getUTCAttr
Gets the specified XML attribute as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching date
-
getDoubleAttr
public static double getDoubleAttr(Element el, String attr, double def, boolean required, Namespace ns) Gets the specified XML attribute as a double, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching double
-
getBoolAttr
public static boolean getBoolAttr(Element el, String attr, boolean def, boolean required, Namespace ns) Gets the specified XML attribute as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching boolean
-
getStrAttr
Gets the specified XML attribute as a string, returning the default value if the attribute is not found. If the required flag is set and the attribute isn't found, throw an exception.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching string
-
getIntAttr
Gets the specified XML attribute as an int, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching int
-
getLongAttr
Gets the specified XML attribute as a long, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching long
-
getUTCAttr
Gets the specified XML attribute as a UTC value and return it as a date, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching UTC value
-
getDoubleAttr
Gets the specified XML attribute as a double, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching double
-
getBoolAttr
Gets the specified XML attribute as a boolean, returning the default value if the attribute is not found or doesn't parse correctly.- Parameters:
el
- the XML elementattr
- the attribute namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching boolean
-
getStrTag
Gets the specified XML tag as a string, returning the default value if the tag is not found. If the required flag is set and the tag isn't found, throw an exception.- Parameters:
el
- the XML tagtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching string
-
getIntTag
Gets the specified XML tag as an int, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching int
-
getLongTag
Gets the specified XML tag as a long, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching long
-
getUTCTag
Gets the specified XML tag as a UTC value and return it as a date, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching UTC value
-
getDoubleTag
public static double getDoubleTag(Element el, String tag, double def, boolean required, Namespace ns) Gets the specified XML tag as a double, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching double
-
getBoolTag
public static boolean getBoolTag(Element el, String tag, boolean def, boolean required, Namespace ns) Gets the specified XML tag as a boolean, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not foundns
- the namespace for the tag- Returns:
- the matching boolean
-
getStrTag
Gets the specified XML tag as a string, returning the default value if the tag is not found. If the required flag is set and the tag isn't found, throw an exception.- Parameters:
el
- the XML tagtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching string
-
getIntTag
Gets the specified XML tag as an int, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching int
-
getLongTag
Gets the specified XML tag as a long, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching long
-
getDoubleTag
Gets the specified XML tag as a double, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching double
-
getBoolTag
Gets the specified XML tag as a boolean, returning the default value if the tag is not found or doesn't parse correctly.- Parameters:
el
- the XML elementtag
- the child tag namedef
- the default valuerequired
- if true, throw an exception if not found- Returns:
- the matching boolean
-
setAttr
Sets a string attribute value in the specified element. If the value is null then attribute won't be added.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
setAttr
Sets an int attribute value in the specified element.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
setAttr
Sets a long attribute value in the specified element.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
setAttr
Sets a double attribute value in the specified element.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
setAttr
Sets a boolean attribute value in the specified element.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
setAttr
Sets a date attribute value in the specified element.- Parameters:
el
- the XML element to modifyattr
- the attribute nameval
- the value to set
-
addTag
Sets a string tag value in the specified element. If the value is null then tag won't be added.- Parameters:
el
- the XML element to modifytag
- the child tag name- Returns:
- the new element for the tag
-
addTag
Sets a string tag value in the specified element. If the value is null then tag won't be added.- Parameters:
el
- the XML element to modifytag
- the child tag namens
- the namespace for the tag- Returns:
- the new element for the tag
-
setNonEmptyTag
Sets a string tag value in the specified element. If the value is null then tag won't be added.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
setTag
Sets a string tag value in the specified element. If the value is null then tag will be added regardless.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for that tag
-
setTag
Sets an int tag value in the specified element.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
setTag
Sets a long tag value in the specified element.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
setTag
Sets a double tag value in the specified element.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
setTag
Sets a boolean tag value in the specified element.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
setTag
Sets a date tag value in the specified element.- Parameters:
el
- the XML element to modifytag
- the child tag nameval
- the value to set- Returns:
- the new element for the tag
-
parse
Parses XML stored in a string into an XML document.- Parameters:
str
- the XML in string form- Returns:
- the root element for the XML
-
load
Parses a file into an XML element.- Parameters:
file
- the file to parse- Returns:
- the root element for the XML
-
load
Parses an XML stream into an XML element.- Parameters:
is
- the input stream containing XML- Returns:
- the root element for the XML
-
loadDocument
Parses a file into an XML element.- Parameters:
file
- the file to parse- Returns:
- the document for the XML
-
loadDocument
Parses an XML stream into an XML document.- Parameters:
is
- the XML stream containing XML- Returns:
- the document for the XML
-
toString
Outputs XML back into string form.- Parameters:
el
- the root element to output as a string- Returns:
- the string representation of the element
- Throws:
IOException
- if there is an error rendering the string
-
write
Outputs XML to the specified output stream.- Parameters:
el
- the root element to output as a stringos
- the stream to output to- Throws:
IOException
- if there is an error outputting to the stream
-
save
Wrapper for write().- Parameters:
el
- the element to writeos
- the output stream- Throws:
IOException
- if there is an error outputting to the stream
-
save
Outputs XML to the specified file.- Parameters:
el
- the root element to savefile
- the file to output to- Throws:
IOException
- if there is an error writing to the file
-
split
This is basically a wrapper around StringTokenizer that makes it look more like string.split() which isn't available 1.3.1.- Parameters:
str
- the string to splitdelim
- the delimiter string- Returns:
- an array of split strings
-