Java Bean to DOM Document Wrapper (Bean2DOM)
Submitted by jscheffl on 27. March 2005 - 23:00
Small Java Wrapper around any Java object to gather XML DOM Level 1 trees.
Target of this implementation is to retrieve DOM XML from any Java object without use of any mapping file. The result can be used to process any bean or to serialize it. All access to objects is done using reflection. In this way all public members of the class are available.
Besides other projects (e.g. Jakarta Betwixt) this code does not use mapping files but as trade-off is may not be too performant.
Features
- Runs in Java 1.4++
- Works with any Java object
- Uses Java reflection mechanisms to retrieve object information - no mapping file required
- Filtering can suppress unwanted results to optimize output
- Special object mappings can be implemented to extend the basic resolution
- Mappings for Base types, Collections and Maps are already included
Limitations
- Due to reflection based approach not too fast - please use any alternative solution if you need speed.
- The recursion depth should be limited to a minimum to not generate endless recursion.
Usage
Just include the JAR file into your project and class path
Example 1
The following code:
String toBeOutputted = "String as XML";
try {
BeanDocumentBuilder beanBuilder = new BeanDocumentBuilder();
beanBuilder.setMaxDepth(2);
Document xml = beanBuilder.getDocument(toBeOutputted);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(xml), new StreamResult(System.out));
} catch (Exception e) {
System.err.println("Error during transformation: " + e);
e.printStackTrace();
}
Generates the output:
<?xml version="1.0" encoding="UTF-8"?>
<String className="java.lang.String">String as XML</String>
Example 2
The following code:
Object xmlSource = System.getProperties();
try {
BeanDocumentBuilder beanBuilder = new BeanDocumentBuilder();
beanBuilder.setMaxDepth(2);
Document xml = beanBuilder.getDocument(xmlSource);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.transform(new DOMSource(xml), new StreamResult(System.out));
} catch (Exception e) {
System.err.println("Error during transformation: " + e);
e.printStackTrace();
}
Generates the output:
<?xml version="1.0" encoding="UTF-8"?>
<Properties className="java.util.Properties">
<propertyNames className="java.util.Hashtable$Enumerator"/>
<hashCode className="java.lang.Integer">1436952335</hashCode>
<toString className="java.lang.String">{java.runtime.name=Java(TM) 2 Runtime Environment(...)</toString>
<size className="java.lang.Integer">54</size>
<values className="java.util.Collections$SynchronizedCollection"/>
<elements className="java.util.Hashtable$Enumerator"/>
<keys className="java.util.Hashtable$Enumerator"/>
<entrySet className="java.util.Collections$SynchronizedSet"/>
<isEmpty className="java.lang.Boolean">false</isEmpty>
<keySet className="java.util.Collections$SynchronizedSet"/>
<getClass className="java.lang.Class"/>
<element key="java.runtime.name" className="java.lang.String">Java(TM) 2 Runtime Environment</element>
<element key="java.vm.version" className="java.lang.String">1.4.2_05-b04</element>
<element key="java.vm.vendor" className="java.lang.String">Sun Microsystems Inc.</element>
<element key="java.vendor.url" className="java.lang.String">http://java.sun.com/</element>
<element key="path.separator" className="java.lang.String">;</element>
<element key="java.vm.name" className="java.lang.String">Java HotSpot(TM) Client VM</element>
(...)
</Properties>
System Requirements
- Java 1.4++
Change Log
- none (yet)
Attachment | Size |
---|---|
bean2dom 1.0.zip | 17.12 KB |
bean2dom Source 1.0.zip | 269.95 KB |