簡體   English   中英

使用PHP解析XML並獲取嵌套的級別元素值和屬性值

[英]Parse XML with PHP and get nested level element value and attribute value

您好,我正在嘗試解析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>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);

?>  
$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>';
}

您遇到的問題是在此行中:

$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/zh/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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM