Class XmlDataAdaptor
- All Implemented Interfaces:
DataAdaptor,FileDataAdaptor
XmlDataAdaptor is a DataAdaptor that specifically supports (reading/writing) (from/to) XML. While the DataAdaptor provides methods for getting and setting properties of nodes and getting and setting nodes in a tree of data, XmlDataAdaptor uses an XML backing store for the data. In particular, you can use the methods of DataAdaptor for all data manipulation. You need to specifically use XmlDataAdaptor when creating a new document, writing an XML document to a file or loading an XML document from a file.
To create a new, empty XML document simply invoke:
document_adaptor = XmlDataAdaptor.newEmptyDocumentAdaptor();
You can populate the document by adding child nodes. You can only add a
single node to the top document node, but otherwise you can add and nest as
many nodes as needed. Each such node is returned as a DataAdaptor. For
example, to add a node to the top document node, invoke:
childAdaptor = document_adaptor.createChild("nodeName")
You can set attributes of nodes with some basic types such as boolean,
integer, double and string where you supply the name of the attribute and the
value. If you add an Object as a value, then toString() is invoked to fetch
the value as a string. Some examples:
adaptor.setValue("attribute", "some string");
adaptor.setValue("attr2", 3.14);
You can write an XML document to a URL, a file or generally to a
java.io.Writer. For example, to write an XML document to a file invoke:
document_adaptor.writeTo( new File("file_path") );
You can read an XML document from a string of XML text, a URL or a file. You
may specify whether or not to use DTD validation. For example, to read an XML
document from a file invoke:
document_adaptor = XmlDataAdaptor.adaptorForFile( new File("file_path"), false );
You can fetch named child nodes from a parent node. Some examples are:
List
List
DataAdaptor yAdaptor = parentAdaptor.childAdaptor("Y")
You can test if a node defines an attribute:
boolean status = adaptor.hasAttribute("attribute");
You can read the value of an attribute:
double value = adaptor.doubleValue("attr2");
There are several more methods available for DataAdaptor and XmlDataAdaptor, but the examples listed above should provide an overview and foundation.
- Author:
- tap
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic classWrapper for exceptions that may be thrown while writing: java.io.IOException java.net.MalformedURLException -
Constructor Summary
ConstructorsConstructorDescriptionXmlDataAdaptor(Document newDocument) Creates a new XmlDataAdaptor from an XML DocumentXmlDataAdaptor(Node newNode) Creates a new XmlDataAdaptor from an XML Node -
Method Summary
Modifier and TypeMethodDescriptionstatic XmlDataAdaptoradaptorForFile(File file, boolean isValidating) Generate an XmlDataAdaptor from a urlPath and given dtd validating optionstatic XmlDataAdaptoradaptorForString(String source, boolean isValidating) Generate an XmlDataAdaptor from an XML string and given dtd validating optionstatic XmlDataAdaptoradaptorForUrl(String urlPath, boolean isValidating) Generate an XmlDataAdaptor from a urlPath and given dtd validating optionstatic XmlDataAdaptoradaptorForUrl(String urlPath, boolean isValidating, String schemaPath) Generate an XmlDataAdaptor from a urlPath, given dtd validating option, and given schemaUrlstatic XmlDataAdaptoradaptorForUrl(URL url, boolean isValidating) Generate an XmlDataAdaptor from a urlPath and given dtd validating optionString[]return an array of attribute namesbooleanbooleanValue(String attribute) return the boolean value associated with the attributechildAdaptor(String label) Convenience method to get a single child adaptor when only one is expectedCreate a list of child adaptors (one adaptor for each non-null child node).childAdaptors(String label) Create a list of child adaptors (one adaptor for each non-null child node whose tag name is equal to the specified label).createChild(String tagName) Create a new offspring DataAdaptor given the tagNamedocument()get the document associated with this XML adaptordouble[]doubleArray(String attribute) Returns the value of an attribute as an array of doubles.doubledoubleValue(String attribute) return the double value associated with the attributebooleanhasAttribute(String attribute) check whether the main node has the specified attributeintreturn the integer value associated with the attributelongreturn the long value associated with the attributename()get the tag name for the main nodestatic XmlDataAdaptornewDocumentAdaptor(DataListener dataHandler, String dtdUri) Create a new XmlDataAdaptor given a DataListener and a dtd URIprotected static DocumentBuildernewDocumentBuilder(boolean isValidating) Create a new document builder with the given DTD validationprotected static DocumentBuildernewDocumentBuilder(boolean isValidating, URL schemaURL) Create a new document builder with the given DTD validation, and schemaUrlstatic XmlDataAdaptorCreate an empty XML documentstatic XmlDataAdaptornewEmptyDocumentAdaptor(String docTag, String dtdURI) Create an XML document with only the doc tag and DTD URI specifiedintreturn the count of non-null child nodesintreturn the count of all child nodes (including null child nodes)protected final StringGet the string value associated with the specified attribute allowing DOM to recover escaped characters as necessary.voidremoveChild(DataAdaptor adaptor) Remove a child DataAdaptorprotected final voidsetRawValue(String attribute, String value) Set the string value to associate with the attribute and allow DOM to escape special characters as necessary.voidset the boolean value to be associated with the attributevoidset the double value to be associated with the attributevoidStores the value of the givendouble[]object in the data adaptor backing store.voidset the integer value to be associated with the attributevoidset the long value to be associated with the attributevoidset the value of the specified attribute to the specified valuevoidSet the string value to be associated with the attribute and replace illegal XML attribute characters (less than and ampersand) with their legal XML attribute substitutions.stringValue(String attribute) Get the string value associated with the specified attribute.voidwriteNode(DataListener listener) append a node associated with the listenervoidwriteNodes(Collection<? extends DataListener> nodes) append a node for each listener in the listener listvoidConvenience method for writing an XML filevoidWrite XML to the specified urlvoidwriteToUrl(URL url) Write XML to the specified urlvoidwriteToUrlSpec(String urlSpec) Write XML to the specified url
-
Constructor Details
-
XmlDataAdaptor
Creates a new XmlDataAdaptor from an XML Node -
XmlDataAdaptor
Creates a new XmlDataAdaptor from an XML Document
-
-
Method Details
-
name
get the tag name for the main node- Specified by:
namein interfaceDataAdaptor
-
document
get the document associated with this XML adaptor -
hasAttribute
check whether the main node has the specified attribute- Specified by:
hasAttributein interfaceDataAdaptor
-
rawValue
Get the string value associated with the specified attribute allowing DOM to recover escaped characters as necessary.- Parameters:
attribute- The node attribute.- Returns:
- the raw string value associated with the attribute or null if the attribute does not exist
-
stringValue
Get the string value associated with the specified attribute.- Specified by:
stringValuein interfaceDataAdaptor- Parameters:
attribute- The node attribute.- Returns:
- the raw string value associated with the attribute
-
doubleValue
return the double value associated with the attribute- Specified by:
doubleValuein interfaceDataAdaptor- Throws:
NumberFormatException
-
longValue
return the long value associated with the attribute- Specified by:
longValuein interfaceDataAdaptor- Throws:
NumberFormatException
-
intValue
return the integer value associated with the attribute- Specified by:
intValuein interfaceDataAdaptor- Throws:
NumberFormatException
-
booleanValue
return the boolean value associated with the attribute- Specified by:
booleanValuein interfaceDataAdaptor- Throws:
NumberFormatException
-
doubleArray
Returns the value of an attribute as an array of doubles.- Specified by:
doubleArrayin interfaceDataAdaptor- Parameters:
attribute- the attribute name- Returns:
- Array of double values, a
nullvalue is returned if the value string is empty. - Throws:
NumberFormatException
-
setRawValue
Set the string value to associate with the attribute and allow DOM to escape special characters as necessary.- Parameters:
attribute- The node attribute.value- The string value to associate with the attribute.
-
setValue
Set the string value to be associated with the attribute and replace illegal XML attribute characters (less than and ampersand) with their legal XML attribute substitutions.- Specified by:
setValuein interfaceDataAdaptor- Parameters:
attribute- The node attribute.value- The string value to associate with the attribute.
-
setValue
set the double value to be associated with the attribute- Specified by:
setValuein interfaceDataAdaptor
-
setValue
set the long value to be associated with the attribute- Specified by:
setValuein interfaceDataAdaptor
-
setValue
set the integer value to be associated with the attribute- Specified by:
setValuein interfaceDataAdaptor
-
setValue
set the boolean value to be associated with the attribute- Specified by:
setValuein interfaceDataAdaptor
-
setValue
set the value of the specified attribute to the specified value- Specified by:
setValuein interfaceDataAdaptor
-
setValue
Stores the value of the givendouble[]object in the data adaptor backing store.- Specified by:
setValuein interfaceDataAdaptor- Parameters:
attribute- attribute namearray- attribute value
-
attributes
return an array of attribute names- Specified by:
attributesin interfaceDataAdaptor
-
nodeCount
public int nodeCount()return the count of non-null child nodes -
rawNodeCount
public int rawNodeCount()return the count of all child nodes (including null child nodes) -
childAdaptors
Create a list of child adaptors (one adaptor for each non-null child node).- Specified by:
childAdaptorsin interfaceDataAdaptor- Returns:
- a list of child adaptors
-
childAdaptors
Create a list of child adaptors (one adaptor for each non-null child node whose tag name is equal to the specified label).- Specified by:
childAdaptorsin interfaceDataAdaptor- Parameters:
label- the label for which to match the node's tag- Returns:
- a list of child adaptors
-
childAdaptor
Convenience method to get a single child adaptor when only one is expected- Specified by:
childAdaptorin interfaceDataAdaptor- Parameters:
label- the label which identifies the tag of the nodes to fetch- Returns:
- a data adaptor for the specified child node
-
createChild
Create a new offspring DataAdaptor given the tagName- Specified by:
createChildin interfaceDataAdaptor
-
removeChild
Remove a child DataAdaptor- Specified by:
removeChildin interfaceDataAdaptor
-
writeNode
append a node associated with the listener- Specified by:
writeNodein interfaceDataAdaptor
-
writeNodes
append a node for each listener in the listener list- Specified by:
writeNodesin interfaceDataAdaptor- Parameters:
nodes- the nodes to write
-
writeToUrlSpec
Write XML to the specified url- Specified by:
writeToUrlSpecin interfaceFileDataAdaptor- Throws:
XmlDataAdaptor.WriteException
-
writeToUrl
Write XML to the specified url- Specified by:
writeToUrlin interfaceFileDataAdaptor- Throws:
XmlDataAdaptor.WriteException
-
writeTo
Write XML to the specified url -
writeTo
Convenience method for writing an XML file- Specified by:
writeToin interfaceFileDataAdaptor- Throws:
IOException
-
adaptorForUrl
public static XmlDataAdaptor adaptorForUrl(String urlPath, boolean isValidating) throws XmlDataAdaptor.ParseException, XmlDataAdaptor.ResourceNotFoundException Generate an XmlDataAdaptor from a urlPath and given dtd validating option -
adaptorForUrl
public static XmlDataAdaptor adaptorForUrl(String urlPath, boolean isValidating, String schemaPath) throws XmlDataAdaptor.ParseException, XmlDataAdaptor.ResourceNotFoundException Generate an XmlDataAdaptor from a urlPath, given dtd validating option, and given schemaUrl -
adaptorForUrl
public static XmlDataAdaptor adaptorForUrl(URL url, boolean isValidating) throws XmlDataAdaptor.ParseException, XmlDataAdaptor.ResourceNotFoundException Generate an XmlDataAdaptor from a urlPath and given dtd validating option -
adaptorForFile
public static XmlDataAdaptor adaptorForFile(File file, boolean isValidating) throws MalformedURLException, XmlDataAdaptor.ParseException, XmlDataAdaptor.ResourceNotFoundException Generate an XmlDataAdaptor from a urlPath and given dtd validating option -
adaptorForString
public static XmlDataAdaptor adaptorForString(String source, boolean isValidating) throws XmlDataAdaptor.ParseException, XmlDataAdaptor.ResourceNotFoundException Generate an XmlDataAdaptor from an XML string and given dtd validating option -
newDocumentBuilder
protected static DocumentBuilder newDocumentBuilder(boolean isValidating) throws SAXException, ParserConfigurationException Create a new document builder with the given DTD validation -
newDocumentBuilder
protected static DocumentBuilder newDocumentBuilder(boolean isValidating, URL schemaURL) throws SAXException, ParserConfigurationException Create a new document builder with the given DTD validation, and schemaUrl -
newDocumentAdaptor
public static XmlDataAdaptor newDocumentAdaptor(DataListener dataHandler, String dtdUri) throws XmlDataAdaptor.CreationException Create a new XmlDataAdaptor given a DataListener and a dtd URI- Throws:
XmlDataAdaptor.CreationException
-
newEmptyDocumentAdaptor
Create an XML document with only the doc tag and DTD URI specified -
newEmptyDocumentAdaptor
Create an empty XML document
-