Package gov.usgs.util
Class XmlUtils
java.lang.Object
gov.usgs.util.XmlUtils
Xml parsing utility functions.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
Escape a value when writing XML.static String
formatDate
(Date date) Convenience method to format a Date as an XML DateTime String.static String
formatGregorianCalendar
(GregorianCalendar calendar) Format a Gregorian Calendar as an XML DateTime String.static String
getAttribute
(Attributes attributes, String uri, String localName) Sometimes parsers do not preserve the namespace for attributes.static Date
Convenience method to parse an XML Date Time into a Date.static Date
getDate
(XMLGregorianCalendar xmlDate) Converts an XMLGregorianCalendar to a Date.static XMLGregorianCalendar
getXMLGregorianCalendar
(String toParse) Parse an XML Date Time into an XMLGregorianCalendar.static void
parse
(Object xml, DefaultHandler handler) Creates an XMLReader and uses handler as a content and error handler.static String
Unescape a value when reading XML.
-
Field Details
-
ESCAPES
Hashmap of ESCAPES
-
-
Constructor Details
-
XmlUtils
public XmlUtils()
-
-
Method Details
-
formatDate
Convenience method to format a Date as an XML DateTime String.- Parameters:
date
- the date to format.- Returns:
- the XML representation as a string.
-
formatGregorianCalendar
Format a Gregorian Calendar as an XML DateTime String.- Parameters:
calendar
- the calendar to format.- Returns:
- the XML representation as a string.
-
getDate
Convenience method to parse an XML Date Time into a Date. Only useful when the XML Date Time is within the Date object time range.- Parameters:
toParse
- the xml date time string to parse.- Returns:
- the parsed Date object.
-
getXMLGregorianCalendar
Parse an XML Date Time into an XMLGregorianCalendar.- Parameters:
toParse
- the xml date time string to parse.- Returns:
- the parsed XMLGregorianCalendar object.
-
getDate
Converts an XMLGregorianCalendar to a Date.- Parameters:
xmlDate
- XMLGregorianCalendar to convert.- Returns:
- corresponding date object.
-
parse
public static void parse(Object xml, DefaultHandler handler) throws SAXException, IOException, ParserConfigurationException Creates an XMLReader and uses handler as a content and error handler.- Parameters:
xml
- source of xml.handler
- SAX handler for xml.- Throws:
SAXException
- if any exceptions occur during parsing.IOException
- if unable to convert xml to an inputstream.ParserConfigurationException
- if unable to create a namespace aware parser.
-
getAttribute
Sometimes parsers do not preserve the namespace for attributes. This attempts to use the namespace and localname, and, if not available using the namespace, checks for an attribute using only localname.- Parameters:
attributes
- Attributes object to search.uri
- namespace of attribute.localName
- local name of attribute.- Returns:
- value of attribute.
-
escape
Escape a value when writing XML. Replaces each character in the ESCAPES map with its escaped value. This method should only be used when generating xml manually, since most xml writers escape automatically.- Parameters:
value
- the value to escape- Returns:
- the escaped value.
-
unescape
Unescape a value when reading XML. Replaces each escaped character in the ESCAPES map with its unescaped value. This method should only be used when parsing xml manually, since most xml parsers unescape automatically.- Parameters:
value
- the value to unescape- Returns:
- the unescaped value.
-