Class XmlUtils

java.lang.Object
com.itextpdf.kernel.utils.XmlUtils

public final class XmlUtils extends Object
Utility methods for working with XML Documents in a safe way.
  • Method Details

    • writeXmlDocToStream

      public static void writeXmlDocToStream (Document xmlReport, OutputStream stream) throws TransformerException
      Writes the provided DOM Document to the given OutputStream.
      Parameters:
      xmlReport - the DOM document to serialize; must not be null
      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 via XmlProcessorCreator.createSafeDocumentBuilder(boolean, boolean)), normalizes the documents and removes empty text nodes before delegating to Node.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:
      true if the documents are structurally equal, false otherwise
      Throws:
      SAXException - when a parse error occurs
      IOException - when an I/O error occurs while reading the streams
    • initNewXmlDocument

      public static Document initNewXmlDocument()
      Creates and returns a new empty DOM Document using a safe DocumentBuilder from XmlProcessorCreator.
      Returns:
      a new empty Document
    • initXmlDocument

      public static Document initXmlDocument (InputStream inputStream)
      Parses an XML document from the provided input stream and returns the resulting DOM Document.

      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