首先,我是PHP的初学者。特别是当我必须处理XML解析时,我现在正在努力解决它,很多xml。小时和小时我已经尝试解决它已经让我头痛。
这里是XML:
<v1:product>
<v1:data>
<v1:account_id>5637</v1:account_id>
<v1:account_name>John Doe</v1:account_name>
<v1:product_id>f4dc8300-1f13-11e8-bfa43d4d9ee60f6b</v1:product_id>
<v1:product_name>Product Test</v1:product_name>
<v1:product_desc>
<v1:name>Arc</v1:name>
<v1:unit>boxes</v1:unit>
<v1:value>10</v1:value>
</v1:product_desc>
<v1:product_desc>
<v1:name>Birg</v1:name>
<v1:unit>kilos</v1:unit>
<v1:value>2</v1:value>
</v1:product_desc>
<v1:product_desc>
<v1:name>Cyitha</v1:name>
<v1:unit>Minutes</v1:unit>
<v1:value>30</v1:value>
</v1:product_desc>
<v1:offer>
<v1:offer_id>3575374</v1:offer_id>
<v1:offer_name>Flash</v1:offer_name>
</v1:offer>
</v1:data>
<v1:data>
<v1:account_id>5892</v1:account_id>
<v1:account_name>John Doe</v1:account_name>
<v1:product_id>jsad2sdx-asd2-983j</v1:product_id>
<v1:product_name>Product Test 2</v1:product_name>
<v1:product_desc>
<v1:name>Arc</v1:name>
<v1:unit>boxes</v1:unit>
<v1:value>2</v1:value>
</v1:product_desc>
<v1:product_desc>
<v1:name>Birg</v1:name>
<v1:unit>kilos</v1:unit>
<v1:value>10</v1:value>
</v1:product_desc>
<v1:product_desc>
<v1:name>Cyitha</v1:name>
<v1:unit>Minutes</v1:unit>
<v1:value>99</v1:value>
</v1:product_desc>
<v1:offer>
<v1:offer_id>3575374</v1:offer_id>
<v1:offer_name>Flash</v1:offer_name>
</v1:offer>
</v1:data>
</v1:product>
这就是我在PHP中所拥有的
$objGetEmAll = new DOMDocument();
$objGetEmAll->loadXML($theXML);
$datas = $objGetEmAll->getElementsByTagName('data');
$responseValue = array();
foreach($datas as $data) //each element of DATA
{
$dataValue = array();
if($data->childNodes->length)
{
foreach($data->childNodes as $i)
{
$dataValue[$i->nodeName] = $i->nodeValue;
}
}
$responseValue[] = $dataValue;
}
但是当我想在product_desc
标记内动态提取值时仍然失败。我想将上面的XML更改为如下所示的数组
Array
(
[0] => Array
(
[v1:account_id] => 5637
[v1:account_name] => adsfafds
[v1:product_id] => 124asd
[v1:product_name] => HALO
[v1:product_desc] => Array
(
[0] => Array
(
[v1:name] => A
[v1:unit] => BOXes
[v1:value] => 7
)
[1] => Array
(
[v1:name] => B
[v1:unit] => mins
[v1:value] => 1000
)
[2] => Array
(
[v1:name] => C
[v1:unit] => call
[v1:value] => 700
)
[3] => Array
(
[v1:name] => D
[v1:unit] => GB
[v1:value] => 4
)
)
[v1:offer] => Array
(
[v1:offer_id] => 3575374
[v1:offer_name] => Flash
)
)
)
我知道也许这很容易,但对于那些刚刚参加上个月编程的人来说,这很令人困惑。欢迎链接或帮助
答案 0 :(得分:0)
使用此
$objGetEmAll = new DOMDocument();
$objGetEmAll->loadXML($theXML);
$datas = $objGetEmAll->getElementsByTagName('data');
$responseValue = array();
foreach($datas as $data) //each element of DATA
{
$dataValue = array();
$product_desc_counter = 0;
$offer_counter = 0;
if($data->childNodes->length)
{
foreach($data->childNodes as $i) {
// for v1:product_desc group case :1
if($i->nodeName == 'v1:product_desc' ){
foreach($i->childNodes as $p) {
$dataValue[$i->nodeName][ $product_desc_counter][$p->nodeName] = $p->nodeValue;
}
$product_desc_counter ++;
} else if($i->nodeName == 'v1:offer' ) // for offer group case:2
{
foreach($i->childNodes as $p) {
$dataValue[$i->nodeName][ $offer_counter][$p->nodeName] = $p->nodeValue;
}
$offer_counter ++;
}
else // case:3
$dataValue[$i->nodeName] = $i->nodeValue;
}
}
$responseValue[] = $dataValue;
}
在上面的行中,我们循环遍历所有data
个节点,然后检查它是否有任何节点,如果它没有(如项目 account_name , product_id ..)我们只是将它nodeValue
存储在我们的结果数组中即ie。在$responseValue
。如果它有任何子项(v1:product_desc , v1:offer 即案例:1或2),我们再次循环其子项并将它们附加到相应键和相应的循环计数器($product_desc_counter
或$offer_counter
)。一开始可能很难理解(特别是数组索引),但是在摆弄它一段时间后你就会掌握它。