structuring pull parsing code and using serializer [Re: [xmlpull-user] Java <-> XML using XMLPull]
Aleksander Slominski <[email protected]> Wed, 26 Feb 2003 00:30:10 -0500
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
Naresh Bhatia wrote:
> I am trying to serialize / deserialize some Java objects to XML and
> back. I am using XML namespaces and schemas. I have looked at the
> XMLPull example code, but wanted to get some pointers before I start
> serious coding. Below is a sample of what my XML doc looks like. Note
> that Message / CompositeMessage form a "Composite" pattern that allow
> specification of recursive message structures. All messages are
> specified with a <message> tag with the concrete type specified by an
> xsi:type attribute.
>
> <message
> xmlns="http://www.user.org/user-schema"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:type="CompositeMessageType">
>
> <message xsi:type="CreateUserMessageType">
> <user>
> <username>user1</username>
> <password>user1</password>
> </user>
> </message>
>
> <message xsi:type="DeleteUserMessageType">
> <username>user2</username>
> </message>
>
> </message>
>
>
> 1) Can you recommend any documents I should read to construct a
> framework above the XMLPull API so that my design is clean. I started
> coding a small portion of my schema and I can see that it can quickly
> get out of hand unless I structure my code using some well thought-out
> strategy.
>
hi,
i started working on some design patterns for XML pull parsing
(disclaimer: this _is_ work in progress) it is here:
http://www.extreme.indiana.edu/~aslom/xmlpull/patterns.html
let me know what you think about it - i am interested to hear if it
addresses some of your concerns
> 2) Same question for serializing. Do you recommend any documents or
> frameworks I should follow for serializing? The important thing here
> is that the XML produced should be fully compliant with my schema,
> producing all xmlns and xsi attributes as shown above.
>
that is why we have added XmlSerializer to XmlPull API to create a
really easy to use to use framework to write XML with namespaces (at
least we did every effort to make it so and will be happy to hear about
any problems) that allows full control over XML output if needed.
i have just finished writing a quick tutorial on writing XML output with
XmlSerializer please see:
http://www.xmlpull.org/v1/download/unpacked/doc/quick_write.html
find below an example program to write your sample output, if you run it
with kXML2 you will see:
serializer implementation class is class org.kxml2.io.KXmlSerializer
<message xmlns="http://www.user.org/user-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="CompositeMessageType"><message
xsi:type="CreateUserMessageType"><username>user1</username><password>user1</password></message><message
xsi:type="DeleteUserMessageType"><username>user1</username></message></message>
if you use current vereion of XPP3 from CVS you will get output like
that as line indentation property is supported now in CVS (if
indentation property is not supported you will need to add spaces and
new lines manually or you get output like this)
serializer implementation class is class
org.xmlpull.mxp1_serializer.MXSerializer
<message xsi:type="CompositeMessageType"
xmlns="http://www.user.org/user-schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<message xsi:type="CreateUserMessageType">
<username>user1</username>
<password>user1</password>
</message>
<message xsi:type="DeleteUserMessageType">
<username>user1</username>
</message>
</message>
thanks,
let me know if you have more questions.
alek
ps. here is sample program to produce output that you want:
import java.io.IOException;
import java.io.PrintWriter;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;
public class Example9 {
private final static String NAMESPACE = "http://www.user.org/user-schema";
private final static String NAMESPACE_XSI = "http://www.w3.org/2001/XMLSchema-instance";
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();
System.out.println("serializer implementation class is "+serializer.getClass());
serializer.setOutput(new PrintWriter(System.out));
// now try to set some nice indenting -- need to catch exception as this is optional in API
try {
serializer.setProperty(
"http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
} catch(Exception ex) {
}
// set prefixes
serializer.setPrefix("", NAMESPACE);
serializer.setPrefix("xsi", NAMESPACE_XSI);
serializer.startTag(NAMESPACE, "message");
serializer.attribute(NAMESPACE_XSI, "type", "CompositeMessageType");
writeUser(serializer, "CreateUserMessageType", "user1", "user1");
writeUser(serializer, "DeleteUserMessageType", "user1", null);
serializer.endTag(NAMESPACE, "message");
serializer.endDocument();
}
private static void writeUser(XmlSerializer serializer, String messageType,
String user, String password)
throws IOException
{
// now loop over entries
serializer.startTag(NAMESPACE, "message");
serializer.attribute(NAMESPACE_XSI, "type", messageType);
serializer.startTag(NAMESPACE, "username")
.text(user)
.endTag(NAMESPACE, "username");
if(password !=null) {
serializer.startTag(NAMESPACE, "password")
.text(user)
.endTag(NAMESPACE, "password");
}
serializer.endTag(NAMESPACE, "message");
}
}
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/FpY02D/vN2EAA/xGHJAA/2U_rlB/TM
---------------------------------------------------------------------~->
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/