public class XmlUtil extends Object
Constructor and Description |
---|
XmlUtil() |
Modifier and Type | Method and Description |
---|---|
static String |
escape(String s)
Escapes the passed string, converting the five reserved XML characters
into their entities: &, <, >, ', and
".
|
static String |
formatXsdBoolean(boolean value)
Converts a
boolean value to the literal strings "true" or
"false" (XML Schema boolean fields also allow "1" or "0"). |
static String |
formatXsdDatetime(Date date)
Converts a Java Date object to a string, using the format specified by
XML Schema for
dateTime elements. |
static String |
formatXsdDecimal(double value)
Converts a Java
double to a string, using the format
specified by XML Schema for decimal elements. |
static String |
formatXsdDecimal(Number value)
Converts a Java
double to a string, using the format
specified by XML Schema for decimal elements. |
static boolean |
isLegal(String s)
Determines whether the passed string contains any illegal characters,
per section 2.2 of the XML spec.
|
static boolean |
parseXsdBoolean(String value)
Parses an XML Schema
boolean value, accepting any of
the legal formats and trimming whitespace. |
static Date |
parseXsdDatetime(String value)
Parses an XML Schema
dateTime value, accepting any of
the legal formats. |
static BigDecimal |
parseXsdDecimal(String value)
Parses an XML Schema
decimal value. |
static String |
stripIllegals(String s)
Removes all illegal characters from the passed string.
|
static String |
unescape(String s)
Unescapes the passed string, converting the five XML entities
(&, <, >, ', and ") into
their correspinding characters.
|
public static boolean isLegal(String s)
Note: at present, marks characters from the UTF-16 "surrogate blocks" as illegal. The XML specification allows code points from the "higher planes" of Unicde, but disallows the surrogate blocks used to contruct code points in these planes. Rather than allow code points from the surrogate block, and get hurt by a bozo transformer that doesn't know that Java strings are UTF-16, I took the conservative (and wrong) approach. On the other hand, if you're using characters from outside the BMP, you probably don't have ASCII control characters in your text, and don't need this method at all.
public static String stripIllegals(String s)
public static String formatXsdDatetime(Date date)
dateTime
elements. Output is UTC time, and
omits timezone specifier.public static Date parseXsdDatetime(String value) throws XmlException
dateTime
value, accepting any of
the legal formats. Note that this method can also be used to parse
a generic ISO-8601 date.XmlException
- if unable to parse.public static String formatXsdDecimal(double value)
double
to a string, using the format
specified by XML Schema for decimal
elements. This
method wraps the value and calls formatXsdDecimal(Number)
,
so call that method if you already have an object.public static String formatXsdDecimal(Number value)
double
to a string, using the format
specified by XML Schema for decimal
elements. If
passed null
, returns an empty string.public static BigDecimal parseXsdDecimal(String value)
decimal
value. Note that the return
type is BigDecimal
: this provides us exact representation
where necessary, and meets the spec's minimum requirement of 18 digits
of precision. Use .toLong()
, toDouble()
, or
equivalent to create the type that you want.public static String formatXsdBoolean(boolean value)
boolean
value to the literal strings "true" or
"false" (XML Schema boolean
fields also allow "1" or "0").public static boolean parseXsdBoolean(String value)
boolean
value, accepting any of
the legal formats and trimming whitespace.XmlException
- the passed value, after trimming, is not one
of the 4 legal representations of boolean data under XML
Schema.public static String escape(String s)
null
, returns an
empty string.
Yes, this method is available elsewhere, eg Jakarta Commons. I'm trying to minimize external dependencies from this library, so am reinventing a few small wheels (but they're round!).
public static String unescape(String s)
null
,
returns an empty string.
Yes, this method is available elsewhere, eg Jakarta Commons.