简体   繁体   English

如何在php中编写xpath来查询一个xml文件

[英]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:我有一个 xml 文件,我想从 php 页面查询。实际上,该文件包含用户的用户名和密码。我想查询该文件并查看用户名和密码是否匹配。这是我的代码php页面:

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

<?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>mygirl@hotmail.com</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警告:SimpleXMLElement::xpath(): Invalid expression in C:\\Program Files (x86)\\EasyPHP-Devserver-17\\eds-www\\Freshshop 免费网站模板 - Free-CSS.com\\freshshop\\successfullogin.php 第 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警告:SimpleXMLElement::xpath(): xmlXPathEval: 评估在 C:\\Program Files (x86)\\EasyPHP-Devserver-17\\eds-www\\Freshshop 免费网站模板 - Free-CSS.com\\freshshop\\successfullogin.php 中失败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警告:在 C:\\Program Files (x86)\\EasyPHP-Devserver-17\\eds-www\\Freshshop 免费网站模板 - Free-CSS.com\\freshshop\\successfullogin.php 第 13 行中为 foreach() 提供的参数无效

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;
}

You were quite close with your XPath expression but you have it slightly wrong.您的 XPath 表达式非常接近,但您的做法略有错误。 You are trying to match on two child elements so the XPath query should have both elements within the [ square braces ]您正在尝试匹配两个子元素,因此 XPath 查询应该在[ 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>mygirl@hotmail.com</email>
    </user>
    <user>
        <id>2</id>
        <username>mybanana</username>
        <password>myb4n4n4</password>
        <email>mybanana@hotmail.com</email>
    </user>
    <user>
        <id>3</id>
        <username>mytrumpet</username>
        <password>mytrumpet</password>
        <email>mytrumpet@hotmail.com</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 mybanana@hotmail.com

You should be able to adapt this to simple_xml very easily I would imagine...我想你应该能够很容易地将它调整到simple_xml ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM