Package gov.usgs.util
Class XmlUtils
- java.lang.Object
-
- gov.usgs.util.XmlUtils
-
public class XmlUtils extends java.lang.Object
Xml parsing utility functions.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.Map<java.lang.String,java.lang.String>
ESCAPES
Hashmap of ESCAPES
-
Constructor Summary
Constructors Constructor Description XmlUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
escape(java.lang.String value)
Escape a value when writing XML.static java.lang.String
formatDate(java.util.Date date)
Convenience method to format a Date as an XML DateTime String.static java.lang.String
formatGregorianCalendar(java.util.GregorianCalendar calendar)
Format a Gregorian Calendar as an XML DateTime String.static java.lang.String
getAttribute(org.xml.sax.Attributes attributes, java.lang.String uri, java.lang.String localName)
Sometimes parsers do not preserve the namespace for attributes.static java.util.Date
getDate(java.lang.String toParse)
Convenience method to parse an XML Date Time into a Date.static java.util.Date
getDate(javax.xml.datatype.XMLGregorianCalendar xmlDate)
Converts an XMLGregorianCalendar to a Date.static javax.xml.datatype.XMLGregorianCalendar
getXMLGregorianCalendar(java.lang.String toParse)
Parse an XML Date Time into an XMLGregorianCalendar.static void
parse(java.lang.Object xml, org.xml.sax.helpers.DefaultHandler handler)
Creates an XMLReader and uses handler as a content and error handler.static java.lang.String
unescape(java.lang.String value)
Unescape a value when reading XML.
-
-
-
Method Detail
-
formatDate
public static java.lang.String formatDate(java.util.Date date)
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
public static java.lang.String formatGregorianCalendar(java.util.GregorianCalendar calendar)
Format a Gregorian Calendar as an XML DateTime String.- Parameters:
calendar
- the calendar to format.- Returns:
- the XML representation as a string.
-
getDate
public static java.util.Date getDate(java.lang.String toParse)
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
public static javax.xml.datatype.XMLGregorianCalendar getXMLGregorianCalendar(java.lang.String toParse)
Parse an XML Date Time into an XMLGregorianCalendar.- Parameters:
toParse
- the xml date time string to parse.- Returns:
- the parsed XMLGregorianCalendar object.
-
getDate
public static java.util.Date getDate(javax.xml.datatype.XMLGregorianCalendar xmlDate)
Converts an XMLGregorianCalendar to a Date.- Parameters:
xmlDate
- XMLGregorianCalendar to convert.- Returns:
- corresponding date object.
-
parse
public static void parse(java.lang.Object xml, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationException
Creates an XMLReader and uses handler as a content and error handler.- Parameters:
xml
- source of xml.handler
- SAX handler for xml.- Throws:
org.xml.sax.SAXException
- if any exceptions occur during parsing.java.io.IOException
- if unable to convert xml to an inputstream.javax.xml.parsers.ParserConfigurationException
- if unable to create a namespace aware parser.
-
getAttribute
public static java.lang.String getAttribute(org.xml.sax.Attributes attributes, java.lang.String uri, java.lang.String localName)
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
public static java.lang.String escape(java.lang.String value)
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
public static java.lang.String unescape(java.lang.String value)
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.
-
-