使用PHP从XML中使用相同父级的另一个子级的值获取子级的值

时间:2018-03-07 21:27:20

标签: php xml

我有以下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>

我正在尝试使用PHP创建一个程序,它返回某个“badgeid”的“稀缺性”(这始终是唯一的)。

所以我的问题是:当我从用户收到的唯一输入是唯一的<scarcity></scarcity>时,如何获得badgeid的值?一般来说:如何从同一父母的另一个孩子的价值中获得孩子的价值?

2 个答案:

答案 0 :(得分:3)

您还可以使用xpath表达式并使用$badgeid作为参数:

$expression = "/response/message[./badgeid='$badgeid']";

然后,您可以使用SimpleXMLElementDOMDocument

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

Demo

答案 1 :(得分:0)

您可以将simplexml_load_string()用于XML字符串,如果它是XML文件,则使用simplexml_load_file()。然后,您可以迭代$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
}