Package xal.tools.xml

Class XmlDataAdaptor

java.lang.Object
xal.tools.xml.XmlDataAdaptor
All Implemented Interfaces:
DataAdaptor, FileDataAdaptor

public class XmlDataAdaptor extends Object implements 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 xAdaptors = parentAdaptor.childAdaptors("X")
List allAdaptors = parentAdaptor.childAdaptors()
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