Php SimpleXML循环检查子元素
我对XML不是很在行,但我对PHP很在行。我需要使用XML文件作为某种“数据库”。它包含各种用户信息。包括登录信息。XML的设置如下所示:Php SimpleXML循环检查子元素,php,xml,simplexml,Php,Xml,Simplexml,我对XML不是很在行,但我对PHP很在行。我需要使用XML文件作为某种“数据库”。它包含各种用户信息。包括登录信息。XML的设置如下所示: <employee ssnum=""> <first_name></first_name> <last_name></last_name> <contact_info> <office_phone></office_phone> <email
<employee ssnum="">
<first_name></first_name>
<last_name></last_name>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username></username>
<password></password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>
我需要一些方法来检查用户名和密码是否匹配,并登录相应的用户。我不确定如何检查用户名/密码对,以及如何返回正确的用户进行登录。有另一种方法可以做到这一点,我通过DOMdocument和mysql查询看到了使用xml文件作为数据库的方法,但我不确定这是最简单的方法,或者这是如何工作的
这不是针对任何严肃的工作,而是一个学习项目
任何帮助都将不胜感激,谢谢。好像有人告诉你这是胡说八道。或者——更有可能——你误解了什么。您将不使用mysql查询,而是使用xpath查询访问xml中的节点。您确实应该使用
DOMDocument
和访问节点
下面是一个如何读取或修改密码的示例:
$doc = new DOMDocument();
$doc->load('employee.xml');
$selector = new DOMXpath($doc);
$result = $selector->query('/employee/access_info/password');
// read password:
$password = $result->item(0)->nodeValue;
// set password:
$result->item(0)->nodeValue = 'secret';
// save xml
$doc->save('employee.xml');
如果您需要有关XPath的更多信息,可以按照下面的说明进行操作。我的第一个想法是使用xml_parse,或者,如所建议的,也可以使用SimpleXML,我希望能帮助您理清一些问题,并扩展hek2mgl的正确建议。我意识到这是一个学习过程,但如果操作正确,您根本不需要循环(当然,假设您只需要访问当前试图登录的一名员工)。 在hek2mgl链接到的教程中,它向您展示了谓词的全部含义,您应该使用谓词通过用户输入的用户名在xpath查询中立即自动选择正确的员工,然后将密码与用户输入的密码进行比较,看看它们是否匹配。这样就不需要知道$result长度的任何信息(当然,它的有效长度为1除外)。假设包含所有employee元素的xml文件有一个名为“employees”的根容器元素,它可能会:
<?php
//below is php heredoc string.
//just a simulation of an employees.xml file which
//obviously contains multiple employee elements
//within a root 'employees' element
$xmlStr = <<<XMLBookendMarker
<employees>
<employee ssnum="555662222">
<first_name></first_name>
<last_name></last_name>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username>jackass</username>
<password>letmein</password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>
<employee ssnum="555991111">
<first_name></first_name>
<last_name></last_name>
<contact_info>
<office_phone></office_phone>
<email></email>
<cell_phone></cell_phone>
</contact_info>
<access_info level="user">
<username>god</username>
<password>qwerty</password>
</access_info>
<department></department>
<date_started></date_started>
<position></position>
</employee>
</employees>
XMLBookendMarker;
//below is the information entered by the user,
//which we will use to find the username in the
//employees.xml and then check if the password matches
$enteredUsername = 'god';
$enteredUserPass = 'qwerty';
$doc = new DOMDocument();
$doc->loadXML($xmlStr);
$selector = new DOMXpath($doc);
//the quote styles are important below
$result = $selector->query("/employees/employee/access_info[username='$enteredUsername']/password");
//if length should happen to be longer than 1, well you screwed up long
//time ago by allowing multiple same usernames, and that can't be allowed!
//if length is 0, then there is no such username
if ($result->length === 0) {
die('NO SUCH USERNAME EXISTS');
} elseif ($result->length > 1) {
die('ERROR: CONSULT ADMIN');
}
//ok, now we know $result->length is 1
$password = $result->item(0)->nodeValue;
if ($password == $enteredUserPass) {
echo 'The password matches! logging in....';
//now we might want some other info about the employee
$employee = $result->item(0)->parentNode->parentNode;
//just demoing showing the employee
echo '<br><br>'.htmlentities($doc->saveXML($employee));
}
?>
不确定我是否做错了什么,但是当我回显$password时,它回显的是用户名,而不是密码。您没有做错任何事情,只需将/employee/access\u info/username
更改为/employee/access\u info/password
^^我开始明白了。但它只获取第一个密码(假设是因为第(0)项),因此我必须循环遍历每个用户,比较用户名/密码以查看两者是否匹配。我想一个for循环就足够了,但是我怎么知道有多少员工(对于我链接的教程下面的$I,它会解释你需要的任何东西。(大约30分钟内)我已经完成了这一部分。这应该足够我做我需要的事情了。非常感谢。顺便说一句,我尽可能多地使用php手册和谷歌。但是我一直在使用与XML相关的东西时遇到问题,真的需要一些个人帮助。再次感谢。我不太确定我被感谢了什么…但不客气?哈哈(以友好的方式)给我一些关于期望和礼仪的建议。