This week's book giveaway is in the Open Source forum.
We're giving away four copies of Programmers Guide to Apache Thrift and have Randy Abernethy on-line!
See this thread for details.
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Parsing with JDOM  RSS feed

 
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If you have an XML structure like this:



how do you visit all the DATA nodes using JDOM (to get the dates)? I've tried to write code, but I just end up visiting the parent nodes.

There can be multiple DATA nodes; we would not know how many of these ahead of time.

Thanks much in advance,

- mike
 
Marshal
Posts: 24460
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a very long time since I used JDOM but I'm sure it had methods which allowed you to navigate through the children of a node. But I expect you're already trying to use those methods; if so then perhaps you could post the code which isn't acting the way you would like?
 
Mike London
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's a very long time since I used JDOM but I'm sure it had methods which allowed you to navigate through the children of a node. But I expect you're already trying to use those methods; if so then perhaps you could post the code which isn't acting the way you would like?



If you have a better suggestion for XML parsing, that would be welcome as well.

The problem I have is that all the examples online are simple with a simple structure. A real XML document with multiple elements and some of those elements being nested doesn't seem to be simple to parse. So far, I have not found a single example.

Thanks,

- mike
 
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem I have is that all the examples online are simple with a simple structure. A real XML document with multiple elements and some of those elements being nested doesn't seem to be simple to parse. So far, I have not found a single example.


Nesting shouldn't over-complicate things that much. Root node has children. Those children are possibly root nodes to their children. Those children are possibly...

As mentioned, post the actual XML file structure you have and post the code you wrote.

 
Mike London
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code that works. It's tied to the specific XML file, but I guess all XML Parsing code is. The code below specifically, not generically, knows the structure of the XML document and has nested blocks to handle it.

(Note: I changed the element name from Data to Date).

I'm assuming if you wanted to, say, change the value of a date in an XML file, it might be easier to just use a regex search and replace rather than trying to parse the XML document like this.

- mike

 
Liutauras Vilda
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just pulled that jdom (actually jdom2) library and gave a try, that is a quick solution against your file:

However, if you know little to nothing about the the file structure beforehand, that might be a bit more complicated by having few more checks, but that shouldn't be much more complicated. Recursion might be helpful, but dangerous though and probably used as a last resort.

Don't know whether Regex is a right way to go if there is a talk about the XML parsing, modifying it. That is why this library has been created for.

Let's see what others think.
 
Saloon Keeper
Posts: 5475
143
Android Firefox Browser Mac OS X Safari Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I strongly advise against using regexps for XML manipulation. Given that XML can contain all kinds of stuff in CDATA sections, it's a brittle approach compared to using one of the available libraries (XOM and dom4j are others).
 
Mike London
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:I strongly advise against using regexps for XML manipulation. Given that XML can contain all kinds of stuff in CDATA sections, it's a brittle approach compared to using one of the available libraries (XOM and dom4j are others).



Thanks, but ...


Didn't find anything.
 
Liutauras Vilda
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike London wrote:Didn't find anything.


For me it did and it still does.
 
Mike London
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Mike London wrote:Didn't find anything.


For me it did and it still does.



My bad. Sorry. I had changed DATA to DATE. Yep, it works.

Very nice, thanks very much.

- mike
 
Liutauras Vilda
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but what's next? Did we have any problem to solve? Or that was the problem...
 
Ranch Hand
Posts: 725
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice this <?Xml ...>. The prolog is written <?xml ...>, uppercase X will not do.

jdom2 with all its nice or less nice constructions has its convenience methods trying to make it stand out in competition.

First you define an element filter

or pack it into the one-liner if you like.

Then you use getDescendants() to get to your target elements DATA (or DATE if that's a typo).

 
Liutauras Vilda
Sheriff
Posts: 6738
466
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g tsuji, have a cow.

Certainly more elegant way to do that in this particular case.
 
Mike London
Bartender
Posts: 1664
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:g tsuji, have a cow.

Certainly more elegant way to do that in this particular case.



Man, that's amazing!  I wasn't even aware you could chain methods like that off the SAXBUilder().

JDOM is an odd beast in that it's such a sweet API on the one hand, but, on the other hand, it's nearly impossible to find through documentation with plentiful examples.

Regardless, seems like how ever much I "think" I know, there's still so much more to learn (since starting Java in 2002). The Java 8 syntax still eludes me sometimes. I like its declarativeness, but often, what I want to do is complicated and raises the question whether it's the best way to go vs. the formerly-traditional coding methods. In my case, I make small (meant to be searched) example programs for future reference.

Thanks again..Oh, I did find that capitalization problem and fixed it.

--

The actual problem was that the XML file had one or more incorrectly formatted dates that would not import into the third-party application. So, in the end, rather than using JDOM at all, I just read the XML file line by line and searched for the incorrect formatted date. Then if found, fixed it, and wrote the fixed date and the rest of the XML file to a new file.

You guys are amazing!

Best,

- mike
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!