neem88
neem88

Reputation: 53

how to write xpath to query an xml file in php

i have an xml file which i want to query from a php page.Actually the file contains username and password of users.I want to query the file and see if there is a match for a username and password.Here are my codes for the php page:

<?php
$username=$_POST['username'];
$password=$_POST['password'];


$xml = simplexml_load_file('users.xml') or 
    die("error :cannot create object");
    print_r($xml);


$result = $xml->xpath("/user[username=".$username."] and //user[password=".$password."]");

foreach ($result as $node){
    echo $node->id;
    echo $node->username;
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


<user>
<id>1</id>
<username>mygirl</username>
<password>mygirl1234</password>
<email>[email protected]</email>
</user>

Exceptions

Warning: SimpleXMLElement::xpath(): Invalid expression in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\Freshshop Free Website Template - Free-CSS.com\freshshop\successfullogin.php on line 11

Warning: SimpleXMLElement::xpath(): xmlXPathEval: evaluation failed in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\Freshshop Free Website Template - Free-CSS.com\freshshop\successfullogin.php on line 11

Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\Freshshop Free Website Template - Free-CSS.com\freshshop\successfullogin.php on line 13

UPDATE
$xml = simplexml_load_file('users.xml') or 
die("error :cannot create object");

$username='neem88';
$pwd='dbhcasvc';
$query=sprintf('/users/user[ username="%s" and password="%s" ]', $username, 
$pwd );


libxml_use_internal_errors( true );
$dom=new DOMDocument();
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadXML( $xml );
$errors = libxml_get_errors();
libxml_clear_errors();

$xp=new DOMXPath( $dom );
$col=$xp->query( $query );

if( $col->length > 0 ){
foreach( $col as $node )echo $node->id;
}

Upvotes: 0

Views: 195

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

You were quite close with your XPath expression but you have it slightly wrong. You are trying to match on two child elements so the XPath query should have both elements within the [ square braces ]

The query would be of the form:

//users/user[ username="X" and password="Y" ]

putting it all together

$strxml='<?xml version="1.0" encoding="utf-8"?>
<users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <user>
        <id>1</id>
        <username>mygirl</username>
        <password>mygirl1234</password>
        <email>[email protected]</email>
    </user>
    <user>
        <id>2</id>
        <username>mybanana</username>
        <password>myb4n4n4</password>
        <email>[email protected]</email>
    </user>
    <user>
        <id>3</id>
        <username>mytrumpet</username>
        <password>mytrumpet</password>
        <email>[email protected]</email>
    </user>
</users>';


$username='mybanana';
$pwd='myb4n4n4';

/* put both elements and values within square braces - combine with AND */
$query=sprintf('//users/user[ username="%s" and password="%s" ]', $username, $pwd );


libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->recover=true;
$dom->strictErrorChecking=false;
$dom->loadXML( $strxml );
$errors = libxml_get_errors();
libxml_clear_errors();

$xp=new DOMXPath( $dom );
$col=$xp->query( $query );

if( $col->length > 0 ){
    foreach( $col as $node )echo $node->nodeValue;
}

This will output

2 mybanana myb4n4n4 [email protected]

You should be able to adapt this to simple_xml very easily I would imagine...

Upvotes: 0

Related Questions