你好,我想解析XML我有几个麻烦做到这一点。用PHP解析XML并获取嵌套的级别元素值和属性值
我无法获得嵌套的级别元素。例如,电话 我无法获得属性值。我无法正确获取第三级嵌套元素,我尝试了很多代码一些元素的属性值。 喜欢这里的电话类型和commercialListingType
这里是我的xml
<propertyList date="2014-06-02-17:30:33" username="bad" password="dfdfd">
<business modTime="2014-06-02-12:22:32" status="current">
<agentID>TEST</agentID>
<uniqueID>1420648</uniqueID>
<listingAgent id="1">
<name>hjon Smith</name>
<telephone type="BH"></telephone>
<telephone type="mobile"></telephone>
<email>[email protected]</email>
</listingAgent><listingAgent id="2"></listingAgent>
<address display="no">
<subNumber>Yoghurt bbd 4000</subNumber>
<streetNumber></streetNumber>
<street></street>
<suburb display="no">Newy</suburb>
<state>NSW</state>
<postcode>2000</postcode>
<country>London</country>
</address>
<price display="yes" plusSAV="no" tax="exclusive">200000</price>
<priceView></priceView>
<externalLink href=""/><externalLink href=""/>
<videoLink href=""/>
<underOffer value="no"/>
<commercialListingType value="sale"/>
<franchise value="yes"/>
<businessCategory id="1">
<name>Franchise</name>
</businessCategory>
</propertyList>
这里是我的代码
<?php
$xml=simplexml_load_file("testing.xml");
$data = array();
foreach($xml->business as $business) {
$business = (array) $business;
if (!array_key_exists($business['uniqueID'], $data)) {
$listingAgent = (array) $business['listingAgent'];
$price = (array) $business['price'];
$commercialListingType= (array)$business['commercialListingType'];
print_r($commercialListingType->attributes());
$data[$business['uniqueID']] = array(
'agentID' => $business['agentID'],
'uniqueID' => $business['uniqueID'],
'name' => (string)$listingAgent[0]->name,
'email' => (string) $listingAgent[0]->email,
'price'=>(string) $price[0],
'telephone' => (string) $listingAgent[0]->telephone[0],
'mobile' => (string) $listingAgent[0]->telephone[1],
);
}
}
echo "<pre>";
print_r($data);
?>
嗨,感谢您的帮助。但我仍然得到电话和手机空我不知道是什么原因。可能是他们嵌套到第三级?我得到的第一级数据正常或使用属性,这是为什么? – Vishal
这两个*都是空的,看看XML。这两个标签是空的,它们不包含任何文本。这就是为什么电话和手机在'$ data'数组中也是空的。 – hakre
你让我建议一些随机电话号码发生器? :) – hakre