PHP - Iterate Though Additonial XML Node -


i working following xml response structure:

<compressedvehicles> <f> <rs>  <r>     <vs>         <v />         <v />     </vs> </r> <r>     <vs>         <v />         <v />     </vs> </r> </rs> </f> </compressedvehicles> 

so far, guidance fellow stack overflow member, able construct working json output based on following php code:

header('content-type: application/json');  $xml  = simplexml_load_file( 'inventory.xml' ); $compressedvehicles = $xml->compressedvehicles;  $attributes = array(); foreach( $compressedvehicles->f->attributes() $key => $val ) {     $attributes[$key] = $val->__tostring(); }  $data = array(); foreach( $compressedvehicles->f->rs->r->vs->v $vehicle ) {     $line = array();     foreach( $vehicle->attributes() $key => $val ) {     $line[$attributes[$key]] = $val->__tostring(); } $data[] = $line; }  $json = json_encode($data); echo $json; 

this iterates through single <r> node before completion. how can append code iterate through each <r> node well?

thank in advance.

right now, you're directly going $compressedvehicles->f->rs->r->vs->v, modify loop each <r> node:

foreach( $compressedvehicles->f->rs->r $r ) { 

this iterate each <r>.

then each <r>, add nest $vehicle:

foreach($r->vs->v $vehicle)  { // rest of code 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -