php - 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>
解决方案
像这样的东西应该做你想做的事。它遍历 等值的列表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
)
)
推荐阅读
- mongodb - MongoDB 查询不返回排序
- jquery - 幻灯片内的光滑轮播 appendDots 失去活动课程
- c# - 从应用程序调用时,WorkItem Tracking API 跳过了几个工作项
- bash - 当 ssh 进入远程时 git pull 不提示输入用户名/密码
- excel - Excel / VBA / Rows.Count 返回错误 1004
- android - 单击抽屉时未显示片段
- java - Spring Boot 微服务正在关闭:DiscoveryClient 已完成关闭
- sql - 在最大时间段的窗口上聚合(SUM 或 AVG)
- python - python时间序列合成数据使用ydata-synthetic包 - 时间序列GAN
- embedded - 为什么 FreeRTOS 需要在断点处停止一次才能运行良好?