java - XML parsing. How can I get child's child? -


i'm trying parse xml in java:

<entities> <entity name="product_section" id="1">     <product_type>3</product_type>     <section_type>1</section_type>     <name>empresa</name>     <description>d</description>     <position>1</position>     <align>left</align>      <files section_id="1">         <ico id="ico_1" type="normal" src="sections/1/icons/ico.png"></ico>         <ico id="ico_2" type="hover"  src="sections/1/icons/ico.png"></ico>         <ico id="ico_3" type="active" src="sections/1/icons/ico.png"></ico>          <img id="img_1" type="normal" src="sections/1/img/pestanya.png"></img>         <img id="img_2" type="hover"  src="sections/1/img/pestanya-hover.png"></img>         <img id="img_3" type="active" src="sections/1/img/pestanya-active.png"></img>          <background id="background_1" type="background" position="1" src="sections/1/background/bg1.png"></background>         <background id="background_2" type="background" position="2" src="sections/1/background/bg2.png"></background>         <background id="background_3" type="background" position="3" src="sections/1/background/bg3.png"></background>     </files> </entity> 

but achieved loop through entities, getting entity , each <product_type>, <section_type>, etc. want loop through files too.

this implementation far:

try {         file contingut = new file("xmlfile.xml");         documentbuilderfactory dbfactory = documentbuilderfactory.newinstance();         documentbuilder dbuilder = dbfactory.newdocumentbuilder();         document doc = dbuilder.parse(contingut);         doc.getdocumentelement().normalize();         system.out.println("root of xml file " + doc.getdocumentelement().getnodename());         //loop cada entity         nodelist nodes = doc.getelementsbytagname("entity");         (int = 0; < nodes.getlength(); i++) {             node node = nodes.item(i);             element element = (element) node;             system.out.println("product_type: " + getvalue("product_type", element));             system.out.println("section_type: " + getvalue("section_type", element));             system.out.println("name: " + getvalue("name", element));             system.out.println("description: " + getvalue("description", element));             system.out.println("position: " + getvalue("position", element));             system.out.println("align: " + getvalue("align", element));         }     } catch (exception e){         e.printstacktrace();     } 

getvalue function is:

private static string getvalue(string tag, element element) {     nodelist nodes = element.getelementsbytagname(tag).item(0).getchildnodes();     node node = (node) nodes.item(0);     return node.getnodevalue(); } 

i've done lot of google search, , find "simple" examples, parent, , child, not child's child.

any appreciated.

at first 1 suggestion:

check element type after element element = (element) node;

use code or :

if (element.getnodetype() == element.element_node) {  // smth} 

and answer question:

you can rewrite code. after create element can it's child elements using element.getchildnodes();

it gives child tags. after write simple loop each node element node list :

nodelist nodes = element.getchildnodes(); for(int =0; < nodes.getlength(); i++){      element child = (element) nodes.item(i);      if(child.getnodetype() == element.element_node){              string tagname = child.gettagname();              if(!tagname.equals("files")){                    system.out.println(tagname + " : " + child.gettextcontent());              }else{                    nodelist fileschilds = child.getchildnodes();                    for(int j = 0; j < fileschilds.getlength(); j++){                       //and above                    }              }      } } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -