Search the web
Sign In
New User? Sign Up
xmlpull-user
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
writing to xml file   Message List  
Reply | Forward Message #29 of 308 |
Re: [xmlpull-user] writing to xml file

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






Fri Feb 14, 2003 4:36 pm

as10m
Offline Offline
Send Email Send Email

Forward
Message #29 of 308 |
Expand Messages Author Sort by Date

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...
eskgwin <eskgwin@...>
eskgwin
Offline Send Email
Feb 14, 2003
3:57 pm

... hi Allyson, that should be very easy - use XmlSerializer. for example to write beginning of your sample XML: import java.io.IOException; import...
Aleksander Slominski
as10m
Offline Send Email
Feb 14, 2003
4:37 pm
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help