gpt4 book ai didi

php - 从xml文件中全选,其中person ='Bob'

转载 作者:行者123 更新时间:2023-12-03 16:17:42 24 4
gpt4 key购买 nike

因此,当我将php与MySQL结合使用时,我只是这样做了:

$result = $mysqli->query("SELECT * FROM table WHERE person='Bob'");


我现在将PHP与XML结合使用,但不知道如何执行此操作。我尝试了谷歌搜索,但不知道在XML中将其称为什么。

我的XML是:

<People>
<person>Bob</person>
<about>Bob is a blob. He is 20 Years old, etc</about>
</People>
<People>
<person>Jim</person>
<about>Jim is 90 Years old, he plays basketball, etc</about>
</People>
<People>
<person>Steve</person>
<about>Steve is a superhero with the ability to fly.</about>
</People>


我基本上是想回应某个人的信息。因此,如果用户搜索Jim,它将回显:


Name: Jim
About: Jim is 90 Years old, he plays basketball, etc

最佳答案

使用XPath

XML:

<data>
<People>
<person>Bob</person>
<about>Bob is a blob. He is 20 Years old, etc</about>
</People>
<People>
<person>Jim</person>
<about>Jim is 90 Years old, he plays basketball, etc</about>
</People>
<People>
<person>Steve</person>
<about>Steve is a superhero with the ability to fly.</about>
</People>
</data>


PHP代码:

$xml = new SimpleXMLElement($xml_string);
$result = $xml->xpath('/data/People[person="Jim"]');
var_dump($result);


输出:

array(1) {
[0]=>
object(SimpleXMLElement)#2 (2) {
["person"]=>
string(3) "Jim"
["about"]=>
string(45) "Jim is 90 Years old, he plays basketball, etc"
}
}

关于php - 从xml文件中全选,其中person ='Bob',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26767607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com