java - Multiple repeated child elements xml -


looking bit of here.

i parse xml file using simplexml i´m not 100% sure how procede.

this snippet of xml code:

<bible abbrev="reinav" name="reina valera actualizada">   <book num="gen">     <chapter num="1">       <verse num="1">en el principio creó dios los cielos y la tierra       </verse>       <verse num="2">       y la tierra estaba sin orden y vacía. había tinieblas sobre la faz del       océano, y el espíritu de dios se movía sobre la faz de las aguas.       </verse>       <verse num="3">entonces dijo dios: "sea la luz", y fue la luz.</verse>       <verse num="4">        dios vio que la luz era buena, y separó dios la luz de las tinieblas.       </verse>     </chapter>   </book> </bible> 

this had far:

@root public class bible {  @attribute string abbrev; @attribute string name; @element book book;  public string getabbrev() {     return abbrev; }  public string getname() {     return name; }  public book getbook() {     return book; }  @root(strict = false) private class book {      @attribute(required = false)     string num;      @element(required = false)     chapter chapter;      private class chapter {     }   } } 

how manage repeating verse elements? =)

use list @elementlist-annotation. since @elementlist create element wrapping list's content, use inline = true prevent this.

bible

@root(name = "bible") public class bible {     @attribute     private string name;     @attribute     private string abbrev;     @element(required = false)     private book book;      // ... } 

book

@root(name = "book") public class book {     @attribute     private string num;     @element(required = false)     private chapter chapter;      // ... } 

chapter

@root(name = "chapter") public class chapter {     @attribute     private string num;     @elementlist(name = "verse", required = false, inline = true) /* <-- ! */     private list<verse> verse;      // ... } 

verse

@root(name = "verse") public class verse {     @attribute     private string num;     @text     private string text;      // ... } 

btw., required = false optional, have set them enable eg. empty book - no child elements. if wont make sense, remove them.

with @elementlist can have chapter multiple verse - can same eg. books; book may have multiple chapters etc..


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -