Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 11th, 2002, 05:07 AM
Hoggie Hoggie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Ireland
Posts: 10 Hoggie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
JDOM: Reading xml doc problem

Hello,

I do appologise if this does not fall under the realm of pure "server side" apps. But I like this place, it has a lot of intelligent people with good idea, so here I go!

I have an xml doc eg:
- <report>
- <sub_report>
<date required="Yes">Friday, January 11, 2002(0830)</date>
<heading>Report BLAH</heading>
- <sub_heading>
<sub_heading_name>Customer</sub_heading_name>
</sub_heading>
- <sub_heading>
<sub_heading_name>Number</sub_heading_name>
</sub_heading>
- <sub_heading>
<sub_heading_name>Counterparty</sub_heading_name>
</sub_heading>
- <data_row>
<data>BLAH</data>
<data>100</data>
<data>AAA Ltd.</data>
</data_row>
- <data_row>
...
</data_row>
</sub_report>
<sub_report>
...
</sub_report>
...
</report>

So each report has a sub_report. The problem I am getting is that when I use Jdom to extract the data from the xml doc, it looks at the first sub_report it meets and proceeds to parse though it. Once finished it comes out, sees "sub_report" again and parses the same sub_report it just came from again, and not going on to the next one!

I realise I could make matters easier by labeling the sub_report like such, sub_report1, sub_report2, sub_report3... but for other reasons (XSLT hic-cups) I would rather keep it the way it is.

If anyone has used Jdom extensively and perhaps has an answer to making it skip over roots it has done before with the same name then please let me know.

Thanks in advance!

Reply With Quote
  #2  
Old January 11th, 2002, 05:31 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,801 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 14 h 25 sec
Reputation Power: 278
Maybe XML forum is more suitable?

Reply With Quote
  #3  
Old January 11th, 2002, 05:40 AM
Hoggie Hoggie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Ireland
Posts: 10 Hoggie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I suppose you are right, but its really the JDOM section that is causing me the problems, thus the reason I posted it in here.

Thanks anyway
bye

Reply With Quote
  #4  
Old January 13th, 2002, 01:30 PM
oscagne oscagne is offline
Java Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Posts: 5 oscagne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hoggie,

I presume what you are doing is using the getChild method of Element. As far as I am aware this will always get the first element of a specified name. I haven't done all that much with JDom before, but at first glance I reckon the following will probably work, but whether it's the best way to do it I'm not really sure

List subReports = report.getChildren
for (int i=0;i<subReports.size();i++){
Element subrep=(Element) subReports.get(i);
//do whatever you want with the sub-report here
}

This essentially is dumping all the children into a list which you then cycle through. If this doesn't work then have a re-post, and I'll have another look.

Anyone got any better suggestions?

I realise this isn't a specifically a servlet or JSP issue, but I'm sure a lot of you have come accross XML within servlets as it's a very clean way to do an transaction. So I think this question certainly has a place here

Reply With Quote
  #5  
Old January 14th, 2002, 01:58 AM
Hoggie Hoggie is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Ireland
Posts: 10 Hoggie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hey,

Yeah I tried that along with lists of other elements within lists...the code is quite a maze!!! Really it is just looking at the first sub_report, as you pointed out, but I dont really want to have to change my structure so as to have a different name for each sub_report (ie sub_report1,sub_report2...)

With the way JDOM is, however, I dont see any other alternative. No one seems to be alive on their Forum???

Thank you for your input.

All the best!

Reply With Quote
  #6  
Old January 14th, 2002, 03:23 PM
oscagne oscagne is offline
Java Developer
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Posts: 5 oscagne User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hoggie,

I think you mis-understood. You can have the sub_reports all in elements called <sub_report></sub_report>. Because as I'm sure you know this is one of the reasons XML is so powerful...being able to have multiple elements of the same type as syblings.

The solution I suggested works with this (theoretically), as all it does it get all the elements (in this case sub_report elements) in a "List" data structure. You then can use a loop to cycle through these and do whatever you wish with them.

I know my suggestion wasn't particularly elegant, but I don't see any reason that it wouldn't work. I suppose if you broke it down into methods it would not even look to bad. You could I suppose even make a class that inheirets from Element if it's not final (didn't check), and add some useful methods. If you want more detailed code then re-post, as I haven't got time to write any now.

Another way to do this looks like you use the removeChild (String name). Which removes the first child element of that name (i.e. the one you are stuck on), but to me this is a nasty way of doing it, as it would compromise later calculations you want to do on that document.

Hope this has been of some help

Oscagne

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > JDOM: Reading xml doc problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway