Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Read XML into Java Array

 
tony deprato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20776
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry --here is the xml doc






 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
tony deprato
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 408
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Paul Clapham
Sheriff
Pie
Posts: 20776
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic