You are not logged in.

#1 2006-03-01 19:03:36

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Parsing XML with Java - seeking advice for method to pursue

Hi guys

So here's the deal: I want to parse some XML from a server, get all the data I need from it, then shunt that data to the classes that need it. However, I'm really not sure what the best way of parsing the XML is.

I've just written a class for obtaining the file, and one for building a DOM from the XML file. But looking at org.w3c.dom in the Java API documentation (see HERE) I'll have to implement shedloads of interfaces to be able to take advantage of the DOM, and I don't even know a lot about it.

So am I best just writing a simple parser based on regular expressions to get the data I want? There's about 5 attributes in the XML file that I need to get - 5 for each instance of the containing element. So I don't think it'll be hard to do with regular expressions.. plus even if I did decide to implement all the DOM interfaces, the only way I'd know how to do half the stuff is with regular expressions anyway. I mean, how else would you do it? How else could you match up Nodes according to the Strings you're using for the elements, attributes etc?

I worry that a parser using regular expressions might be too slow... I'm building this as an applet to visually display information from the server. I have nothing to support those fears, I'm just not experienced enough to know whether speed would be a problem if I chose this route... I don't think it would, but really need confirmation of that suspicion being unfounded.

Any advice would be very, very welcome, as I'm tearing my hair out at the moment, unsure what to do.


.oO Komodo Dave Oo.

Offline

#2 2006-03-01 19:30:35

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Parsing XML with Java - seeking advice for method to pursue

You might want to look at the following page: http://www.oracle.com/technology/oramag … evxml.html.
It gives a good simple overview of XML parsing techniques. My immediate impression is that you could probably just go with SAX type parsing.

I would advise *not* writing your own. My experience is that you can write something that works for now but then the requirements change and you have to start over again. And again... And then you end up with really ugly code.

Good luck!

Offline

#3 2006-03-01 19:44:31

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Parsing XML with Java - seeking advice for method to pursue

Thanks for the info alterkacker.

The thing is, I'm working directly with the person producing the XML, and while one attribute might be altered in the future, the structure of the document is pretty much fixed, so doing it using regular expressions would be very simple...


.oO Komodo Dave Oo.

Offline

#4 2006-03-01 21:46:54

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Parsing XML with Java - seeking advice for method to pursue

I quite like JDOM for DOM-based parsing. I was able to get up and running very quickly with that. Apache Digester is quite nice to for extracting XML structures and mapping them directly to objects.

DOM approaches are not suited to large data sets - this is where SAX approaches come in. But I find SAX quite awkward, personally.

Offline

#5 2006-03-01 22:04:10

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Parsing XML with Java - seeking advice for method to pursue

But JDOM is not in the core class libraries, is it? So if I want to create an applet embedded into a website, am I able to get people to download that as well?

Everyone's advice is appreciated, but my core question remains unanswered - would using regular expressions, considering how simple and unchanging the XML files are, be a viable option in terms of speed?


.oO Komodo Dave Oo.

Offline

#6 2006-03-02 09:52:40

Rubinas
Member
Registered: 2005-02-21
Posts: 36

Re: Parsing XML with Java - seeking advice for method to pursue

try JAXB, its easy, fast and good way to parse xml...

Offline

#7 2006-03-02 10:51:15

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Parsing XML with Java - seeking advice for method to pursue

Komodo wrote:

But JDOM is not in the core class libraries, is it? So if I want to create an applet embedded into a website, am I able to get people to download that as well?

Sorry, I don't know anything about applets.

Komodo wrote:

Everyone's advice is appreciated, but my core question remains unanswered - would using regular expressions, considering how simple and unchanging the XML files are, be a viable option in terms of speed?

Yes! I've done more than my fair share of XML processing with REs. It's not always easy. I often wish I could just use something XPath-like. But it's certainly easy to do and would probably mean that you'd have something up and running quicker than if you spent time investigating pure XML parsing approaches.

Offline

#8 2006-03-02 10:52:25

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Parsing XML with Java - seeking advice for method to pursue

It looks good Rubinas, but no-one has answered my question about regular expressions -_-

I'd be grateful if someone would read the whole thread before posting.

Also, if I use something like JAXB, how do I include it with my applet so that those dl'ing the applet will dl JAXB as well? Because obviously they're not (unless by chance) going to have it already on their machine...


.oO Komodo Dave Oo.

Offline

#9 2006-03-02 15:22:03

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Parsing XML with Java - seeking advice for method to pursue

It's O.K. with me if you use R.E.s! If you're really sure you know the format of the XML data that you need to parse then go for it. To address your speed concerns you might want to write a little standalone Java program to benchmark the process & test out different expression and data combinations - R.E. parsing speed can vary widely depending on the R.E. engine & expression.

Offline

#10 2006-03-03 00:40:29

Komodo
Member
From: Oxford, UK
Registered: 2005-11-03
Posts: 674

Re: Parsing XML with Java - seeking advice for method to pursue

alterkacker wrote:

It's O.K. with me if you use R.E.s! If you're really sure you know the format of the XML data that you need to parse then go for it. To address your speed concerns you might want to write a little standalone Java program to benchmark the process & test out different expression and data combinations - R.E. parsing speed can vary widely depending on the R.E. engine & expression.

ok alterkacker, that's the sort of info I was after, many thanks! I'll give it a shot with R.E.s and see how things turn out.


.oO Komodo Dave Oo.

Offline

Board footer

Powered by FluxBB