note 98061 added to simplexmlelement.attributes
| From: | alan at performantsystems dot com | Date: | Mon, 24 May 2010 05:26:44 +0000 |
| Subject: | note 98061 added to simplexmlelement.attributes | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169545@lists.php.net to get a copy of this message | ||
So lets say you have database type data in an XML string called $xmlstring with the key or item ID
as an XML Attribute and all content data as regular XML Elements, as above. SimpleXML processes the
Attributes as an array, so we can play along and push the Attributes into an array. Then we can get
the value of any specific Attribute we want by addressing it by name, such as "ID".
Considering this data:
<?xml version="1.0" encoding="utf-8"?>
<data>
<item ID="30001">
<Company>Navarro Corp.</Company>
</item>
<item ID="30002">
<Company>Performant Systems</Company>
</item>
<item ID="30003">
<Company>Digital Showcase</Company>
</item>
</data>
Example of listing both the ID Attribute and Company Element values:
<?php
$xmlObject = new SimpleXMLElement($xmlstring);
foreach ($xmlObject->children() as $node) {
$arr = $node->attributes(); // returns an array
print ("ID=".$arr["ID"]); // get the value of this attribute
print (" Company=".$node->Company);
print ("<p><hr>");
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 75.27.189.114
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.attributes.php
Edit -- https://master.php.net/note/edit/98061
Del: integrated -- https://master.php.net/note/delete/98061/integrated
Del: useless -- https://master.php.net/note/delete/98061/useless
Del: bad code -- https://master.php.net/note/delete/98061/bad+code
Del: spam -- https://master.php.net/note/delete/98061/spam
Del: non-english -- https://master.php.net/note/delete/98061/non-english
Del: in docs -- https://master.php.net/note/delete/98061/in+docs
Del: other reasons-- https://master.php.net/note/delete/98061
Reject -- https://master.php.net/note/reject/98061
Search -- https://master.php.net/manage/user-notes.php