RE: structuring pull parsing code and using serializer [Re: [xmlpull-user] Java <-> XML using XMLPull]
"Naresh Bhatia" <[email protected]> Thu, 27 Feb 2003 00:44:29 -0500
| Newsgroups | gmane.text.xml.xmlpull.devel |
|---|---|
| Message-ID | <[email protected]> |
Great stuff! Just what I needed! I am starting to integrate the concepts
into my application. I will post if I have need any clarification.
One quick question. In your MyAddressBook class, you have lines with
asserts in them. For example:
assert parser.getEventType() == parser.START_TAG;
What is this? Some external processor to use asserts? These lines are
not compiling.
Thanks.
Naresh
-----Original Message-----
From: Aleksander Slominski [mailto:[email protected]]
Sent: Wednesday, February 26, 2003 12:30 AM
To: [email protected]
Cc: [email protected]
Subject: [xmlpull-dev] structuring pull parsing code and using
serializer [Re: [xmlpull-user] Java <-> XML using XMLPull]
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>use
r1</password></message><message
xsi:type="DeleteUserMessageType"><username>user1</username></message></m
essage>
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
ADVERTISEMENT
<http://rd.yahoo.com/M=246920.2960106.4328965.2848452/D=egroupweb/S=1706
030390:HM/A=1464858/R=0/*http://www.gotomypc.com/u/tr/yh/cpm/grp/300_Cqu
o_1/g22lp?Target=mm/g22lp.tmpl>
<http://us.adserver.yahoo.com/l?M=246920.2960106.4328965.2848452/D=egrou
pmail/S=:HM/A=1464858/rand=826224572>
To unsubscribe from this group, send an email to:
[email protected]
Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service <http://docs.yahoo.com/info/terms/> .