java - Looping through XML to build an object -


so im trying loop through xml file shown below , save contents objects each "screen".

my xml follows:

<?xml version="1.0" encoding="utf-8"?> <screens>     <screen id="house" backdropimage = "r.drawable.scene">         <region id="one" rleft = "200" rright = "200" rtop = "10" rbottom = "10"/>         <region id="two" rleft = "220" rright = "220" rtop = "12" rbottom = "12"/>         <paths left = "park" right = "x" top = "x" bottom = "x"/>     </screen>     <screen id="park" backdropimage = "r.drawable.park">         <region id="1" rleft = "500" rright = "200" rtop = "10" rbottom = "10"/>         <paths right = "house" left = "x" top = "x" bottom = "x"/>     </screen> </screens> 

so idea create object in array of screen information in each screen block. id , backdrop fine im having problems iterating through 'region' parts there different amounts of region entries in different sections. code im using ut reason goes through 'one' 'two' , '1' stop @ /screen.

    depth = parser.getdepth();             while(parser.getdepth() == depth) {                   if(event==xmlpullparser.start_tag && parser.getname().contentequals("region")) {                     region reg = new region(parser.getattributeintvalue(null, "rleft", 0),                             parser.getattributeintvalue(null, "rtop", 0),                             parser.getattributeintvalue(null, "rright", 0),                             parser.getattributeintvalue(null, "rbottom", 0));                     //string str = parser.getidattribute();                     newscreen.area = (reg);                     //rect temp = reg.getbounds();                     newscreen.areaarray.add(reg);             parser.next();             }    

this first time i've tried using xml files in java/android , i'm still pretty new java on whole i'm sure i'm not using best method here , appreciate help. thanks

i use simple-xml serialize xml object. have pretty documentation, can library here: http://simple.sourceforge.net/

here's example:

youractivity.java

import org.simpleframework.xml.serializer; import org.simpleframework.xml.core.persister;  .... private screens myscreens;  ....   private void serializemyxml(){         file source = new file("directory/yourxmlfile.xml");         serializer serializer = new persister();         try {             myscreens= serializer.read(screens.class, source);          } catch (exception e) {             e.printstacktrace();         } } 

screens.java

import java.io.serializable; import java.util.list;  import org.simpleframework.xml.elementlist; import org.simpleframework.xml.root;  @root public class screensimplements serializable{      /**      *       */     private static final long serialversionuid = 1111;//some random unique serialversion id      @elementlist(inline=true)     private list<screen> list;      public list<screen> getlist() {         return list;     }  } 

screen.java

    import java.io.serializable;      import org.simpleframework.xml.attribute;      public class entity implements serializable{          /**          *           */         private static final long serialversionuid = 5004451486040850151l;          @attribute         private string backdropimage;          @attribute         private int id;          @elementlist         private list<region> region;  @element private paths paths;          public int getid() {             return id;         }          public string getbackdropimage() {             return backdropimage;         }      } 

and on.. , on.. read more in documewntatio.

good luck


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -