Package groovy.xml
Class XmlUtil
java.lang.Object
groovy.xml.XmlUtil
Used for pretty printing XML content and other XML related utilities.
The serialization helpers accept DOM nodes, Groovy XML trees,
GPathResult,
Writable, and raw XML text.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringEscape control characters (below 0x20) with their XML entities, e.g.static StringEscape the following characters" ' & < >with their XML entities, e.g.Streams XML events from the suppliedReaderusing a hardened StAXXMLInputFactory.Streams XML events from the suppliedReaderusing a hardened StAXXMLInputFactory.static SAXParsernewSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, boolean allowDoctypeDecl, Source... 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.static SAXParsernewSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, File 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.static SAXParsernewSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, URL schema) Factory method to create a SAXParser configured to validate according to a particular schema language and a URL pointing to the schema to validate against.static SAXParsernewSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, Source... 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.static SAXParsernewSAXParser(String schemaLanguage, File 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.static SAXParsernewSAXParser(String schemaLanguage, URL schema) Factory method to create a SAXParser configured to validate according to a particular schema language and a URL pointing to the schema to validate against.static SAXParsernewSAXParser(String schemaLanguage, Source... 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.static StringReturn a pretty String version of the XML content produced by the Writable.static Stringserialize(Writable writable, SerializeOptions options) Return a pretty String version of the XML content produced by the Writable using the specified options.static voidserialize(Writable writable, OutputStream os) Write a pretty version of the XML content produced by the Writable to the OutputStream.static voidserialize(Writable writable, OutputStream os, SerializeOptions options) Write a pretty version of the XML content produced by the Writable to the OutputStream using the specified options.static voidWrite a pretty version of the XML content produced by the Writable to the Writer.static StringReturn a pretty String version of the Node.static Stringserialize(Node node, SerializeOptions options) Return a pretty String version of the Node using the specified options.static voidserialize(Node node, OutputStream os) Write a pretty version of the Node to the OutputStream.static voidserialize(Node node, OutputStream os, SerializeOptions options) Write a pretty version of the Node to the OutputStream using the specified options.static voidWrite a pretty version of the Node to the Writer.static Stringserialize(GPathResult node) Return a pretty version of the GPathResult.static Stringserialize(GPathResult node, SerializeOptions options) Return a pretty String version of the GPathResult using the specified options.static voidserialize(GPathResult node, OutputStream os) Write a pretty version of the GPathResult to the OutputStream.static voidserialize(GPathResult node, OutputStream os, SerializeOptions options) Write a pretty version of the GPathResult to the OutputStream using the specified options.static voidserialize(GPathResult node, Writer w) Write a pretty version of the GPathResult to the Writer.static StringReturn a pretty version of the XML content contained in the given String.static StringReturn a pretty version of the XML content contained in the given String.static Stringserialize(String xmlString, SerializeOptions options) Return a pretty version of the XML content contained in the given String using the specified options.static voidserialize(String xmlString, OutputStream os) Write a pretty version of the given XML string to the OutputStream.static voidserialize(String xmlString, OutputStream os, boolean allowDocTypeDeclaration) Write a pretty version of the given XML string to the OutputStream.static voidserialize(String xmlString, OutputStream os, SerializeOptions options) Write a pretty version of the given XML string to the OutputStream using the specified options.static voidWrite a pretty version of the given XML string to the Writer.static voidWrite a pretty version of the given XML string to the Writer.static StringReturn a pretty String version of the Element.static StringReturn a pretty String version of the Element.static Stringserialize(Element element, SerializeOptions options) Return a pretty String version of the Element using the specified options.static voidserialize(Element element, OutputStream os) Write a pretty version of the Element to the OutputStream.static voidserialize(Element element, OutputStream os, boolean allowDocTypeDeclaration) Write a pretty version of the Element to the OutputStream.static voidserialize(Element element, OutputStream os, SerializeOptions options) Write a pretty version of the Element to the OutputStream using the specified options.static voidWrite a pretty version of the Element to the Writer.static voidWrite a pretty version of the Element to the Writer.static voidsetFeatureQuietly(DocumentBuilderFactory factory, String feature, boolean value) Attempts to set a feature on the document builder factory and ignores unsupported features.static voidsetFeatureQuietly(SAXParserFactory factory, String feature, boolean value) Attempts to set a feature on the SAX parser factory and ignores unsupported features.static voidsetFeatureQuietly(TransformerFactory factory, String feature, boolean value) Attempts to set a feature on the transformer factory and ignores unsupported features.streamElements(Reader reader, String localName) Streams matching subtrees from a (potentially very large) XML document as DOMNodes.streamElements(Reader reader, String namespaceURI, String localName) Streams matching subtrees from a (potentially very large) XML document as DOMNodes.streamElements(Reader reader, String namespaceURI, String localName, boolean allowDocTypeDeclaration) Streams matching subtrees from a (potentially very large) XML document as DOMNodes.
-
Constructor Details
-
XmlUtil
public XmlUtil()
-
-
Method Details
-
serialize
Return a pretty String version of the Element.- Parameters:
element- the Element to serialize- Returns:
- the pretty String representation of the Element
-
serialize
Return a pretty String version of the Element.- Parameters:
element- the Element to serializeallowDocTypeDeclaration- whether to allow doctype processing- Returns:
- the pretty String representation of the Element
-
serialize
Write a pretty version of the Element to the OutputStream.- Parameters:
element- the Element to serializeos- the OutputStream to write to
-
serialize
Write a pretty version of the Element to the OutputStream.- Parameters:
element- the Element to serializeos- the OutputStream to write toallowDocTypeDeclaration- whether to allow doctype processing
-
serialize
Write a pretty version of the Element to the Writer.- Parameters:
element- the Element to serializew- the Writer to write to
-
serialize
Write a pretty version of the Element to the Writer.- Parameters:
element- the Element to serializew- the Writer to write toallowDocTypeDeclaration- whether to allow doctype processing
-
serialize
Return a pretty String version of the Node.- Parameters:
node- the Node to serialize- Returns:
- the pretty String representation of the Node
-
serialize
Write a pretty version of the Node to the OutputStream.- Parameters:
node- the Node to serializeos- the OutputStream to write to
-
serialize
Write a pretty version of the Node to the Writer.- Parameters:
node- the Node to serializew- the Writer to write to
-
serialize
Return a pretty version of the GPathResult.- Parameters:
node- a GPathResult to serialize to a String- Returns:
- the pretty String representation of the GPathResult
-
serialize
Write a pretty version of the GPathResult to the OutputStream.- Parameters:
node- a GPathResult to serializeos- the OutputStream to write to
-
serialize
Write a pretty version of the GPathResult to the Writer.- Parameters:
node- a GPathResult to serializew- the Writer to write to
-
serialize
Return a pretty String version of the XML content produced by the Writable.- Parameters:
writable- the Writable to serialize- Returns:
- the pretty String representation of the content from the Writable
-
serialize
Write a pretty version of the XML content produced by the Writable to the OutputStream.- Parameters:
writable- the Writable to serializeos- the OutputStream to write to
-
serialize
Write a pretty version of the XML content produced by the Writable to the Writer.- Parameters:
writable- the Writable to serializew- the Writer to write to
-
serialize
Return a pretty version of the XML content contained in the given String.- Parameters:
xmlString- the String to serialize- Returns:
- the pretty String representation of the original content
-
serialize
Return a pretty version of the XML content contained in the given String.- Parameters:
xmlString- the String to serializeallowDocTypeDeclaration- whether to allow doctype processing- Returns:
- the pretty String representation of the original content
-
serialize
Write a pretty version of the given XML string to the OutputStream.- Parameters:
xmlString- the String to serializeos- the OutputStream to write to
-
serialize
Write a pretty version of the given XML string to the OutputStream.- Parameters:
xmlString- the String to serializeallowDocTypeDeclaration- whether to allow doctype processingos- the OutputStream to write to
-
serialize
Write a pretty version of the given XML string to the Writer.- Parameters:
xmlString- the String to serializew- the Writer to write to
-
serialize
Write a pretty version of the given XML string to the Writer.- Parameters:
xmlString- the String to serializeallowDocTypeDeclaration- whether to allow doctype processingw- the Writer to write to
-
serialize
Return a pretty String version of the Element using the specified options.- Parameters:
element- the Element to serializeoptions- the serialization options- Returns:
- the pretty String representation of the Element
- Since:
- 6.0.0
-
serialize
Write a pretty version of the Element to the OutputStream using the specified options.- Parameters:
element- the Element to serializeos- the OutputStream to write tooptions- the serialization options- Since:
- 6.0.0
-
serialize
Return a pretty String version of the Node using the specified options.- Parameters:
node- the Node to serializeoptions- the serialization options- Returns:
- the pretty String representation of the Node
- Since:
- 6.0.0
-
serialize
Write a pretty version of the Node to the OutputStream using the specified options.- Parameters:
node- the Node to serializeos- the OutputStream to write tooptions- the serialization options- Since:
- 6.0.0
-
serialize
Return a pretty String version of the GPathResult using the specified options.- Parameters:
node- a GPathResult to serialize to a Stringoptions- the serialization options- Returns:
- the pretty String representation of the GPathResult
- Since:
- 6.0.0
-
serialize
Write a pretty version of the GPathResult to the OutputStream using the specified options.- Parameters:
node- a GPathResult to serializeos- the OutputStream to write tooptions- the serialization options- Since:
- 6.0.0
-
serialize
Return a pretty String version of the XML content produced by the Writable using the specified options.- Parameters:
writable- the Writable to serializeoptions- the serialization options- Returns:
- the pretty String representation of the content from the Writable
- Since:
- 6.0.0
-
serialize
Write a pretty version of the XML content produced by the Writable to the OutputStream using the specified options.- Parameters:
writable- the Writable to serializeos- the OutputStream to write tooptions- the serialization options- Since:
- 6.0.0
-
serialize
Return a pretty version of the XML content contained in the given String using the specified options.- Parameters:
xmlString- the String to serializeoptions- the serialization options- Returns:
- the pretty String representation of the original content
- Since:
- 6.0.0
-
serialize
Write a pretty version of the given XML string to the OutputStream using the specified options.- Parameters:
xmlString- the String to serializeos- the OutputStream to write tooptions- the serialization options- Since:
- 6.0.0
-
newSAXParser
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- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)schemas- the schemas to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
- See Also:
-
newSAXParser
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- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)namespaceAware- will the parser be namespace awarevalidating- will the parser also validate against DTDsschemas- the schemas to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
-
newSAXParser
public static SAXParser newSAXParser(String schemaLanguage, boolean namespaceAware, boolean validating, boolean allowDoctypeDecl, 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- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)namespaceAware- will the parser be namespace awarevalidating- will the parser also validate against DTDsallowDoctypeDecl- whether to allow doctype declarations (potentially insecure)schemas- the schemas to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 3.0.11
-
newSAXParser
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- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)schema- a file containing the schema to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
- See Also:
-
newSAXParser
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- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)namespaceAware- will the parser be namespace awarevalidating- will the parser also validate against DTDsschema- a file containing the schema to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
-
newSAXParser
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 a URL pointing to the schema to validate against. The created SAXParser will be namespace-aware and not validate against DTDs.- Parameters:
schemaLanguage- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)schema- a URL pointing to the schema to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
- See Also:
-
newSAXParser
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 a URL pointing to the schema to validate against.- Parameters:
schemaLanguage- the schema language used, e.g. XML Schema or RelaxNG (as per the String representation in javax.xml.XMLConstants)namespaceAware- will the parser be namespace awarevalidating- will the parser also validate against DTDsschema- a URL pointing to the schema to validate against- Returns:
- the created SAXParser
- Throws:
SAXExceptionParserConfigurationException- Since:
- 1.8.7
-
escapeXml
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- the original String- Returns:
- A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
- See Also:
-
escapeControlCharacters
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- the original String- Returns:
- A new string in which all characters that require escaping have been replaced with the corresponding XML entities.
- See Also:
-
setFeatureQuietly
Attempts to set a feature on the transformer factory and ignores unsupported features.- Parameters:
factory- the transformer factory to configurefeature- the fully qualified JAXP feature URIvalue- the value to apply
-
setFeatureQuietly
Attempts to set a feature on the document builder factory and ignores unsupported features.- Parameters:
factory- the document builder factory to configurefeature- the fully qualified JAXP feature URIvalue- the value to apply
-
setFeatureQuietly
Attempts to set a feature on the SAX parser factory and ignores unsupported features.- Parameters:
factory- the SAX parser factory to configurefeature- the fully qualified JAXP feature URIvalue- the value to apply
-
events
Streams XML events from the suppliedReaderusing a hardened StAXXMLInputFactory. The returned stream lazily pulls events; closing the stream closes the underlying reader.Equivalent to
events(reader, false).- Parameters:
reader- the XML source- Returns:
- a stream of
XMLEvents - Since:
- 6.0.0
-
events
Streams XML events from the suppliedReaderusing a hardened StAXXMLInputFactory. The returned stream lazily pulls events; closing the stream closes the underlying reader.- Parameters:
reader- the XML sourceallowDocTypeDeclaration- whether DOCTYPE declarations are permitted- Returns:
- a stream of
XMLEvents - Since:
- 6.0.0
-
streamElements
Streams matching subtrees from a (potentially very large) XML document as DOMNodes. Each emitted node is the root of one complete element matchinglocalName; matching is performed on local name only (any namespace).Equivalent to
streamElements(reader, null, localName, false).Closing the returned stream closes the underlying reader. For parallel per-subtree processing, sink the stream into a virtual-thread executor (JDK 21+).
- Parameters:
reader- the XML sourcelocalName- the element local name to match- Returns:
- a stream of DOM nodes, one per matching subtree
- Since:
- 6.0.0
-
streamElements
Streams matching subtrees from a (potentially very large) XML document as DOMNodes. Matching is namespace-qualified.- Parameters:
reader- the XML sourcenamespaceURI- the namespace URI to match (usenullto match any)localName- the element local name to match- Returns:
- a stream of DOM nodes, one per matching subtree
- Since:
- 6.0.0
-
streamElements
public static Stream<Node> streamElements(Reader reader, String namespaceURI, String localName, boolean allowDocTypeDeclaration) Streams matching subtrees from a (potentially very large) XML document as DOMNodes. Matching is namespace-qualified.- Parameters:
reader- the XML sourcenamespaceURI- the namespace URI to match (usenullto match any)localName- the element local name to matchallowDocTypeDeclaration- whether DOCTYPE declarations are permitted- Returns:
- a stream of DOM nodes, one per matching subtree
- Since:
- 6.0.0
-