使用PHP解析XML并获取嵌套的级元素值和属性值

时间:2014-06-07 10:36:06

标签: php xml parsing

您好我正在尝试解析xml我有几个麻烦这样做。

我无法获得嵌套级别元素。对于eg.telephone 我无法获得属性值。我无法正确获得第三级嵌套元素,我为一些元素的属性值尝试了许多代码。 喜欢这里电话类型和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>bbd@ozemail.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>
    </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);

?>  

2 个答案:

答案 0 :(得分: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>';
}

答案 1 :(得分:0)

你遇到的问题是:

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

由于$commercialListingType是一个SimpleXMLElement,您不需要将其强制转换为数组 - 它允许通过标准对象和数组访问符号访问您需要的任何内容。转换为数组会降低功能,但存在破坏它的风险。

这就是下一行中你的情况正是如此:

print_r($commercialListingType->attributes());

给你着名的

  

致命错误:在非对象上调用成员函数attributes()

错误,因为你告诉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] => bbd@ozemail.com.au
            [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>bbd@ozemail.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);