Package net.sf.basedb.util
Class XmlUtil2
java.lang.Object
net.sf.basedb.util.XmlUtil2
This class contains some useful methods for handling
XML documents using JDOM2.
- Since:
- 3.4
- Author:
- Nicklas
- Last modified
- $Date$
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static class
This class is used to handle callbacks from the XML parser. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Document
Create a newDocument
with root element and System id declaration.static boolean
getBooleanAttribute
(Element el, String attribute, boolean defaultValue) Get the value of an attribute as a boolean.static int
getIntAttribute
(Element el, String attribute, int defaultValue) Get the value of an attribute as an integer.static Document
getSchemaValidatedXML
(InputStream in, String filename, String... schemaFiles) Validate an XML input stream using XML schemas.static Document
getSchemaValidatedXML
(URL xmlFile, String... schemaFiles) Validate an XML file using XML schemas.static Document
getValidatedXml
(String xml, URL dtdFile) static Document
getValidatedXml
(URL xmlFile, URL dtdFile) Load and validate an XML file against a DTD, and return it as aDocument
object.static Document
getValidatedXML
(InputStream is, URL dtdFile, String filename) Validate anInputStream
against a DTD file and return it as aDocument
static Document
Parse a string containing XML, optionally validating it against a DTD, and return it as aDocument
object.static String
Convert aDocument
to xml.
-
Field Details
-
log
Log core events. -
xmlParserClass
The parser class we are using.- See Also:
-
-
Constructor Details
-
XmlUtil2
public XmlUtil2()
-
-
Method Details
-
toXml
Convert aDocument
to xml. The encoding is set to UTF-8.- Parameters:
dom
- The document- Returns:
- The XML
-
createDom
Create a newDocument
with root element and System id declaration.- Parameters:
rootElement
- The name of the root element tagsystemId
- The system id in the DOCTYPE section- Returns:
- a
Document
object
-
getValidatedXml
Load and validate an XML file against a DTD, and return it as aDocument
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 theClass.getResource(String)
method, ie.XMLUtil.class.getResource("/net/sf/basedb/core/the-xml-file.xml")
.- Parameters:
xmlFile
- The URL to the XML filedtdFile
- 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
- Throws:
IOException
- See Also:
-
getXml
Parse a string containing XML, optionally validating it against a DTD, and return it as aDocument
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 theClass.getResource(String)
method, ie.XMLUtil.class.getResource("/net/sf/basedb/core/the-dtd-file.dtd")
.- Parameters:
xml
- The string containing the XMLdtdFile
- The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1validate
- 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 anInputStream
against a DTD file and return it as aDocument
- Parameters:
is
- The inputstream to the XMLdtdFile
- The URL to the DTD used for validation or null to not valdiate against a DTD. The DTD must be encoded in ISO-8859-1filename
- 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
Validate an XML file using XML schemas.- Parameters:
xmlFile
- The URL pointing to the XML file to loadschemaFiles
- 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 XMLfilename
- The filename of the original XML data, used for error reporting only; use null if the filename is not knownschemaFiles
- 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
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 attributeattribute
- The name of the attributedefaultValue
- The default value to return- Returns:
- an integer.
-
getBooleanAttribute
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 attributeattribute
- The name of the attributedefaultValue
- The default value to return- Returns:
- TRUE if the attribute has a true value, FALSE if it has a false value
-