首页 > 解决方案 > PHP - 使用可能的多个项目解析 xml,而不使用简单的 xml

问题描述

我一直在使用以下内容解析 xml 文件,但现在遇到了可能有多个项目而不是只有一个可能值的情况。我需要能够将它们保存到一个数组中,这样我就可以在它们上运行一个 foreach 循环。我不想在这种情况下使用 simplexml,所以请不要推荐 - 我想保持方法与我做类似的其他地方相同 - 这种情况恰好可能发送多个项目。

这是我过去一直使用的,当只有一个名称/值时效果很好:

//receive the xml and trim
$xml_post = trim(file_get_contents('php://input'));

//save posted xml to file to ensure correct post values
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/../something/something.txt', print_r($xml_post, true));

//open domdocument
$xml = new DOMDocument();

//load xml
$xml->loadXML($xml_post);

//parse the XML into a usable array
$xmlval = array();

//these are all fine because there can only be one value sent
$xmlval['orderid'] = $xml->getElementsByTagName("orderid")->item(0)->nodeValue;
$xmlval['test'] = $xml->getElementsByTagName("test")->item(0)->nodeValue;
$xmlval['referrer'] = $xml->getElementsByTagName("referrer")->item(0)->nodeValue;

//******these can be repeated so I need to figure out how to save these as an array in something like $xmlval['items'] so I can run a foreach loop - foreach($xmlval['items'] as $item) and access like $item['productname'] and so on for each group

$xmlval['productname'] = $xml->getElementsByTagName("productname")->item(0)->nodeValue;
$xmlval['quantity'] = $xml->getElementsByTagName("quantity")->item(0)->nodeValue;
$xmlval['returnprice'] = $xml->getElementsByTagName("returnprice")->item(0)->nodeValue;
$xmlval['originalprice'] = $xml->getElementsByTagName("originalprice")->item(0)->nodeValue;

这是一个存储在 something.txt 中的内容的示例,当它进入时我会保存它:

<return > 
    <orderid>ggfegse53534353</orderid> 
    <test>true</test> 
    <referrer>gfdgsdggfgrer</referrer> 
    <items> 
        <item> 
            <productname>something</productname> 
            <quantity>1</quantity> 
            <returnprice>$19.95</returnprice> 
            <originalprice>$19.95</originalprice> 
        </item>
        <item> 
            <productname>something2</productname> 
            <quantity>5</quantity> 
            <returnprice>$19.95</returnprice> 
            <originalprice>$19.95</originalprice> 
        </item>
        <item> 
            <productname>something3</productname> 
            <quantity>8</quantity> 
            <returnprice>$19.95</returnprice> 
            <originalprice>$19.95</originalprice> 
        </item>
    </items> 
</return>

标签: phpxml-parsing

解决方案


像这样的东西应该做你想做的事。它遍历 等值的列表productname,依次quantity将它们添加到items数组中:

$xmlval['items'] = array();
$productname = $xml->getElementsByTagName("productname");
$quantity = $xml->getElementsByTagName("quantity");
$returnprice = $xml->getElementsByTagName("returnprice");
$originalprice = $xml->getElementsByTagName("originalprice");
for ($i = 0; $i < $productname->length; $i++) {
    $xmlval['items'][$i] = array('productname' => $productname->item($i)->nodeValue,
                                 'quantity' => $quantity->item($i)->nodeValue,
                                 'returnprice' => $returnprice->item($i)->nodeValue,
                                 'originalprice' => $originalprice->item($i)->nodeValue);
}
print_r($xmlval['items']);

输出:

Array (
 [0] => Array (
    [productname] => something
    [quantity] => 1
    [returnprice] => $19.95
    [originalprice] => $19.95
  )
  [1] => Array (
    [productname] => something2
    [quantity] => 5
    [returnprice] => $19.95
    [originalprice] => $19.95
  )
  [2] => Array (
    [productname] => something3
    [quantity] => 8
    [returnprice] => $19.95
    [originalprice] => $19.95
  )
)

3v4l.org 上的演示


推荐阅读