Last update: 2011-06-23

org.proteios.util
Class XMLUtil

java.lang.Object
  extended by org.proteios.util.XMLUtil

public class XMLUtil
extends Object

This class contains some useful methods for handling XML documents.

Version:
2.0
Author:
Nicklas
Last modified
$Date: 2009-08-03 08:46:28 +0200 (Mon, 03 Aug 2009) $

Constructor Summary
XMLUtil()
           
 
Method Summary
static org.dom4j.Document createDom(String rootElement, String systemId)
          Create a new Document with root element and System id declaration.
static boolean getBooleanAttribute(org.dom4j.Element el, String attribute, boolean defaultValue)
          Get the value of an attribute as a boolean.
static int getIntAttribute(org.dom4j.Element el, String attribute, int defaultValue)
          Get the value of an attribute as an integer.
static org.dom4j.Document getValidatedXml(String xml, URL dtdFile)
          Parse and validate a string containing XML against a DTD, and return it as a Document object.
static org.dom4j.Document getValidatedXml(URL xmlFile, URL dtdFile)
          Load and validate an XML file against a DTD, and return it as a Document object.
static String toXml(org.dom4j.Document dom)
          Convert a Document to xml.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLUtil

public XMLUtil()
Method Detail

toXml

public static String toXml(org.dom4j.Document dom)
Convert a Document to xml. The encoding is set to UTF-8.

Parameters:
dom - The document
Returns:
The XML

createDom

public static org.dom4j.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

getValidatedXml

public static org.dom4j.Document getValidatedXml(URL xmlFile,
                                                 URL dtdFile)
                                          throws org.dom4j.DocumentException,
                                                 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("/org.proteios/core/the-xml-file.xml").

Parameters:
xmlFile - The URL to the XML file
dtdFile - The URL to the DTD used for validation
Returns:
A Document object
Throws:
org.dom4j.DocumentException
IOException

getValidatedXml

public static org.dom4j.Document getValidatedXml(String xml,
                                                 URL dtdFile)
                                          throws org.dom4j.DocumentException,
                                                 IOException
Parse and validate a string containing XML 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("/org.proteios/core/the-dtd-file.dtd").

Parameters:
xml - The string containing the XML
dtdFile - The URL to the DTD used for validation
Returns:
A Document object
Throws:
org.dom4j.DocumentException
IOException

getIntAttribute

public static int getIntAttribute(org.dom4j.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

getBooleanAttribute

public static boolean getBooleanAttribute(org.dom4j.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

Last update: 2011-06-23