0

I saw lot of examples but nothing is working perfectly. This is the array i got after parsing.

SimpleXMLElement Object
(
[@attributes] => Array
    (
        [areaUnits] => acre
    )

)

Now i try to get attributes,like this:

var_dump($list->attributes());

I got this error:

 var_dump(): Node no longer exists 
4
  • please provide the xml file so we can have a better view of you issue Commented Nov 3, 2014 at 14:44
  • It's a huge file. I just gave you some node here. define("XMLNS_COMMON","rets.org/xsd/RETSCommons"); <Listing> <LivingArea>2200</LivingArea> <LotSize commons:areaUnits="acre">0.897</LotSize> <YearBuilt>1992</YearBuilt> </Listing> Now i get the array for this $list = $eachListing->LotSize->children(XMLNS_COMMON); Commented Nov 3, 2014 at 14:49
  • 1
    based on what you provided, there is no way to replicate your issue. For me at least, I need: more php code before and after the var_dump, at least 1 xml node Commented Nov 3, 2014 at 14:52
  • @-Ares <Listings xmlns="rets.org/xsd/Syndication/2012-03" xmlns:commons="rets.org/xsd/RETSCommons" xmlns:xsi="w3.org/2001/XMLSchema-instance" xml:lang="en-us"> <Listing> <LivingArea>2200</LivingArea> <LotSize commons:areaUnits="acre">0.897</LotSize> <YearBuilt>1992</YearBuilt> <ListingDate>2012-01-06</ListingDate> <ListingTitle>Ranch, Ranch - Morgantown, WV</ListingTitle> <FullBathrooms>2</FullBathrooms> </Listing> </Listings> $xml=simplexml_load_file($filename); is this ok now for you? Commented Nov 3, 2014 at 15:55

2 Answers 2

0
<?php
function xml_attribute($object, $attribute)
{
    if(isset($object[$attribute]))
    return (string) $object[$attribute];
}
print xml_attribute($xml, 'areaUnits'); //prints "acre"
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this print $this->xml_attribute($list, 'areaUnits'); but it's not satisfy this "isset($object[$attribute]". I am using Yii is this any problem for Yii
yiiframework.com/forum/index.php/topic/… will this satisfy your question? i'm not sure the above code for yii
Your code should work perfectly. But i don't know why it's not working.
0

Obtaining the attributes of a SimpleXMLElement is very straight forward.

The XML:

<?xml version="1.0"?>
<root>
  <node attribute1="value1" attribute2="value2">data</node>
</root>

The PHP:

// assume $xml variable contains the XML document above
$sxe = new SimpleXMLElement($xml)
$value1 = $sxe->node->attributes()->attribute1;
$value2 = $sxe->node->attributes()->attribute2;

In your example above, $list MUST reference an actual XML node in order for you to attempt to access its attributes. Based on your error, it sounds like you're not doing that, which can often happen if you modify the XML structure referenced by $list at run time.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.