开发者

array iterating

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 ?


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


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.


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() .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜