[英]Get value of child from the value of another child with the same parent in XML using PHP
I have the following XML document: 我有以下XML文档:
<response>
<message>
<badgeid>13</badgeid>
<level>4672</level>
<completion_time>1518626787</completion_time>
<xp>4922</xp>
<scarcity>9717</scarcity>
</message>
<message>
<badgeid>25</badgeid>
<level>1</level>
<completion_time>1480166791</completion_time>
<xp>25</xp>
<scarcity>3761041</scarcity>
</message>
<message>
<badgeid>21</badgeid>
<level>1</level>
<completion_time>1467581153</completion_time>
<xp>100</xp>
<scarcity>16650345</scarcity>
</message>
</response>
I am trying to create a program using PHP that returns the "scarcity" of a certain "badgeid" (that is always unique). 我正在尝试使用PHP创建一个程序,该程序返回某个“徽章”(总是唯一的)的“稀缺性”。
So my question is: How do I get the value of <scarcity></scarcity>
when the only input I receive from the user is a unique badgeid
? 所以我的问题是:当我从用户收到的唯一输入是唯一的
badgeid
时,如何获得<scarcity></scarcity>
的值? In general words: How do I get the value of a child from the value of another child with the same parent? 概括地说:如何从同一个父母的另一个孩子的价值中获得一个孩子的价值?
You could also use an xpath expression and use the $badgeid
as a parameter: 您还可以使用xpath表达式并将
$badgeid
用作参数:
$expression = "/response/message[./badgeid='$badgeid']";
Then you could for example use SimpleXMLElement or DOMDocument . 然后,您可以使用SimpleXMLElement或DOMDocument 。
$data = <<<DATA
<response>
<message>
<badgeid>13</badgeid>
<level>4672</level>
<completion_time>1518626787</completion_time>
<xp>4922</xp>
<scarcity>9717</scarcity>
</message>
<message>
<badgeid>25</badgeid>
<level>1</level>
<completion_time>1480166791</completion_time>
<xp>25</xp>
<scarcity>3761041</scarcity>
</message>
<message>
<badgeid>21</badgeid>
<level>1</level>
<completion_time>1467581153</completion_time>
<xp>100</xp>
<scarcity>16650345</scarcity>
</message>
</response>
DATA;
$badgeid = 25;
$xml = simplexml_load_string($data);
$expression = "/response/message[./badgeid='$badgeid']";
$items = $xml->xpath($expression);
if (count($items) === 1) {
echo $items[0]->scarcity;
}
$doc = new DOMDocument();
$doc->loadXML($data);
$xpath = new DOMXpath($doc);
$res = $xpath->query($expression);
if ($res->length === 1) {
echo $res->item(0)->getElementsByTagName("scarcity")->item(0)->nodeValue;
}
You could use simplexml_load_string()
for an XML string, or simplexml_load_file()
if it is a XML file. 你可以使用
simplexml_load_string()
为一个XML字符串,或simplexml_load_file()
如果它是一个XML文件。 Then you could iterate over $xml->message
and check if $message->badgeid
is the item you want : 然后,您可以遍历
$xml->message
并检查$message->badgeid
是否是您想要的项目:
$xmlstr = '<response>
<message>
<badgeid>13</badgeid>
<level>4672</level>
<completion_time>1518626787</completion_time>
<xp>4922</xp>
<scarcity>9717</scarcity>
</message>
<message>
<badgeid>25</badgeid>
<level>1</level>
<completion_time>1480166791</completion_time>
<xp>25</xp>
<scarcity>3761041</scarcity>
</message>
<message>
<badgeid>21</badgeid>
<level>1</level>
<completion_time>1467581153</completion_time>
<xp>100</xp>
<scarcity>16650345</scarcity>
</message>
</response>';
$badgeid = 21 ; // The value you want to search
$xml = simplexml_load_string($xmlstr);
$scarcity = null ;
foreach ($xml->message as $message) {
if ((string)$message->badgeid == $badgeid) {
$scarcity = (string)$message->scarcity ;
break ;
}
}
// use $scarcity if set.
if (isset($scarcity)) {
// do stuff
echo $scarcity ; // 16650345
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.