java.lang.Object
com.aquima.interactions.foundation.xml.parsing.XmlDOMParser
All Implemented Interfaces:
IXmlParser

public class XmlDOMParser extends Object implements IXmlParser
DOM Parser with error handling in place (validating or normal).

This is a convenience wrapper around the JAXP DocumentBuilder, which in turn is the pluggable XML API for any compatible DOM parser. Notice that this is a synchronous parser that may be used by one thread only! It is fine to reuse the parser after parsing has finished.

This parser returns custom XMLElement objects instead of org.w3c.dom.Document / Node, to make it much easer to access the relevant data (attributes, values,...)

Developed using JAXP 1.1, previous versions will not work. JDK 1.4 comes with a JAXP parser, or use a recent Apache Xerces parser.

Example use:

 XmlDOMParser parser = new XmlDOMParserBuilder().namespaceAware(true).entityResolver(theDTDMgr).build();
 // create an XML document source, for instance from a file:
 InputStream inputStream = new FileInputStream(xmlfile);
 // parse it!
 IXmlNode root = parser.parse(inputStream);
 // ... process root...
 // if any errors: parser.getErrors() or XMLException.getParseErrors()... process...
 
Since:
5.0
Author:
IdJ
  • Method Details

    • getValidating

      public boolean getValidating()
      Returns:
      Boolean indicating if the XML should be validated.
    • getNamespaceAware

      public boolean getNamespaceAware()
      Returns:
      Boolean indicating if the parser should consider name-spaces.
    • getValueTrimming

      public boolean getValueTrimming()
      Returns:
      Boolean indicating if white-spaces should be removed while parsing.
    • getCoalescing

      public boolean getCoalescing()
      Specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node. By default the value of this is set to false
      Returns:
      true if the parser produced will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node; false otherwise.
    • getEntityResolver

      public EntityResolver getEntityResolver()
      Returns:
      The entity resolver that is used.
    • getErrors

      public String[] getErrors()
      Return an array of the errors that were encountered.
      Specified by:
      getErrors in interface IXmlParser
      Returns:
      array of errors, may be empty, but never null.
    • parse

      public IXmlNode parse(String strXml)
      Parses the given XML doc and returns the root element. You may call this again for a new document after parsing has finished. Note that the platform's default charset is used, when the doc string cannot be encoded in the default charset it's behaviour is unspecified. (use parse(InputStream source) for more control over the used charset/encoding)
      Parameters:
      strXml - an XML document as a String
      Returns:
      XMLElement that represents the root of the XML document
      Throws:
      XmlParseException - in case of XML errors
    • parse

      public IXmlNode parse(InputStream source) throws IOException
      Parses the given SAX2 input source (with the DOM parser) and returns the root element. You may call this again for a new document after parsing has finished.
      Specified by:
      parse in interface IXmlParser
      Parameters:
      source - a SAX2 input source from which the XML is read
      Returns:
      XMLElement that represents the root of the XML document
      Throws:
      XmlParseException - in case of XML errors
      IOException - when the source can't be read
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getParseTime

      public long getParseTime()
      Get the time in milliseconds it took for the parse method to run.
      Specified by:
      getParseTime in interface IXmlParser
      Returns:
      The parse time in milliseconds.
    • cleanup

      public void cleanup()
      Clean up internal storage (such as the errorHandler's list of errors).