Internally parse XML

Put your problem here if it does not fit any of the other categories.

Internally parse XML

Postby timwebuk » Thu Jun 24, 2010 7:47 pm

Hi everyone, my first post here. So go easy! :)

I've started making an application and my overall plan is to exchange data between objects and methods with short XML strings. However, I'd like to write a few simple, generic reusable XML methods like:

getRootTag, getTagContents, etc. That I can reuse constantly throughout my project, so obviously I need to write a class and these methods. However I'm pretty much stuck understanding whether I need to use DOM or SAX. An example of what I'd like to do is:

- Send an HTTP Request by storing all the parameters in an XML string, passing the string to my HTTP class where it parses the XML, constructs an HTTP request and sends it.

Any ideas guys? Sorry if this seems a bit jumbled, if you need me to clarify, please let me know!
VB6/C++/XML programmer - moving to the future, Android!
timwebuk
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 24, 2010 7:36 pm

Top

Re: Internally parse XML

Postby Ath » Thu Jun 24, 2010 8:40 pm

Why use XML? The XML specification is rather bloated having a big data to packaging ratio, especially on small data, compared to other 'packaging' protocols like json. If it's for (app) internal communication, either go for plain strings, or the equally compact json protocol.
Examples/comparison for json vs. xml are here
/Ath
Ath
Developer
Developer
 
Posts: 42
Joined: Sun Jun 06, 2010 6:07 pm
Location: NL

Re: Internally parse XML

Postby timwebuk » Thu Jun 24, 2010 8:52 pm

The reason why I planned on using XML strings is they are easy to validate in code and pass lots of different 'strings' within a string that can later be separated.

To me calling a method and passing it a plain string just seems a bit clunky... but I'm really not sure anymore haha. I just want to get off to a good start rather than start coding something I'll want to change later.
VB6/C++/XML programmer - moving to the future, Android!
timwebuk
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 24, 2010 7:36 pm

Re: Internally parse XML

Postby timwebuk » Thu Jun 24, 2010 9:57 pm

I've decided to go for the XML way for now, even if it is bulky I need to do it this way to get a foundation in Android and get some practice. Could anyone give me a hand creating what I'm after? All the tutorials I've found are about reading feeds and are huge chunks of code, unexplained.

Pseudo code would be:

Grab user input from form and store in variables
Build an XML string containing all the parameters
Pass this string to my HttpRequest class
Within HttpRequest class I dissect the XML string and retrieve parameters to build an HTTP request
Send HTTP request.


I just need help with the taking the string passed as a parameter to the method and dissecting the tags. Could anyone provide any assistance?
VB6/C++/XML programmer - moving to the future, Android!
timwebuk
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 24, 2010 7:36 pm

Re: Internally parse XML

Postby padde » Thu Jun 24, 2010 10:19 pm

I still dont get why you want to make a detour via XML.. its absolutly unnecessary and overkill for internal
communication. It makes really no sense sorry...
There are already objects like HttpRequest or HttpUriRequest.. build one of them from your "input form"
an then simply execute that with your HttpClient object.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Internally parse XML

Postby vengeance_mj » Thu Jun 24, 2010 10:47 pm

well if you do want to go via the XML route, check out plusminus's wonderful tutorial on how to use a SAXParser. A SAXParser is faster than a DOM parser so i suggest you go with that.
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Top

Re: Internally parse XML

Postby timwebuk » Thu Jun 24, 2010 11:36 pm

padde wrote:There are already objects like HttpRequest or HttpUriRequest.. build one of them from your "input form"
an then simply execute that with your HttpClient object.


They wouldn't be as generic as custom XML strings passed to methods though surely.

I did look at that SAXParser but it seemed to be for taking an XML file from a URI.
VB6/C++/XML programmer - moving to the future, Android!
timwebuk
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 24, 2010 7:36 pm

Re: Internally parse XML

Postby vengeance_mj » Thu Jun 24, 2010 11:41 pm

well the SAXParser only needs an input source, so well you can probably store it in a temp file and then wrap a FileInputReader inside the InputSource of XMLReader .
vengeance_mj
Senior Developer
Senior Developer
 
Posts: 174
Joined: Thu Oct 29, 2009 2:32 am

Re: Internally parse XML

Postby padde » Fri Jun 25, 2010 4:32 am

You can be exactly generic as you would with xml with a simple object.. but a simple object would be
less overhead and i guess an hundred times faster because you dont have to parse anything.
XML is not intended for something like that.. but if you suffer from some kind of XML fetish you should
go with the SAXParser. As vengeance_mj already said it just needs an InputSource and that could be anything..
a string, a file, a stream from a url .. etc.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: Internally parse XML

Postby anarche » Fri Jun 25, 2010 5:31 am

Dude, move to the future, Android!

you don't need XML and your app will suffer for the overhead (I haven't used json but I trust the above posters re comparison efficiencies).

=p
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Internally parse XML

Postby timwebuk » Fri Jun 25, 2010 1:41 pm

Haha ok! You all seem to have made your point, I guess XML would only be useful if I was using a multiple layered piece of software - the best way to pass messages (and multiple parameters) between different languages.

Thanks for your help, I'll be sure to stick around these forums, they seem very useful.
VB6/C++/XML programmer - moving to the future, Android!
timwebuk
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 24, 2010 7:36 pm

Re: Internally parse XML

Postby Johan Degraeve » Fri Jun 25, 2010 1:59 pm

In case you still want to use XML, i've been developing a package that allows to build custom class and to create and parse XML.
For the parsing I use SAX.

For creating xml :

For every XML element, you define your own class. Each of your classes needs to implement the interface "XMLElement"

You implement every method, but in your case (creating XML), you need "getAttributes", "getChildren", "getTagname", "getText" and "preserveSpaces"

then with Utilities.createXML you can create an XML representation.
whereby input is your root element.

The only thing still missing is the XML declaration. This is still a bit in development, but it works quite well.

The package can be downloaded here
regards,

Johan
Johan Degraeve
Experienced Developer
Experienced Developer
 
Posts: 55
Joined: Tue Oct 27, 2009 1:50 pm
Location: Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests