• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to keep XML parsing code from becoming spaghetti ?  RSS feed

 
René Schmidt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few words of background .....

I'm using Java 8, and org.apache.commons.configuration.XMLConfiguration to pull a config from a file. I'm also a bit of a Java newbie, but can recgognise my code is quickly turning into mush !

We'll start with the XML :



There may be :
- Multiple <file> instances
- Multiple target buckets (as illustrated in the above example)


And here is my current horrific code ! So far, I've managed to keep the rest of the code for this app short and sweet, but I don't know how I can keep the XML parsing clean ?

 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Can you see what portion of code is "similar"? I see 2 chunks

You can take a chunk of code and put it in a method. This way will make your original smaller and neater.

I would suggest you can make L30-L58 into 1 method and L60-L89 into another. This is just a start.
 
René Schmidt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang ,

Indeed, I can spot the repetitive bits (looping for the possibilities file/buckets).

I just wasn't sure if it was likely to be fixable with a method (or indeed if I'm just likely to sweep the problem under a carpet by "hiding" it in a method).

I'll take a look at it and see what I can come up with.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great you spotted the somewhat repetitive code.

I see the major difference is one set is for "one" file (file.buckets.bucket[j]) and the other collection (file[i].buckets.bucket[j])

The thing is the number of file. Processing 1 file entry or many should be the same (it's just the loop count that is different)
 
René Schmidt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:I see the major difference is one set is for "one" file (file.buckets.bucket[j]) and the other collection (file[i].buckets.bucket[j])


Yeah, that's probably my primary problem.

My understanding is that Commons XMLConfiguration only returns a collection when there is more than one instance, otherwise it does not return a connection object.

Which is why I have to do that check (prop instanceof Collection) first and treat one vs multiple differently.

Unless I've misunderstood ?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commons Configuration provides flexibility for handling various input formats, but if it's only XML you're concerned with, Commons Digester might be a better fit.
 
René Schmidt
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it must be said that Apache's documentation is pretty lousy when it comes to real-life use-cases.

However a bit of google-fu to find other people's open-source java source code, and I eventually came up with the below optimisation !

If I there are ways to do better than that, let me know !

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!