Class XmlUtil2

java.lang.Object
net.sf.basedb.util.XmlUtil2

public class XmlUtil2
extends Object
This class contains some useful methods for handling XML documents using JDOM2.
Since:
3.4
Author:
Nicklas
Last modified
$Date$
  • Field Details

    • log

      private static final org.slf4j.Logger log
      Log core events.
    • xmlParserClass

      private static final String xmlParserClass
      The parser class we are using.
      See Also:
      Constant Field Values
  • Constructor Details

    • XmlUtil2

      public XmlUtil2()
  • Method Details

    • toXml

      public static String toXml​(Document dom)
      Convert a Document to xml. The encoding is set to UTF-8.
      Parameters:
      dom - The document
      Returns:
      The XML
    • createDom

      public static Document createDom​(String rootElement, String systemId)
      Create a new Document with root element and System id declaration.
      Parameters:
      rootElement - The name of the root element tag
      systemId - The system id in the DOCTYPE section
      Returns:
      a Document object
    • getValidatedXml

      public static Document getValidatedXml​(URL xmlFile, URL dtdFile) throws IOException
      Load and validate an XML file against a DTD, and return it as a Document object. The path to the files should be given as a URL, not a filesystem path. For a file on the classpath it is easiest done by the Class.getResource(String) method, ie. XMLUtil.class.getResource("/net/sf/basedb/core/the-xml-file.xml").
      Parameters:
      xmlFile - The URL to the XML file
      dtdFile - The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
      Returns:
      A Document object
      Throws:
      IOException - If reading the input stream fails.
    • getValidatedXml

      public static Document getValidatedXml​(String xml, URL dtdFile) throws IOException
      Throws:
      IOException
      See Also:
      getXml(String, URL, boolean)
    • getXml

      public static Document getXml​(String xml, URL dtdFile, boolean validate) throws IOException
      Parse a string containing XML, optionally validating it against a DTD, and return it as a Document object. The path to the DTD should be given as a URL, not a filesystem path. For a file on the classpath it is easiest done by the Class.getResource(String) method, ie. XMLUtil.class.getResource("/net/sf/basedb/core/the-dtd-file.dtd").
      Parameters:
      xml - The string containing the XML
      dtdFile - The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1
      validate - TRUE to validate the xml data
      Returns:
      A Document object
      Throws:
      IOException - If reading the string fails.
    • getValidatedXML

      public static Document getValidatedXML​(InputStream is, URL dtdFile, String filename) throws IOException
      Validate an InputStream against a DTD file and return it as a Document
      Parameters:
      is - The inputstream to the XML
      dtdFile - The URL to the DTD used for validation or null to not valdiate against a DTD. The DTD must be encoded in ISO-8859-1
      filename - The filename of the original XML data, used for error reporting only; use null if the filename is not known
      Returns:
      A Document object
      Throws:
      IOException - If reading the input stream fails.
    • getSchemaValidatedXML

      public static Document getSchemaValidatedXML​(URL xmlFile, String... schemaFiles) throws IOException
      Validate an XML file using XML schemas.
      Parameters:
      xmlFile - The URL pointing to the XML file to load
      schemaFiles - An array of schema file declarations. The array must contain pairs of elements, the first one is the namespace name, and the second element is the URL to the schema definition file
      Returns:
      A Document object
      Throws:
      IOException - If reading the input stream fails
    • getSchemaValidatedXML

      public static Document getSchemaValidatedXML​(InputStream in, String filename, String... schemaFiles) throws IOException
      Validate an XML input stream using XML schemas.
      Parameters:
      in - The inputstream to the XML
      filename - The filename of the original XML data, used for error reporting only; use null if the filename is not known
      schemaFiles - An array of schema file declarations. The array must contain pairs of elements, the first one is the namespace name, and the second element is the URL to the schema definition file
      Returns:
      A Document object
      Throws:
      IOException - If reading the input stream fails
    • getIntAttribute

      public static int getIntAttribute​(Element el, String attribute, int defaultValue)
      Get the value of an attribute as an integer. If the attribute is missing or doesn't contain an integer, the default value is returned.
      Parameters:
      el - The node element which contains the attribute
      attribute - The name of the attribute
      defaultValue - The default value to return
      Returns:
      an integer.
    • getBooleanAttribute

      public static boolean getBooleanAttribute​(Element el, String attribute, boolean defaultValue)
      Get the value of an attribute as a boolean. If the attribute is missing or doesn't contain an boolean value, the default value is returned.
      Parameters:
      el - The node element which contains the attribute
      attribute - The name of the attribute
      defaultValue - The default value to return
      Returns:
      TRUE if the attribute has a true value, FALSE if it has a false value