public class XmlUtil2 extends Object
Modifier and Type | Class and Description |
---|---|
private static class |
XmlUtil2.Validator
This class is used to handle callbacks from the XML parser.
|
Modifier and Type | Field and Description |
---|---|
private static org.slf4j.Logger |
log
Log core events.
|
private static String |
xmlParserClass
The parser class we are using.
|
Constructor and Description |
---|
XmlUtil2() |
Modifier and Type | Method and Description |
---|---|
static Document |
createDom(String rootElement,
String systemId)
Create a new
Document 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(InputStream is,
URL dtdFile,
String filename)
Validate an
InputStream against a DTD file
and return it as a Document |
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 a
Document object. |
static Document |
getXml(String xml,
URL dtdFile,
boolean validate)
Parse a string containing XML, optionally validating it against a DTD,
and return it as a
Document object. |
static String |
toXml(Document dom)
Convert a
Document to xml. |
private static final org.slf4j.Logger log
private static final String xmlParserClass
public static String toXml(Document dom)
Document
to xml. The encoding is set to UTF-8.dom
- The documentpublic static Document createDom(String rootElement, String systemId)
Document
with root element and System id
declaration.rootElement
- The name of the root element tagsystemId
- The system id in the DOCTYPE sectionDocument
objectpublic static Document getValidatedXml(URL xmlFile, URL dtdFile) throws IOException
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")
.xmlFile
- The URL to the XML filedtdFile
- The URL to the DTD used for validation, the DTD must be encoded in ISO-8859-1Document
objectJDOMException
- If validation of XML fails.IOException
- If reading the input stream fails.public static Document getValidatedXml(String xml, URL dtdFile) throws IOException
IOException
getXml(String, URL, boolean)
public static Document getXml(String xml, URL dtdFile, boolean validate) throws IOException
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")
.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 dataDocument
objectJDOMException
- If validation of XML fails.IOException
- If reading the string fails.public static Document getValidatedXML(InputStream is, URL dtdFile, String filename) throws IOException
InputStream
against a DTD file
and return it as a Document
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 knownDocument
objectJDOMException
- If validation of the XML fails.IOException
- If reading the input stream fails.public static Document getSchemaValidatedXML(URL xmlFile, String... schemaFiles) throws IOException
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 fileDocument
objectJDOMException
- If validation of the XML failsIOException
- If reading the input stream failspublic static Document getSchemaValidatedXML(InputStream in, String filename, String... schemaFiles) throws IOException
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 fileDocument
objectJDOMException
- If validation of the XML failsIOException
- If reading the input stream failspublic static int getIntAttribute(Element el, String attribute, int defaultValue)
el
- The node element which contains the attributeattribute
- The name of the attributedefaultValue
- The default value to returnpublic static boolean getBooleanAttribute(Element el, String attribute, boolean defaultValue)
el
- The node element which contains the attributeattribute
- The name of the attributedefaultValue
- The default value to return