Package gov.usgs.util

Class XmlUtils

java.lang.Object
gov.usgs.util.XmlUtils

public class XmlUtils extends Object
Xml parsing utility functions.
  • Field Details

    • ESCAPES

      public static final Map<String,String> ESCAPES
      Hashmap of ESCAPES
  • Constructor Details

    • XmlUtils

      public XmlUtils()
  • Method Details

    • formatDate

      public static String formatDate(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 String formatGregorianCalendar(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 Date getDate(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 XMLGregorianCalendar getXMLGregorianCalendar(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 Date getDate(XMLGregorianCalendar xmlDate)
      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

      public static String getAttribute(Attributes attributes, String uri, 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 String escape(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 String unescape(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.