2014-06-07 69 views
0

你好,我想解析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); 

?> 

回答

0
$xml = new SimpleXMLElement($string); //variable $string is nothing but your XML content 
$result = $xml->xpath('/propertyList/business/listingAgent/telephone'); 
//short form 
//$result = $xml->xpath('////telephone'); 
foreach($result as $node) { 
    print 'Telephone Atrribute: '.$node->attributes().'<br>'; 
} 
0

你的问题是在这条线:

$commercialListingType = (array)$business['commercialListingType']; 

As $commercialListingType是一个SimpleXMLElement,您不需要将其转换为数组 - 它允许通过标准对象和数组访问符号访问任何您需要的东西。投射到阵列将会降低功能并带来破坏它的风险。

而这究竟是什么发生在这里,你的情况下一行:

print_r($commercialListingType->attributes()); 

给你了著名

Fatal error: Call to a member function attributes() on a non-object

错误,因为你告诉PHP做$commercialListingType阵列 - 这不是PHP中的对象。

因此,更好地理解与SimpleXMLElement对象,没有必要强制转换为数组:

$data = array(); 

foreach ($xml->business as $business) 
{ 
    $uniqueID = (string)$business->uniqueID; 

    if (isset($data[$uniqueID])) { 
     continue; 
    } 

    $listingAgent   = $business->listingAgent; 
    $price     = $business->price; 
    $commercialListingType = $business->commercialListingType; 

    $data[$uniqueID] = array(
     'agentID' => (string) $business->agentID, 
     'uniqueID' => $uniqueID, 
     'name'  => (string)$listingAgent->name, 
     'email'  => (string)$listingAgent->email, 
     'price'  => (string)$price, 
     'telephone' => (string)$listingAgent->telephone, 
     'mobile' => (string)$listingAgent->telephone[1], 
    ); 
} 

你找到PHP手册更多用法示例:http://www.php.net//manual/en/simplexml.examples-basic.php

这也会向你解释如何访问属性。

所以记住:这是使用SimpleXML转换为数组的错误。谁告诉你这样做,并告诉你,这会让事情变得更容易,而不是告诉你整个故事。

示例性输出:

Array 
(
    [1420648] => Array 
     (
      [agentID] => TEST 
      [uniqueID] => 1420648 
      [name] => hjon Smith 
      [email] => [email protected] 
      [price] => 200000 
      [telephone] => 
      [mobile] => 
     ) 

) 

在线演示:https://eval.in/159675;完整代码:

<?php 
/** 
* @link http://stackoverflow.com/a/24096869/367456 
*/ 
ob_start(); 
?> 
    <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]il.com.au</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> 
     </business> 
    </propertyList> 
<?php 
/** 
*/ 
$xml = simplexml_load_string(ob_get_clean()); 

$data = array(); 

foreach ($xml->business as $business) 
{ 
    $uniqueID = (string)$business->uniqueID; 

    if (isset($data[$uniqueID])) { 
     continue; 
    } 

    $listingAgent   = $business->listingAgent; 
    $price     = $business->price; 
    $commercialListingType = $business->commercialListingType; 

    $data[$uniqueID] = array(
     'agentID' => (string) $business->agentID, 
     'uniqueID' => $uniqueID, 
     'name'  => (string)$listingAgent->name, 
     'email'  => (string)$listingAgent->email, 
     'price'  => (string)$price, 
     'telephone' => (string)$listingAgent->telephone, 
     'mobile' => (string)$listingAgent->telephone[1], 
    ); 
} 

print_r($data); 
+0

嗨,感谢您的帮助。但我仍然得到电话和手机空我不知道是什么原因。可能是他们嵌套到第三级?我得到的第一级数据正常或使用属性,这是为什么? – Vishal

+0

这两个*都是空的,看看XML。这两个标签是空的,它们不包含任何文本。这就是为什么电话和手机在'$ data'数组中也是空的。 – hakre

+1

你让我建议一些随机电话号码发生器? :) – hakre