public class XMLObjectFactory
extends java.lang.Object
The XMLObjectFactory can be used to manage the construction of any object
based on an XML description. This class uses reflection to set properties
and call arbitrary methods of the object under construction.
Example:
// Parse the XML document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("example.xml"));
// Here, construct is the XML element to read the configuration from
Element description = (Element) doc.getElementsByTagName("construct").item(0);
// The document is only necessary for idrefs, there is a
// second constructor that accepts just the element
XMLObjectFactory factory = new XMLObjectFactory(doc, description);
// Finally, call newObject() whenever you want to construct a new
// object according to the configuration in the given XML element
Object object = factory.newObject();
Each time XMLObjectFactory encounters a class attribute in the XML, an
instance of the specified class is instantiated. Nested elements are
interpreted as either properties to be set or methods to be called on this
newly constructed object. If no class attribute is specified then the element
is interpreted as a Java primitive type or a string. Elements can be nested in
a hierarchical fashion to handle the construction of complex objects.
Note: Requires the Java API for XML processing (JAXP).
See: Factory Demo for XML samples.| Constructor and Description |
|---|
XMLObjectFactory(org.w3c.dom.Document xmlDocument,
org.w3c.dom.Element xmlObjectDescription)
Creates a new instance of
XMLObjectFactory for constructing objects
given an XML description and an XML document for handling idrefs. |
public XMLObjectFactory(org.w3c.dom.Document xmlDocument,
org.w3c.dom.Element xmlObjectDescription)
XMLObjectFactory for constructing objects
given an XML description and an XML document for handling idrefs.xmlObjectDescription - An XML element that describes the objects to be constructed by this factory.xmlDocument - The XML document to search for any idrefs.public java.lang.Object newObject()
Object based on the underlying XML object description.Object constructed according to the given description.FactoryException - In case the object cannot be constructed.protected void error(org.w3c.dom.Element element,
java.lang.String message)