Package com.itextpdf.kernel.utils
Class XmlUtils
java.lang.Object
com.itextpdf.kernel.utils.XmlUtils
Utility methods for working with XML Documents in a safe way.
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleancompareXmls(InputStream xml1, InputStream xml2) Compares two XML documents provided as input streams for structural equality.static Documentstatic DocumentinitXmlDocument(InputStream inputStream) Parses an XML document from the provided input stream and returns the resulting DOMDocument.static voidwriteXmlDocToStream(Document xmlReport, OutputStream stream) Writes the provided DOMDocumentto the givenOutputStream.
-
Method Details
-
writeXmlDocToStream
public static void writeXmlDocToStream(Document xmlReport, OutputStream stream) throws TransformerException Writes the provided DOMDocumentto the givenOutputStream.- Parameters:
-
xmlReport- the DOM document to serialize; must not benull -
stream- the output stream to write the XML to; the caller is responsible for opening and closing the stream - Throws:
-
TransformerException- when an error occurs during transformation
-
compareXmls
public static boolean compareXmls(InputStream xml1, InputStream xml2) throws SAXException, IOException Compares two XML documents provided as input streams for structural equality.The method parses both input streams using a secure
DocumentBuilder(created viaXmlProcessorCreator.createSafeDocumentBuilder(boolean, boolean)), normalizes the documents and removes empty text nodes before delegating toNode.isEqualNode(Node)to perform the equality check.- Parameters:
-
xml1- input stream of the first XML document; must be readable -
xml2- input stream of the second XML document; must be readable - Returns:
-
trueif the documents are structurally equal,falseotherwise - Throws:
-
SAXException- when a parse error occurs -
IOException- when an I/O error occurs while reading the streams
-
initNewXmlDocument
- Returns:
-
a new empty
Document
-
initXmlDocument
Parses an XML document from the provided input stream and returns the resulting DOMDocument.If parsing fails for any reason the method wraps the underlying exception in a
PdfException.- Parameters:
-
inputStream- the input stream containing XML content; must be readable - Returns:
-
the parsed
Document - Throws:
-
PdfException- if parsing fails; the original exception is available as the cause
-