Discussion:
Starting with XML
leam hall
2013-10-30 13:43:23 UTC
Permalink
I'm trying to work on an XML document and turn it into something I can put
into a database or array. Am getting stuck as I try to learn the PHP stuff
needed for XML.

What I have so far is just:

$xml = simplexml_load_file('my-file.xml');

$status = $xml->status;
echo "status is $status.\n";

$href = $xml->reference->href;
echo "href is $href.\n";

Which gives:

status is accepted.
href is .


If I do:

print_r($xml);

It starts out with:

SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => My_Info
)

[status] => accepted
[title] => Nice long string title
[reference] => SimpleXMLElement Object
(
[@attributes] => Array
(
[href] => http://www.example.com
)

)



How do I reference the nested objects? The file is fairly long and seems
deeply nested.

Thanks!

Leam
--
Mind on a Mission <http://leamhall.blogspot.com/>
Stuart Dallas
2013-10-30 13:53:20 UTC
Permalink
Post by leam hall
I'm trying to work on an XML document and turn it into something I can put
into a database or array. Am getting stuck as I try to learn the PHP stuff
needed for XML.
$xml = simplexml_load_file('my-file.xml');
$status = $xml->status;
echo "status is $status.\n";
$href = $xml->reference->href;
echo "href is $href.\n";
status is accepted.
href is .
print_r($xml);
SimpleXMLElement Object
(
(
[id] => My_Info
)
[status] => accepted
[title] => Nice long string title
[reference] => SimpleXMLElement Object
(
(
[href] => http://www.example.com
)
)
How do I reference the nested objects? The file is fairly long and seems
deeply nested.
The value you’re trying to retrieve is an attribute, and can be accessed using the attributes method: http://php.net/simplexmlelement.attributes

-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
leam hall
2013-10-31 16:14:28 UTC
Permalink
Stuart, thanks for the help so far. I'm diving into the next issue.

#####
Here's a bit from var_dump:

[Group] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => V-756
)

[title] => IDTAG111223
[description] => <GroupDescription></GroupDescription>
[Rule] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => Local_tag_rule
[severity] => medium
[weight] => 10.0
)
#####

The line that fails:

$ruleAttributes =
simplexml_load_string($group->Rule->attributes['severity']);

#####

However, this works:
foreach($group->Rule->attributes() as $k => $v){
echo $k . " => " . $v;
}

#####

The issue is that I just need one value out of the attributes array. I do
not know the purpose of the "@" in [@attributes]

Suggestions?

Leam
Post by leam hall
I'm trying to work on an XML document and turn it into something I can
put
Post by leam hall
into a database or array. Am getting stuck as I try to learn the PHP
stuff
Post by leam hall
needed for XML.
$xml = simplexml_load_file('my-file.xml');
$status = $xml->status;
echo "status is $status.\n";
$href = $xml->reference->href;
echo "href is $href.\n";
status is accepted.
href is .
print_r($xml);
SimpleXMLElement Object
(
(
[id] => My_Info
)
[status] => accepted
[title] => Nice long string title
[reference] => SimpleXMLElement Object
(
(
[href] => http://www.example.com
)
)
How do I reference the nested objects? The file is fairly long and seems
deeply nested.
The value you’re trying to retrieve is an attribute, and can be accessed
using the attributes method: http://php.net/simplexmlelement.attributes
-Stuart
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/
--
Mind on a Mission <http://leamhall.blogspot.com/>
Loading...