tags:

views:

113

answers:

3

I have the following xml format ,which i am parsing and getting the data and storing in an bean called xyz which has testurl as anotherbean xyz has testurl bean an array ,test url bean has id and image

<xyz>
  <testUrl Id="SmallLogo">smallLogo.jpg</testUrl > 
  <testUrl Id="MediumLogo">mediumLogo.jpg</testUrl > 
  <testUrl Id="LargeLogo">largeLogo.jpg</testUrl > 
  <testUrl Id="ExtraLarge">test.png</testUrl > 
  </xyz>
<xyz>
  <testUrl Id="SmallLogo">smallLogo.jpg</testUrl > 
  <testUrl Id="MediumLogo">mediumLogo.jpg</testUrl > 
  <testUrl Id="LargeLogo">largeLogo.jpg</testUrl > 
  <testUrl Id="ExtraLarge">test.png</testUrl > 
  </xyz>

i am accessing the data has xyz.gettesturl()[i].getid(),not able to iterate properly and get all the data,how would i iterate through the array ?

A: 

This:

xyz.gettesturl()[i].getid()

Will get the specific testUrl that is indexed by i. If you want to iterate you should try by using:

xyz.gettesturl()

To iterate over.

Oded
i am doingthe same but its not working properly
sarah
Show us the code, and specify exactly what you are trying to achieve. I can't guess without more detail.
Oded
A: 

the xml you provided is not valid. you dont have a ROOT element in your xml. if you had

<root>
  <xyz> 
   <testUrl Id="SmallLogo">smallLogo.jpg</testUrl >  
   <testUrl Id="MediumLogo">mediumLogo.jpg</testUrl >  
   <testUrl Id="LargeLogo">largeLogo.jpg</testUrl >  
   <testUrl Id="ExtraLarge">test.png</testUrl >  
  </xyz> 
  <xyz> 
   <testUrl Id="SmallLogo">smallLogo.jpg</testUrl >  
   <testUrl Id="MediumLogo">mediumLogo.jpg</testUrl >  
   <testUrl Id="LargeLogo">largeLogo.jpg</testUrl >  
   <testUrl Id="ExtraLarge">test.png</testUrl >  
  </xyz> 
</root>

then you can access all ID's though you might need another bean for root element then you can use something like: root.getxyz()[i].gettesturl()[j].getid() .

Numenor
This XML is also not valid.
BalusC
really it takes 5 secs to correct it and its clearly a typo. why the vote down?
Numenor
+1  A: 

Here is a simplified sample of classes and their accessor methods from what I understood the problem to be -

Class XYZ -

public class XYZ {

   private TestUrl[] testUrlArray;

   public XYZ(){

      testUrlArray = new TestUrl[2];
      testUrlArray[0] = new TestUrl("ID_1");
      testUrlArray[1] = new TestUrl("ID_2");
   }

   public TestUrl getTestUrl(int i){

      return testUrlArray[i];
   }
}

The class TestUrl -

public class TestUrl {

   private String id;

   public TestUrl(String id){

      this.id = id;
   }

   public String getId(){

      return id;
   }
}

This is how you would get the Id for a given TestUrl bean -

  XYZ testXYZ = new XYZ();
  System.out.println("testXYZ 0 - " + testXYZ.getTestUrl( 0 ).getId());
  System.out.println("testXYZ 1 - " + testXYZ.getTestUrl( 1 ).getId());

The output on the console would be -

 testXYZ 0 - ID_1
 testXYZ 1 - ID_2
Ranu Gupta
This is what i am doing.But instead of using testXYZ.getTestUrl( 0 ).getId()) i wanted a loop for iteration
sarah