public class XmlUtil
extends Object
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.
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static String |
escapeControlCharacters(String orig)Escape control characters (below 0x20) with their XML entities, e.g. |
|
public static String |
escapeXml(String orig)Escape the following characters " ' & < > with their XML entities, e.g.
|
|
public static Stream<XMLEvent> |
events(Reader reader)Streams XML events from the supplied Reader using a hardened StAX XMLInputFactory. |
|
public static Stream<XMLEvent> |
events(Reader reader, boolean allowDocTypeDeclaration)Streams XML events from the supplied Reader using a hardened StAX XMLInputFactory. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static SAXParser |
newSAXParser(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. |
|
public static String |
serialize(Element element)Return a pretty String version of the Element. |
|
public static String |
serialize(Element element, boolean allowDocTypeDeclaration)Return a pretty String version of the Element. |
|
public static void |
serialize(Element element, OutputStream os)Write a pretty version of the Element to the OutputStream. |
|
public static void |
serialize(Element element, OutputStream os, boolean allowDocTypeDeclaration)Write a pretty version of the Element to the OutputStream. |
|
public static void |
serialize(Element element, Writer w)Write a pretty version of the Element to the Writer. |
|
public static void |
serialize(Element element, Writer w, boolean allowDocTypeDeclaration)Write a pretty version of the Element to the Writer. |
|
public static String |
serialize(Node node)Return a pretty String version of the Node. |
|
public static void |
serialize(Node node, OutputStream os)Write a pretty version of the Node to the OutputStream. |
|
public static void |
serialize(Node node, Writer w)Write a pretty version of the Node to the Writer. |
|
public static String |
serialize(GPathResult node)Return a pretty version of the GPathResult. |
|
public static void |
serialize(GPathResult node, OutputStream os)Write a pretty version of the GPathResult to the OutputStream. |
|
public static void |
serialize(GPathResult node, Writer w)Write a pretty version of the GPathResult to the Writer. |
|
public static String |
serialize(Writable writable)Return a pretty String version of the XML content produced by the Writable. |
|
public static void |
serialize(Writable writable, OutputStream os)Write a pretty version of the XML content produced by the Writable to the OutputStream. |
|
public static void |
serialize(Writable writable, Writer w)Write a pretty version of the XML content produced by the Writable to the Writer. |
|
public static String |
serialize(String xmlString)Return a pretty version of the XML content contained in the given String. |
|
public static String |
serialize(String xmlString, boolean allowDocTypeDeclaration)Return a pretty version of the XML content contained in the given String. |
|
public static void |
serialize(String xmlString, OutputStream os)Write a pretty version of the given XML string to the OutputStream. |
|
public static void |
serialize(String xmlString, OutputStream os, boolean allowDocTypeDeclaration)Write a pretty version of the given XML string to the OutputStream. |
|
public static void |
serialize(String xmlString, Writer w)Write a pretty version of the given XML string to the Writer. |
|
public static void |
serialize(String xmlString, Writer w, boolean allowDocTypeDeclaration)Write a pretty version of the given XML string to the Writer. |
|
public static String |
serialize(Element element, SerializeOptions options)Return a pretty String version of the Element using the specified options. |
|
public static void |
serialize(Element element, OutputStream os, SerializeOptions options)Write a pretty version of the Element to the OutputStream using the specified options. |
|
public static String |
serialize(Node node, SerializeOptions options)Return a pretty String version of the Node using the specified options. |
|
public static void |
serialize(Node node, OutputStream os, SerializeOptions options)Write a pretty version of the Node to the OutputStream using the specified options. |
|
public static String |
serialize(GPathResult node, SerializeOptions options)Return a pretty String version of the GPathResult using the specified options. |
|
public static void |
serialize(GPathResult node, OutputStream os, SerializeOptions options)Write a pretty version of the GPathResult to the OutputStream using the specified options. |
|
public static String |
serialize(Writable writable, SerializeOptions options)Return a pretty String version of the XML content produced by the Writable using the specified options. |
|
public static void |
serialize(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. |
|
public static String |
serialize(String xmlString, SerializeOptions options)Return a pretty version of the XML content contained in the given String using the specified options. |
|
public static void |
serialize(String xmlString, OutputStream os, SerializeOptions options)Write a pretty version of the given XML string to the OutputStream using the specified options. |
|
public static void |
setFeatureQuietly(TransformerFactory factory, String feature, boolean value)Attempts to set a feature on the transformer factory and ignores unsupported features. |
|
public static void |
setFeatureQuietly(DocumentBuilderFactory factory, String feature, boolean value)Attempts to set a feature on the document builder factory and ignores unsupported features. |
|
public static void |
setFeatureQuietly(SAXParserFactory factory, String feature, boolean value)Attempts to set a feature on the SAX parser factory and ignores unsupported features. |
|
public static Stream<Node> |
streamElements(Reader reader, String localName)Streams matching subtrees from a (potentially very large) XML document as DOM Nodes. |
|
public static Stream<Node> |
streamElements(Reader reader, String namespaceURI, String localName)Streams matching subtrees from a (potentially very large) XML document as DOM Nodes. |
|
public static Stream<Node> |
streamElements(Reader reader, String namespaceURI, String localName, boolean allowDocTypeDeclaration)Streams matching subtrees from a (potentially very large) XML document as DOM Nodes. |
Escape control characters (below 0x20) with their XML entities, e.g.
carriage return (Ctrl-M or \r) becomes
Notes:
orig - the original String Escape the following characters " ' & < > with their XML entities, e.g.
"bread" & "butter" becomes "bread" & "butter".
Notes:
orig - the original StringStreams XML events from the supplied Reader using a hardened StAX XMLInputFactory. The returned stream lazily pulls events; closing the stream closes the underlying reader.
Equivalent to events(Reader, boolean) events(reader, false).
reader - the XML sourceStreams XML events from the supplied Reader using a hardened StAX XMLInputFactory. The returned stream lazily pulls events; closing the stream closes the underlying reader.
reader - the XML sourceallowDocTypeDeclaration - whether DOCTYPE declarations are permittedFactory 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.
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 againstFactory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with.
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 againstFactory method to create a SAXParser configured to validate according to a particular schema language and optionally providing the schema sources to validate with.
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 againstFactory 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.
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 againstFactory method to create a SAXParser configured to validate according to a particular schema language and a File containing the schema to validate against.
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 againstFactory 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.
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 againstFactory method to create a SAXParser configured to validate according to a particular schema language and a URL pointing to the schema to validate against.
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 againstReturn a pretty String version of the Element.
element - the Element to serializeReturn a pretty String version of the Element.
element - the Element to serializeallowDocTypeDeclaration - whether to allow doctype processingWrite a pretty version of the Element to the OutputStream.
element - the Element to serializeos - the OutputStream to write toWrite a pretty version of the Element to the OutputStream.
element - the Element to serializeos - the OutputStream to write toallowDocTypeDeclaration - whether to allow doctype processingWrite a pretty version of the Element to the Writer.
element - the Element to serializew - the Writer to write toWrite a pretty version of the Element to the Writer.
element - the Element to serializew - the Writer to write toallowDocTypeDeclaration - whether to allow doctype processingReturn a pretty String version of the Node.
node - the Node to serializeWrite a pretty version of the Node to the OutputStream.
node - the Node to serializeos - the OutputStream to write toWrite a pretty version of the Node to the Writer.
node - the Node to serializew - the Writer to write toReturn a pretty version of the GPathResult.
node - a GPathResult to serialize to a StringWrite a pretty version of the GPathResult to the OutputStream.
node - a GPathResult to serializeos - the OutputStream to write toWrite a pretty version of the GPathResult to the Writer.
node - a GPathResult to serializew - the Writer to write toReturn a pretty String version of the XML content produced by the Writable.
writable - the Writable to serializeWrite a pretty version of the XML content produced by the Writable to the OutputStream.
writable - the Writable to serializeos - the OutputStream to write toWrite a pretty version of the XML content produced by the Writable to the Writer.
writable - the Writable to serializew - the Writer to write toReturn a pretty version of the XML content contained in the given String.
xmlString - the String to serializeReturn a pretty version of the XML content contained in the given String.
xmlString - the String to serializeallowDocTypeDeclaration - whether to allow doctype processingWrite a pretty version of the given XML string to the OutputStream.
xmlString - the String to serializeos - the OutputStream to write toWrite a pretty version of the given XML string to the OutputStream.
xmlString - the String to serializeallowDocTypeDeclaration - whether to allow doctype processingos - the OutputStream to write toWrite a pretty version of the given XML string to the Writer.
xmlString - the String to serializew - the Writer to write toWrite a pretty version of the given XML string to the Writer.
xmlString - the String to serializeallowDocTypeDeclaration - whether to allow doctype processingw - the Writer to write toReturn a pretty String version of the Element using the specified options.
element - the Element to serializeoptions - the serialization optionsWrite a pretty version of the Element to the OutputStream using the specified options.
element - the Element to serializeos - the OutputStream to write tooptions - the serialization optionsReturn a pretty String version of the Node using the specified options.
node - the Node to serializeoptions - the serialization optionsWrite a pretty version of the Node to the OutputStream using the specified options.
node - the Node to serializeos - the OutputStream to write tooptions - the serialization optionsReturn a pretty String version of the GPathResult using the specified options.
node - a GPathResult to serialize to a Stringoptions - the serialization optionsWrite a pretty version of the GPathResult to the OutputStream using the specified options.
node - a GPathResult to serializeos - the OutputStream to write tooptions - the serialization optionsReturn a pretty String version of the XML content produced by the Writable using the specified options.
writable - the Writable to serializeoptions - the serialization optionsWrite a pretty version of the XML content produced by the Writable to the OutputStream using the specified options.
writable - the Writable to serializeos - the OutputStream to write tooptions - the serialization optionsReturn a pretty version of the XML content contained in the given String using the specified options.
xmlString - the String to serializeoptions - the serialization optionsWrite a pretty version of the given XML string to the OutputStream using the specified options.
xmlString - the String to serializeos - the OutputStream to write tooptions - the serialization optionsAttempts to set a feature on the transformer factory and ignores unsupported features.
factory - the transformer factory to configurefeature - the fully qualified JAXP feature URIvalue - the value to applyAttempts to set a feature on the document builder factory and ignores unsupported features.
factory - the document builder factory to configurefeature - the fully qualified JAXP feature URIvalue - the value to applyAttempts to set a feature on the SAX parser factory and ignores unsupported features.
factory - the SAX parser factory to configurefeature - the fully qualified JAXP feature URIvalue - the value to apply Streams matching subtrees from a (potentially very large) XML document
as DOM Nodes. Each emitted node is the root of one
complete element matching localName; 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+).
reader - the XML sourcelocalName - the element local name to matchStreams matching subtrees from a (potentially very large) XML document as DOM Nodes. Matching is namespace-qualified.
reader - the XML sourcenamespaceURI - the namespace URI to match (use null to match any)localName - the element local name to matchStreams matching subtrees from a (potentially very large) XML document as DOM Nodes. Matching is namespace-qualified.
reader - the XML sourcenamespaceURI - the namespace URI to match (use null to match any)localName - the element local name to matchallowDocTypeDeclaration - whether DOCTYPE declarations are permitted