eskgwin wrote:
>I have some java code (in jbuilder 7) that has data in a vector. I
>want to get all this data out to an xml file. It is not in xml format
>at all. This is part of the vector:
>
>[FH|RCP-SDM|1.0, T|ABC|123456.123N|1234567.123W .....
>
>I want to be able to get this data in this format in an xml file
>without brute forcing it (and I guess I will do that if I have to,
>but I would rather not):
>
><table>
> <entry>
> <Header>FH</Header>
> <Title>RCP-SDM</Title>
> <Version>1.0</Version>
> </entry>
> <Header>T</Header>
> <Name>ABC</Name>
> <Latitude>123456.123N</Latitude>
> <Longitude>1234567.123W</Longitude>
> </entry>
></table>
>
>Can this be done using xmlpull or not? Any help you can give me would
>be great. Thanks.
>
hi Allyson,
that should be very easy - use XmlSerializer.
for example to write beginning of your sample XML:
import java.io.IOException;
import java.io.StringWriter;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
public class Example8 {
public static void main (String args[])
throws XmlPullParserException, IOException
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(
System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null);
//"org.xmlpull.mxp1.MXParserFactory", null);
XmlSerializer serializer = factory.newSerializer();
StringWriter sw = new StringWriter();
serializer.setOutput(sw);
System.out.println("serializer implementation class is
"+serializer.getClass());
// write first start tag
serializer.startTag(null, "table");
// now loop over entries
serializer.startTag(null, "entry");
serializer.startTag(null, "Header");
serializer.text("FH");
serializer.endTag(null, "Header");
serializer.startTag(null, "Title");
serializer.text("RCP-SDM");
serializer.endTag(null, "Title");
serializer.startTag(null, "Version");
serializer.text("1.0");
serializer.endTag(null, "Version");
serializer.endTag(null, "entry");
// repeat the same for other entires
// ...
// this alternative way to use API by chaining method calls ...
serializer.startTag(null, "Header").text("T").endTag(null, "Header");
serializer.startTag(null, "Name").text("ABC").endTag(null, "Name");
serializer.startTag(null, "Latitude").text("123456.123N").endTag(null,
"Latitude");
serializer.startTag(null, "Longitude").text("1234567.123W").endTag(null,
"Longitude");
// close table tag
serializer.endTag(null, "table");
serializer.endDocument();
System.out.println("serializer output: "+sw);
}
}
note that null is used as parameter to start/endTag to make sure that
output has no namespaces this is explained in javadoc:
http://www.xmlpull.org/v1/doc/api/org/xmlpull/v1/XmlSerializer.html#startTag(jav\
a.lang.String,%20java.lang.String)
if you run this example then it produces following output:
serializer implementation class is class
org.xmlpull.mxp1_serializer.MXSerializer
serializer output:
<table><entry><Header>FH</Header><Title>RCP-SDM</Title><Version>1.0</Version></e\
ntry><Header>T</Header><Name>ABC</Name><Latitude>123456.123N</Latitude><Longitud\
e>1234567.123W</Longitude></table>
upcoming version of XPP3 supports optional serializer properties for
pretty printing so you would be able to get exact output you want, right
now it is just XML (you can add you indentation by writing spaces and
new lines but it is painful), see:
http://xmlpull.org/v1/doc/properties.html#serializer-indentation
let me know if you have any problems with it.
thanks,
alek
--
"Mr. Pauli, we in the audience are all agreed that your theory is crazy.
What divides us is whether it is crazy enough to be true." Niels H. D. Bohr