echo simplexml object

后端 未结 4 2053
失恋的感觉
失恋的感觉 2021-01-14 21:21

This question has two parts.

Part 1. Yesterday I had some code which would echo the entire content of the XML from an RSS feed. Then I deleted it fr

相关标签:
4条回答
  • 2021-01-14 21:59

    According to the php manual:

    $xml = new SimpleXMLElement($string);
    .
    .
    .
    

    then if you want to echo the result:

    echo $xml->asXML();
    

    or save the xml to a file:

    $xml->asXML('blog.xml'); 
    

    References

    • http://php.net/manual/fr/simplexmlelement.asxml.php

    • http://spotlesswebdesign.com/blog.php?id=14

    0 讨论(0)
  • 2021-01-14 22:02

    The simplexml_load_file returns an SimpleXMLElement, so:

    print_r($xml);
    

    will show its minor objects and arrays.

    After your tweaks you can call $xml->asXML("filename.xml"); as @Tim Withers pointed out.

    0 讨论(0)
  • 2021-01-14 22:02

    Part 1: echo $xml->asXML(); - http://www.php.net/manual/en/simplexmlelement.asxml.php

    Part 2: php SimpleXML check if a child exists

    $html .= "<h3 class='blog'>".($entry->title!=null?$entry->title:'No Title')
        . "<span class='pubDate'> | " .$pubDate . "</span></h3><p class='blog'>" 
        . $entry->content . "</p>";
    

    Note I would probably load the url like this:

    $feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default';
    $rawFeed = file_get_contents($feedUrl);
    $xml = new SimpleXmlElement($rawFeed);
    

    Based on your comment in regards to part 1, I am not sure if the XML is being loaded completely. If you try loading it this way, it should display all the XML data.

    0 讨论(0)
  • 2021-01-14 22:07

    Part 1

    This is still not exactly what I wanted, but rather a very tidy and organized way of echoing the xml data:

        $url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
    $xml = simplexml_load_file($url);
    echo '<pre>';
    print_r($xml);
    

    Part 2

    I had to get firephp running so I could see exactly what elements php was encountering when it reached an entry without a blog title. Ultimately it is an empty array. Therefore, the simple:

    if(empty($entry->title))
    

    works perfectly. For string comparison, I found that you can simply cast it as a string. For my purposes, that was unnecessary.

    0 讨论(0)
提交回复
热议问题