<entry>
<title>Some title</title>
<author>
<name>Author</name>
</author>
<source:resource url="http://somesite.com?tid=123456"/>
</entry>
I am able to get the values for things like title and author->name
but I need to get the value of the attribute for source:resource,
namely 123456 in this case
What I have so far.. with the above assigned as $xmlstr ..
[code]
if ($xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOEMPTYTAG))
{}else{
echo 'Something isn\'t right.'; exit();
}
foreach ($xml->entry as $entry) {
$title = $entry->title;
$name = $entry->author->name;
/*$tid = presumably it is somehow accessable with SimpleXMLElement-
>attributes
but the parser seems to dislike the colons in the tag*/
}
[/code]
I was hopeing LIBXML_NOEMPTYTAG would help, but doesn't seem to.
Any hints ?
Tags with colons, like <source:resource>, use namespaces. Many XML
parsers have poor support for namespaces, or none at all. As far as I
know SimpleXML can handle namespaces but the documentation is not very
clear. I had to parse such XML not long ago and I couldn't manage to do
it with SimpleXML so I can't provide you with working code but I'll tell
you what I learnt in case it helps.
1) You need to have somewhere in your XML an attribute like this:
xmlns:source="some-unique-id"
I guess your full XML file has it. This prevents the "namespace error"
warning in simplexml_load_string().
2) Supposedly, you need to use the children() method to get elements
that belong to a namespace:
$children = $entry->children('source', TRUE);
3) Once you have the element, you need to fetch the URL as *attribute*.
This works for me when there aren't namespaces:
<foo bar="blah"> ---> $xml->foo['bar']
See also the attributes() method.
> I was hopeing LIBXML_NOEMPTYTAG would help, but doesn't seem to.
According to docs this option *removes* blank nodes:
http://es.php.net/manual/en/libxml.constants.php
As I said, these are only general ideas. I've never made it work myself.
If it's an option, you might consider other tools like XMLReader or a
PHP port of JavaScript's jQuery library called phpQuery:
http://code.google.com/p/phpquery/
--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--