SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleXML Problem.

    When I print_r() on a SimpleXML object, it don't seem to return the root element.

    I need to return the root element to know what sort of RSS feed I'm dealing with, for example <rdf:RDF ... is going to be RSS 1.0 or 0.9 , but I can't seem to access that element. It's not visible when printing back.

    The only other way I can see of doing this is by using stream_get_contents() and a regular expression, but that just seems like additional work when I'm pretty sure SimpleXML can do it ... I just don't know how.

    Any ideas?

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Call the getName method on your SimpleXML object and you should get RDF.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    Call the getName method on your SimpleXML object and you should get RDF.
    Thats correct yes. But when I tried that, and tried to get the attribute values, I was getting an error.

    PHP Code:
    foreach($rss->getFeed()->getName()->attributes() as $el => $val) {
                    print 
    $val "</br>";
                } 

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    getName only returns a string, the name of the element. If you want to get at the attributes, you don't need it.
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    getName only returns a string, the name of the element. If you want to get at the attributes, you don't need it.
    That's what I'm trying to do. Get the attributes of the root element.

  6. #6
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lgnokia View Post
    yes,u know what are the attributes?
    Yeah. "xmlns" is one of the attributes I need. Just struggling atm to get the attribute value.

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyone? This is really bugging me because I have even tried using the DOM.

    PHP Code:
    $dom = new DomDocument();
                
    $dom->load($feed);
                
    $root $dom->documentElement->getElementsByTagName("rdf:RDF");
                
    $item1 $root->length;
                
                print 
    $item1
    Tried $dom->documentElement->getElementsByTagName("rdf:RDF"); with "RDF" aswell. Length always returns 0.

  8. #8
    SitePoint Zealot
    Join Date
    May 2007
    Location
    West Midlands, UK
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even tried this and the <rdf:RDF ... node isn't even there:

    [PHP]
    $dom = new DomDocument();
    $dom->load($feed);
    $root = $dom->documentElement->getElementsByTagName('*');
    $item1 = $root->length;

    for($i = 0; $i < $item1; $i++) {
    print_r($root->item($i)->nodeName);
    }
    [PHP]

    Any help is much appreciated because I've stuck on this for a while now.

  9. #9
    SitePoint Enthusiast
    Join Date
    Mar 2009
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $xml simplexml_load_file('sample.xml');
    $namespaces $xml->getNamespaces(true);
    var_dump($namespaces); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •