php - How to get the specific inner node with SimpleXml? -


my xml structured follow:

<?xml version="1.0" ?> <user>     <name>         foo     </name>     <token>         jfhsjfhksdjfhsjkfhksjfsdk     </token>     <connection>         <host>             localhost         </host>         <username>             root         </username>         <dbname>             test         </dbname>         <dbpass>             123456789         </dbpass>     </connection> </user> <user>     ... same structure... </user> 

i made code iterate through xml node:

function getconstring($node) {    $item = file_get_contents($_server['document_root'] . "con");    $nodes = new simplexmlelement($item);    $result = $nodes[0];     foreach($result $item => $value)    {       if($item == "token")       {          return $value->__tostring();      }    } } 

what i'm trying achieve when $node equal to:

jfhsjfhksdjfhsjkfhksjfsdk 

the connection node returned array, how can achieve this?

if xml you're trying parse you've posted here, it's invalid since

xml documents must contain 1 root element parent of other elements:

http://www.w3schools.com/xml/xml_syntax.asp

(and yours doesn't, , parsing such string fails exception: string not parsed xml in ...).

so xml should be:

<?xml version="1.0" ?> <users>     <user>         <name>             foo         </name>         <token>             jfhsjfhksdjfhsjkfhksjfsdk         </token>         <connection>             <host>                 localhost             </host>             <username>                 root             </username>             <dbname>                 test             </dbname>             <dbpass>                 123456789             </dbpass>         </connection>     </user>     <user>         ... same structure...     </user> </users> 

and don't need iterate through collection

// $nodes simplexmlelement $user = $nodes->user[0]; if($user->token)    return $user->token->__tostring(); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -