Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes XML and Related Technologies and the fly likes Read XML into Java Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Read XML into Java Array" Watch "Read XML into Java Array" New topic
Author

Read XML into Java Array

tony deprato
Greenhorn

Joined: Mar 17, 2007
Posts: 3
Hi I am trying to use this code to put my xml info into an array and I cant seem to get it to work right. Sorry I am new to XML and Java.
------------------------------------------

--------------------------------------------------------
[ April 13, 2007: Message edited by: Paul Clapham ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Tony, I reformatted your code by putting the code tags around it and fixing up the indentation to make it consistent. Easier on the eyes that way. Now, you say it doesn't work right. Can you tell us what it is supposed to do? All we see there is the code, so all we can tell is what it actually does. And it would help if you posted the input XML document too.
tony deprato
Greenhorn

Joined: Mar 17, 2007
Posts: 3
Sorry --here is the xml doc






Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
I cant seem to get it to work right.


As Paul said, that is not a very useful problem description. What exactly should the code do, and how does its behavior deviate from that?


Ping & DNS - my free Android networking tools app
tony deprato
Greenhorn

Joined: Mar 17, 2007
Posts: 3
Sorry, I thought I stated it above. This java code should read xml elements into an array. I can only get the first element in the first position of the array. I ran my xml through a validator and it seems fine. I have used xml with php before but not Java.

CN
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 376
Tony. I have experience with XML processing in JavaScript ... so this might just be a question rather than a solution. Shouldn't there be a loop somewhere that is used to transfer xml data one element at a time into an array? I wondered what the lists are getting ... note that getAttributes and getChildren are plural ... but then you say you're only getting the first one.

Should there be some kind of x.item(i) i=0,1,2... kind of thing? x being the result of a get.
[ April 14, 2007: Message edited by: Roger F. Gay ]

Correlation does not prove causality.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

I don't think your code is doing anything much. And I'm not even sure it will run correctly if you run it. For example this:gets you a list of seven "uni" elements. (At least I think it does, I'm not really familiar with JDOM but that seems like the most reasonable thing for it to do.) But then you do this:And that, I think, should throw a ClassCastException. I wouldn't expect to be able to cast a List to an Element.

So, you might want to have a look at some JDOM tutorials. (The easiest way to find them is to stick "jdom tutorial" into your favourite Internet search engine and look at what comes back, but beware of old tutorials because JDOM went through a number of changes before version 1.0 was finalized.) Here is one that isn't too old, for example.

If you just want to put those seven "uni" elements into an array, then you have them in a List already so it's easy to get an array from that List. But I suspect your requirements will go beyond that once you get used to working with XML. So take some code from the tutorials and fiddle around with it.
 
wood burning stoves
 
subject: Read XML into Java Array