我正在尝试使用php(DOM)解析xml结果:
stdClass Object
(
[GetBilletResult] => Array
(
[0] => stdClass Object
(
[tabGrilleHoraire] => stdClass Object
(
[tabDetailTarifArticle] => stdClass Object
(
[sNomArticle] => ARTICLE1
[tabDetail] => Array
(
[0] => stdClass Object
(
[sDetail] => Liste Pax [Âge:19]
[sAgePax] => 19;
...
)
[1] => stdClass Object
(
[sDetail] => Prix de Base
[sAgePax] => 18;
...
)
到目前为止我的代码:
$processed = array();
foreach( $billets as $GetBilletResult )
{
$sNomProduit = $GetBilletResult->getElementsByTagName( "sNomProduit" )->item(0)->nodeValue;
$sNomArticle = $GetBilletResult->getElementsByTagName( "sNomArticle" )->item(0)->nodeValue;
$tabDetail = $GetBilletResult->getElementsByTagName( "tabDetail" );
if (!isset($processed[$sNomProduit])) {
$processed[$sNomProduit] = array();
}
$processed[$sNomProduit][] = array(
'sNomArticle' => $sNomArticle,
'tabDetail' => $tabDetail,
);
}
显示结果的循环(文章按产品重新分组):
foreach ($processed as $sNomProduit => $list) {
echo "<h3> ".$sNomProduit."</h3>";
foreach ($list as $item) {
echo "<h5> ".$item['sNomArticle'] . "</h5>";
foreach ($item['tabDetail'] as $node) {
var_dump ($node->nodeValue);
}
}
}
输出(“tabDetail”数组下的数组显示为字符串)
PRODUIT A
第A1条
string'Listen Pax:Pax n°1 [Âge:19] 19; ADULTE(12-59.99)00000'(长度= 54)
字符串'Prix de Base240300000'(长度= 21)
...
答案 0 :(得分:3)
在这部分代码中:
foreach ($item['tabDetail'] as $node) {
var_dump ($node->nodeValue);
}
如果tabDetail
下有更多子节点,则使用$node->nodeValue
只会得到节点及其后代的textContent
。如果您想单独打印项目,您应该能够遍历childNodes
并输出它们的值。
foreach ($item['tabDetail'] as $node) {
foreach ($node->childNodes as $child) {
echo $child->nodeValue;
}
}
或者在您之前的代码中获取您想要的特定节点的值:
foreach ($item['tabDetail'] as $node) {
echo $node->getElementsByTagName( "sDetail" )->item(0)->nodeValue;
}