Top Description Constructors Methods
groovy.xml

public Class XmlUtil

extends Object
Class Inheritance
Imports
groovy.lang.Closure, .GroovyRuntimeException, .Writable, groovy.util.Node, groovy.xml.slurpersupport.GPathResult, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.runtime.InvokerHelper, .StringGroovyMethods, org.w3c.dom.Element, org.xml.sax.SAXException, javax.xml.parsers.ParserConfigurationException, .SAXParser, .SAXParserFactory, javax.xml.transform.OutputKeys, .Source, .Transformer, .TransformerException, .TransformerFactory, javax.xml.transform.dom.DOMSource, javax.xml.transform.stream.StreamResult, .StreamSource, javax.xml.validation.Schema, .SchemaFactory, java.io.File, .IOException, .OutputStream, .OutputStreamWriter, .PrintWriter, .StringReader, .Writer, java.net.URL, java.nio.charset.StandardCharsets

Used for pretty printing XML content and other XML related utilities.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static StreamSource
private static String
asString(Node node)

private static String
private static String
asString(Writable writable)

private static String
public static String

Returns:

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
escapeControlCharacters
(String
the original String
orig
)

Escape control characters (below 0x20) with their XML entities, e.g. carriage return (Ctrl-M or \r) becomes Notes:

  • Does not escape non-ascii characters above 0x7e
  • Does not treat surrogate pairs specially
  • Does not perform Unicode validation on its input
public static String

Returns:

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
escapeXml
(String
the original String
orig
)

Escape the following characters " ' & < > with their XML entities, e.g. "bread" & "butter" becomes "bread" & "butter".

private static boolean
public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
Source...
the schemas to validate against
schemas
)

Factory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with.

public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
boolean
will the parser be namespace aware
namespaceAware
,
boolean
will the parser also validate against DTDs
validating
,
Source...
the schemas to validate against
schemas
)

Factory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with.

public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
File
a file containing the schema to validate against
schema
)

Factory method to create a SAXParser configured to validate according to a particular schema language and a File containing the schema to validate against.

public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
boolean
will the parser be namespace aware
namespaceAware
,
boolean
will the parser also validate against DTDs
validating
,
File
a file containing the schema to validate against
schema
)

Factory method to create a SAXParser configured to validate according to a particular schema language and a File containing the schema to validate against.

public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
URL
a URL pointing to the schema to validate against
schema
)

Factory method to create a SAXParser configured to validate according to a particular schema language and an URL pointing to the schema to validate against.

public static SAXParser

Returns:

the created SAXParser
newSAXParser
(String
the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)
schemaLanguage
,
boolean
will the parser be namespace aware
namespaceAware
,
boolean
will the parser also validate against DTDs
validating
,
URL
a URL pointing to the schema to validate against
schema
)

Factory method to create a SAXParser configured to validate according to a particular schema language and an URL pointing to the schema to validate against.

private static SAXParser
newSAXParser(boolean namespaceAware, boolean validating, Schema schema1)

public static String

Returns:

the pretty String representation of the Element
serialize
(Element
the Element to serialize
element
)

Return a pretty String version of the Element.

public static void
serialize(Element
the Element to serialize
element
,
OutputStream
the OutputStream to write to
os
)

Write a pretty version of the Element to the OutputStream.

public static void
serialize(Element
the Element to serialize
element
,
Writer
the Writer to write to
w
)

Write a pretty version of the Element to the Writer.

public static String

Returns:

the pretty String representation of the Node
serialize
(Node
the Node to serialize
node
)

Return a pretty String version of the Node.

public static void
serialize(Node
the Node to serialize
node
,
OutputStream
the OutputStream to write to
os
)

Write a pretty version of the Node to the OutputStream.

public static void
serialize(Node
the Node to serialize
node
,
Writer
the Writer to write to
w
)

Write a pretty version of the Node to the Writer.

public static String

Returns:

the pretty String representation of the GPathResult
serialize
(GPathResult
a GPathResult to serialize to a String
node
)

Return a pretty version of the GPathResult.

public static void
serialize(GPathResult
a GPathResult to serialize
node
,
OutputStream
the OutputStream to write to
os
)

Write a pretty version of the GPathResult to the OutputStream.

public static void
serialize(GPathResult
a GPathResult to serialize
node
,
Writer
the Writer to write to
w
)

Write a pretty version of the GPathResult to the Writer.

public static String

Returns:

the pretty String representation of the content from the Writable
serialize
(Writable
the Writable to serialize
writable
)

Return a pretty String version of the XML content produced by the Writable.

public static void
serialize(Writable
the Writable to serialize
writable
,
OutputStream
the OutputStream to write to
os
)

Write a pretty version of the XML content produced by the Writable to the OutputStream.

public static void
serialize(Writable
the Writable to serialize
writable
,
Writer
the Writer to write to
w
)

Write a pretty version of the XML content produced by the Writable to the Writer.

public static String

Returns:

the pretty String representation of the original content
serialize
(String
the String to serialize
xmlString
)

Return a pretty version of the XML content contained in the given String.

public static void
serialize(String
the String to serialize
xmlString
,
OutputStream
the OutputStream to write to
os
)

Write a pretty version of the given XML string to the OutputStream.

public static void
serialize(String
the String to serialize
xmlString
,
Writer
the Writer to write to
w
)

Write a pretty version of the given XML string to the Writer.

private static void
private static void
serialize(Source source, Writer w)

private static void
serialize(Source source, StreamResult target)

private static void
setIndent(TransformerFactory factory, int indent)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

XmlUtilback to summary
public XmlUtil()

Method Detail

asStreamSourceback to summary
private static StreamSource asStreamSource(String xmlString)
asStringback to summary
private static String asString(Node node)
asStringback to summary
private static String asString(GPathResult node)
asStringback to summary
private static String asString(Writable writable)
asStringAdjustedback to summary
private static String asStringAdjusted(Object node)
escapeControlCharactersback to summary
public static String escapeControlCharacters(String orig)

Escape control characters (below 0x20) with their XML entities, e.g. carriage return (Ctrl-M or \r) becomes Notes:

  • Does not escape non-ascii characters above 0x7e
  • Does not treat surrogate pairs specially
  • Does not perform Unicode validation on its input
Parameters
orig:String

the original String

Returns:String

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.

See Also
escapeXml(String)
escapeXmlback to summary
public static String escapeXml(String orig)

Escape the following characters " ' & < > with their XML entities, e.g. "bread" & "butter" becomes "bread" & "butter". Notes:

  • Supports only the five basic XML entities (gt, lt, quot, amp, apos)
  • Does not escape control characters
  • Does not support DTDs or external entities
  • Does not treat surrogate pairs specially
  • Does not perform Unicode validation on its input
Parameters
orig:String

the original String

Returns:String

A new string in which all characters that require escaping have been replaced with the corresponding XML entities.

See Also
escapeControlCharacters(String)
isOrExtendsLegacyGPathResultback to summary
private static boolean isOrExtendsLegacyGPathResult(Class<T> candidate)
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, Source... schemas) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with. The created SAXParser will be namespace-aware and not validate against DTDs.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

schemas:Source[]

the schemas to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
See Also
newSAXParser(String, boolean, boolean, Source...)
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, Source... schemas) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

namespaceAware:boolean

will the parser be namespace aware

validating:boolean

will the parser also validate against DTDs

schemas:Source[]

the schemas to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, File schema) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and a File containing the schema to validate against. The created SAXParser will be namespace-aware and not validate against DTDs.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

schema:File

a file containing the schema to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
See Also
newSAXParser(String, boolean, boolean, File)
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, File schema) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and a File containing the schema to validate against.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

namespaceAware:boolean

will the parser be namespace aware

validating:boolean

will the parser also validate against DTDs

schema:File

a file containing the schema to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, URL schema) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and an URL pointing to the schema to validate against. The created SAXParser will be namespace-aware and not validate against DTDs.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

schema:URL

a URL pointing to the schema to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
See Also
newSAXParser(String, boolean, boolean, URL)
newSAXParserback to summary
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, URL schema) throws SAXException, ParserConfigurationException

Factory method to create a SAXParser configured to validate according to a particular schema language and an URL pointing to the schema to validate against.

Parameters
schemaLanguage:String

the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)

namespaceAware:boolean

will the parser be namespace aware

validating:boolean

will the parser also validate against DTDs

schema:URL

a URL pointing to the schema to validate against

Returns:SAXParser

the created SAXParser

Since
1.8.7
newSAXParserback to summary
private static SAXParser newSAXParser(boolean namespaceAware, boolean validating, Schema schema1) throws ParserConfigurationException, SAXException
serializeback to summary
public static String serialize(Element element)

Return a pretty String version of the Element.

Parameters
element:Element

the Element to serialize

Returns:String

the pretty String representation of the Element

serializeback to summary
public static void serialize(Element element, OutputStream os)

Write a pretty version of the Element to the OutputStream.

Parameters
element:Element

the Element to serialize

os:OutputStream

the OutputStream to write to

serializeback to summary
public static void serialize(Element element, Writer w)

Write a pretty version of the Element to the Writer.

Parameters
element:Element

the Element to serialize

w:Writer

the Writer to write to

serializeback to summary
public static String serialize(Node node)

Return a pretty String version of the Node.

Parameters
node:Node

the Node to serialize

Returns:String

the pretty String representation of the Node

serializeback to summary
public static void serialize(Node node, OutputStream os)

Write a pretty version of the Node to the OutputStream.

Parameters
node:Node

the Node to serialize

os:OutputStream

the OutputStream to write to

serializeback to summary
public static void serialize(Node node, Writer w)

Write a pretty version of the Node to the Writer.

Parameters
node:Node

the Node to serialize

w:Writer

the Writer to write to

serializeback to summary
public static String serialize(GPathResult node)

Return a pretty version of the GPathResult.

Parameters
node:GPathResult

a GPathResult to serialize to a String

Returns:String

the pretty String representation of the GPathResult

serializeback to summary
public static void serialize(GPathResult node, OutputStream os)

Write a pretty version of the GPathResult to the OutputStream.

Parameters
node:GPathResult

a GPathResult to serialize

os:OutputStream

the OutputStream to write to

serializeback to summary
public static void serialize(GPathResult node, Writer w)

Write a pretty version of the GPathResult to the Writer.

Parameters
node:GPathResult

a GPathResult to serialize

w:Writer

the Writer to write to

serializeback to summary
public static String serialize(Writable writable)

Return a pretty String version of the XML content produced by the Writable.

Parameters
writable:Writable

the Writable to serialize

Returns:String

the pretty String representation of the content from the Writable

serializeback to summary
public static void serialize(Writable writable, OutputStream os)

Write a pretty version of the XML content produced by the Writable to the OutputStream.

Parameters
writable:Writable

the Writable to serialize

os:OutputStream

the OutputStream to write to

serializeback to summary
public static void serialize(Writable writable, Writer w)

Write a pretty version of the XML content produced by the Writable to the Writer.

Parameters
writable:Writable

the Writable to serialize

w:Writer

the Writer to write to

serializeback to summary
public static String serialize(String xmlString)

Return a pretty version of the XML content contained in the given String.

Parameters
xmlString:String

the String to serialize

Returns:String

the pretty String representation of the original content

serializeback to summary
public static void serialize(String xmlString, OutputStream os)

Write a pretty version of the given XML string to the OutputStream.

Parameters
xmlString:String

the String to serialize

os:OutputStream

the OutputStream to write to

serializeback to summary
public static void serialize(String xmlString, Writer w)

Write a pretty version of the given XML string to the Writer.

Parameters
xmlString:String

the String to serialize

w:Writer

the Writer to write to

serializeback to summary
private static void serialize(Source source, OutputStream os)
serializeback to summary
private static void serialize(Source source, Writer w)
serializeback to summary
private static void serialize(Source source, StreamResult target)
setIndentback to summary
private static void setIndent(TransformerFactory factory, int indent)