#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Ireland
    Posts
    10
    Rep 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!
  2. #2
  3. Modding: Oracle MsSQL Firebird
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2001
    Location
    Outside US
    Posts
    8,527
    Rep Power
    539
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Ireland
    Posts
    10
    Rep 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
  6. #4
  7. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep 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
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2001
    Location
    Ireland
    Posts
    10
    Rep 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!
  10. #6
  11. No Profile Picture
    Java Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2001
    Posts
    5
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo