DOM API:
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
public class Main {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document xmldoc = factory.newDocumentBuilder().getDOMImplementation().createDocument(null, "PhoneBook", null);
Element root = xmldoc.getDocumentElement();
Element e = xmldoc.createElementNS(null, "BookRecord");
Node n = xmldoc.createTextNode("Value");
e.appendChild(n);
root.appendChild(e);
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.setOutputProperty(OutputKeys.INDENT,"yes");
serializer.transform(new DOMSource(xmldoc), new StreamResult(new PrintWriter(System.out)));
}
}
SAX API:
import java.io.PrintWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.helpers.*;
public class Main {
public static void main(String argv[]) throws Exception {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
hd.getTransformer().setOutputProperty(OutputKeys.INDENT,"yes");
hd.setResult(new StreamResult(new PrintWriter( System.out)));
hd.startDocument();
hd.startElement("", "", "PhoneBook",new AttributesImpl());
hd.startElement("", "","BookRecord", new AttributesImpl());
hd.characters("Value".toCharArray(), 0, 5);
hd.endElement("", "", "BookRecord");
hd.endElement("", "", "PhoneBook");
hd.endDocument();
}
}
JDOM API:
package com;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class Main {
public static void main(String argv[]) throws Exception {
Document doc = new Document();
Element e = new Element("PhoneBook");
e.addContent(new Element("BookRecord").setText("Value"));
doc.addContent(e);
XMLOutputter outp = new XMLOutputter();
Format f = Format.getPrettyFormat();
//f.setIndent(" ");
outp.setFormat(f);
outp.output(doc, System.out);
}
}
As result in all cases:
<?xml version="1.0" encoding="UTF-8"?>
<PhoneBook>
<BookRecord>Value</BookRecord>
</PhoneBook>
XML Serialization with java.beans.XMLEncoder class
import java.beans.XMLEncoder;
public class Main {
public static void main(String[] args) throws Exception {
XMLEncoder xenc = new XMLEncoder(System.out);
xenc.writeObject("Some String");
xenc.flush();
xenc.close();
}
}
Output:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_06" class="java.beans.XMLDecoder">
<string>Some String</string>
</java>
Or more interesting:
import java.beans.XMLEncoder;
public class Main {
public static void main(String[] args) throws Exception {
Foo[] arr = {new Foo(), new Foo()};
XMLEncoder encoder = new XMLEncoder(System.out);
encoder.writeObject(arr);
encoder.close();
}
public static class Foo {
private int foo = 10 ;
public int getFoo() {
return foo;
}
public void setFoo(int foo) {
this.foo = foo;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_06" class="java.beans.XMLDecoder">
<array class="com.Main$Foo" length="2">
<void index="0">
<object class="com.Main$Foo"/>
</void>
<void index="1">
<object class="com.Main$Foo"/>
</void>
</array>
</java>
As you can see - not so optimistic. For the things to be serialized propery, you need bean pattern getter methods, and sometimes even setters: so for simple java bean serialization will work fine.
XStream - a lightweight open source Java library for serializing Java objects to XML and back again.
import com.thoughtworks.xstream.XStream;
public class Main {
public static void main(String[] args) throws Exception {
XStream xstream = new XStream();
xstream.alias("Foo", Foo.class);
String xml = xstream.toXML(new Foo());
System.out.println(xml);
}
public static class Foo {
public int fooInt = 10 ;
public String fooStr = "20";
}
}
<Foo>
<fooInt>10</fooInt>
<fooStr>20</fooStr>
</Foo>
In case of List it behave too pretty good:
List<Foo> list = new LinkedList<Foo>();
list.add(new Foo());
list.add(new Foo());
String xml = xstream.toXML(list);
Result:
<linked-list>
<Foo>
<fooInt>10</fooInt>
<fooStr>20</fooStr>
</Foo>
<Foo>
<fooInt>10</fooInt>
<fooStr>20</fooStr>
</Foo>
</linked-list>
No comments:
Post a Comment