php - Get specific node in xml? -


my xml structured follow:

<?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> 

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?

update:

for doesn't have understood, want if insert token: jfhsjfhksdjfhsjkfhksjfsdk connection of user token jfhsjfhksdjfhsjkfhksjfsdk returned. in case want content of connection node , create connection string:

<connection>             <host>                 localhost             </host>             <username>                 root             </username>             <dbname>                 test             </dbname>             <dbpass>                 123456789             </dbpass>         </connection> 

first of all, considerations xml code. don't know if real xml formatted above, if it, it's important highlight xml whitespaces behavior different html: without dtd or xml schema definition, whitespaces significant whitespaces , should preserved. dtd or xml schema definitions, the whitespaces in content significant.

this means xml:

<token>     jfhsjfhksdjfhsjkfhksjfsdk </token> 

the <token> assume value '\n jfhsjfhksdjfhsjkfhksjfsdk\n' (\n = line break) instead of 'jfhsjfhksdjfhsjkfhksjfsdk'.

this makes difficult find token using xpath without pre-modify xml (you can use regex it). can use contains() xpath syntax this, (theoretically) can return more 1 result.

i don't know if have tested code above, in $value->__tostring() return '\n jfhsjfhksdjfhsjkfhksjfsdk\n' (it can change depending of indentation level). return token value, have change code in:

return trim( $value->__tostring() ); 

my advice change anyway xml in way:

<user>     <name>foo</name>     <token>jfhsjfhksdjfhsjkfhksjfsdk</token>     <connection>         <host>localhost</host>         <username>root</username>         <dbname>test</dbname>         <dbpass>123456789</dbpass>     </connection> </user> 

because correct way store data: if xml produced you, change should not difficult.

anyway, propose 2 different functions - 1 xpath, 1 without.

function 1 (xpath) works modified xml:

function getconstring( $token ) {     $data = file_get_contents($_server['document_root'] . "con");     $xml = new simplexmlelement( $data );      $path = '//users/user/token[text()="'.$token.'"]';     $found = $xml->xpath( $path );     if( ! count( $found ) ) return false;      $node = $found[0]->xpath('parent::*')[0];     return (array) $node->connection; } 

function 2, works old , new xml:

function getconstring( $token ) {     $data = file_get_contents($_server['document_root'] . "con");     $xml = new simplexmlelement( $data );      foreach( $xml->user $user )     {         if( trim($user->token->__tostring()) == $token )         {             $retval = array();             foreach( $user->connection[0] $key => $val )             { $retval[$key] = trim($val); }             return $retval;         }     }      return false; } 

edit:

in comments, ask: “is possible take connection node such host, dbname ect... separate , save in specific variable”?

i not understand problems can create syntax:

$user = getconstring( 'jfhsjfhksdjfhsjkfhksjfsdk' ); mysqli_connect ( $user['host'], $user['username'], $user['dbpass'], $user['dbname'] ); 

btw, can put returned value in separate variables in way:

$user = getconstring( 'jfhsjfhksdjfhsjkfhksjfsdk' ); foreach( $user $key => $val ) $$key = $val; mysqli_connect ( $host, $username, $dbpass, $dbname ); 

or - if want specific variable names - change 2nd function above in way:

(...) $retval = array(); foreach( $user->connection[0] $key => $val ) { $retval[] = trim($val); } (...) 

and call in way:

list( $dbhost, $dbuser, $dbdb, $dbpass ) = getconstring( 'jfhsjfhksdjfhsjkfhksjfsdk' ); mysqli_connect ( $dbhost, $dbuser, $dbpass, $dbdb ); 

you can change variable names inside list preferred names. function modification necessary because list doesn't work associative arrays.



Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -