RE: structuring pull parsing code and using serializer [Re: [xmlpull-user] Java <-> XML using XMLPull]

"Naresh Bhatia" <[email protected]> Fri, 28 Feb 2003 19:25:24 -0500
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
Hi Alek,
 
I have now worked extensively with XMLPull API and your XmlPullWrapper -
I love this stuff! I have tried several other approaches ranging from
SAX all the way to the latest data binding packages, but they all fell
short of my expectations. Some could not get the job done (especially
when it came to namespaces), others just did not present elegant
solutions (I am very picky about my designs and code :->). Anyway I am
really impressed by the elegance of the XMLPull API and I have decided
to use it for my project.
 
In an effort to make the API even more easier to use, I have the
following suggestions. They are mainly directed at XmlPullWrapper and
your AddressBook example. I have refactored the code a little to
demonstrate how this approach could be used on larger projects. My
changes are attached along with an Ant build file.
 
- I have refactored the AddressBook example into 4 classes.
MyAddressBook.java now contains only the main( ) method. The read
methods have been refactored into the AddressBookReader class and the
write methods have been moved to the AddressBookWriter class. The Person
class is now in its own seperate file as this is more realistic on a
real project.
 
- AddressBookReader inherits from XmlPullWrapper. This gives me access
to the parser in all methods - the parser is no longer passed around in
method signatures. In addition, I do not have to "create" an
XmlPullWrapper object whenever I need to use it - I am the
XmlPullWrapper! This also resulted in more frequent use of the
XmlPullWrapper methods as they are more accessible!
 
- I added a class called XmlSerializerWrapper. The idea is similar to
XmlPullWrapper. This class holds convenience methods for XmlSerializer.
Currently I have only one method in this class called
writeSimpleElement( ), which allows me to write simple elements such as
<username>johndoe</username> in one call.
 
- Added a method to XmlPullWrapper called getRequiredElementText( ).
This method allows you to read simple elements such as
<username>johndoe</username> in one call.
 
- Fixed a minor bug in XmlPullWrapper.nextEndTag() - it was going to the
next start tag insted of the next end tag - probably a cut&paste error
from nextStartTag()  :-)
 
- I would recommend that XmlPullWrapper and XmlSerializerWrapper be
supplied along with the XMLPull API - may be not as part of the API but
atleast as a supported utilities. This will encourage people to use
"best practices" and the wrappers will be thoroughly tested. In
addition, there will be only one standard version of the wrappers
instead of Alek's version and Naresh's version :-). Of course, people
are free to extend these utilities as they feel fit, but the baseline is
always the same.
 
Here's the new main( ) function in MyAddressBook.java after the
refinements mentioned above. It is much more concise.
 
    public static void main (String args[])
    throws XmlPullParserException, IOException {
        
        System.out.println("reading address book from XML");
        AddressBookReader addressBookReader =
            new AddressBookReader(new StringReader(SAMPLE_XML));
        Vector addressBook = addressBookReader.readAddressBook();
 
        //add new entry to address book
        addressBook.add(new Person("Foo Bar", "Silicon Street"));
 
        System.out.println("serializing address book to XML");
        StringWriter sw = new StringWriter();
        AddressBookWriter addressBookWriter = new AddressBookWriter(sw);
        addressBookWriter.writeAddressBook(addressBook);
        System.out.println("Addressbook as XML:\n"+ sw);
    }

I would love to get your feedback on this stuff!
 
Regards,
Naresh

	-----Original Message-----
	From: Aleksander Slominski [mailto:[email protected]] 
	Sent: Thursday, February 27, 2003 1:40 AM
	To: [email protected]
	Subject: Re: [xmlpull-dev] structuring pull parsing code and
using serializer [Re: [xmlpull-user] Java <-> XML using XMLPull]
	
	
	Naresh Bhatia wrote:
	
	> 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.
	
	good to hear this :-)
	
	
	> 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.
	
	just delete it (it should not be there uncommented) - it can
only be 
	compiled and executed using JDK 1.4
	
	to learn more about assert in JDK 1.4 check:
	
http://java.sun.com/j2se/1.4/docs/guide/lang/assert.html#compiling
	http://java.about.com/library/tutorials/bltut-014-1.htm
	
	thanks,
	
	alek
	
	
	
	
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=789055761> 	

	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/> .
MyAddressBook.zip (application/x-zip-compressed, 7.5 KB) - not displayed