Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello i am trying to parse xml i have couple of trouble do this.

I am not able to get nested level element.For eg.telephone I am not able to get attribute value.I am not able to get and the third level nested element properly, i tried many code for that also for attribute values of some elements. like here telephone type and commercialListingType

Here is my 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>

Here is my code

<?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);

?>  
share|improve this question

2 Answers 2

$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>';
}
share|improve this answer

The problem you have is in this line:

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

As $commercialListingType is a SimpleXMLElement you don't need to cast it to an array - it allows to access anything you need already via it's standard object and array-access notations. Casting to an array will reduce the functionality with the risk to break it.

And that is what is exactly happening here in your case on the next line:

print_r($commercialListingType->attributes());

Give you the famous

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

error, as you told PHP to make $commercialListingType an array - which is not an object in PHP.

So better understand that with SimpleXMLElement, there is no need to cast to array:

$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],
    );
}

You find more usage examples in the PHP manual: http://www.php.net//manual/en/simplexml.examples-basic.php

This will also explain to you how to access attributes.

So remember: It is a fault casting to array with SimpleXML. Whoever told you to do that and said to you it would make things more easy, was not telling you the whole story.

Exemplary output:

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

)

Demo online: https://eval.in/159675 ; Full Code:

<?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]</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);
share|improve this answer
    
Hi thanks for help.But i still get telephone and mobile empty i don't know whats the reason.may be they are nested to third level? i get first level data fine or the use attribute that's why? –  vishal Jun 7 at 12:37
    
Those two are empty, take a look into the XML. Those two tags are empty, they do not contain any text. This is why telephone and mobile are empty in the $data array, too. –  hakre Jun 7 at 12:39
1  
You wnat me to suggest some random telephone number generator? :) –  hakre Jun 7 at 12:40
    
Oh yes sorry.....your awesome.Thank you very much again –  vishal Jun 7 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.