Connecting Tech Pros Worldwide Help | Site Map

Need help getting values with simplexml

duane.lortie@gmail.com
Guest
 
Posts: n/a
#1: Nov 2 '08
I'm writing a routine that fetches XML and attempts to parse some
values from it.
A condensed entry node of the XML looks like this ..

<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 ..

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ($xml = simplexml_load_string($xmlstr, NULL, LIBXML_NOEMPTYTAG))
  3. {}else{
  4. echo 'Something isn\'t right.'; exit();
  5. }
  6.  
  7. foreach ($xml->entry as $entry) {
  8. $title = $entry->title;
  9. $name = $entry->author->name;
  10. /*$tid =  presumably it is somehow accessable with SimpleXMLElement-
  11.     Quote:
  •  
  •                 >attributes
  •  
  • but the parser seems to dislike the colons in the tag*/
  • }
  •  
  •  
  • I was hopeing LIBXML_NOEMPTYTAG would help, but doesn't seem to.

    Any hints ?
    =?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=
    Guest
     
    Posts: n/a
    #2: Nov 3 '08

    re: Need help getting values with simplexml


    duane.lortie@gmail.com escribió:
    Quote:
    I'm writing a routine that fetches XML and attempts to parse some
    values from it.
    A condensed entry node of the XML looks like this ..
    >
    <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
    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.
    Quote:
    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
    --
    Closed Thread