Jump to content

SimpleXML and Atom


jrw4
 Share

Recommended Posts

Ok, to my question. I am making a simple atom feed parser. So far here is my code:

 

<?php

    // read in an xml file
      $myFile = "an_atom_feed.xml";
    
    $feed = simplexml_load_file($myFile);
    
    $xml =  $feed->children('http://www.w3.org/2005/Atom');
    
    echo "<h1>".$xml->title . "</h1>\n";
    
    foreach ($xml->entry as $entries) {
        $child = $entries->children('http://www.w3.org/2005/Atom');
        // post title
        echo "<h3>".$child->title . "</h3>\n";
        // post author
        if (!empty($child->author->name)) echo "<p>Posted by " . $child->author->name . "</p>\n";
        // post content
        if (!empty($child->summary)) echo htmlspecialchars($child->summary) . "<br />\n";
        echo $child->content->asXML();
    }
    
?> 

 

So far it seems to work fairly well. Obviously I am not dealing with every item in <entry> just the content and title.

 

Now my question comes with, say my feed has the following items in it:

 

<entry>

<title>Test entry #3</title>

<id>http://example.org/article3</id>

<updated>2006-10-03T13:14:34Z</updated>

<link href="http://example.org/article3" />

<content type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">

<xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)" /></xhtml:div>

</content>

</entry>

 

<entry>

<title>Test entry #4</title>

<id>http://example.org/article4</id>

<updated>2006-10-03T14:14:34Z</updated>

<link href="http://example.org/article4" />

<content type="xhtml">

<div xmlns="http://www.w3.org/1999/xhtml"><p>This is also <em>very</em> exciting.</p>

<p>Yes, it is.</p></div>

</content>

</entry>

 

I haven't been able to find a tutorial on how to deal with content types like those so I am not sure how to.

 

If you know of tutorials that show me how to deal with stuff like this, that would be very appreciated. If you also know how to deal with it, teaching me would make me très appreciative.

 

Thanks a lot

Link to comment
Share on other sites

Im not sure what exactly you are trying to do but if you want to display the content one you can display it using the regular $xml->content or wherever you are calling it from. if you need to see a detailed list of what is being show you can always do print_r(). Example:

 

$xml = simplexml_load_file($yourfile);
print_r($xml);

 

Then you can see what values are there to use. If you cant see the full page because its ouputting the data you can always view source for the page.

Link to comment
Share on other sites

What am I trying to do?  I am trying to display each content value for each entry in an atom feed.

 

echoing $xml->entry->content is fine and dandy if the attribute type of the content is "text" or "html".  If it is xhtml then I am running into a problem.

 

If I have an item like this in the feed:

 

<entry>

  <title>Test entry #3</title>

  <id>http://example.org/article3</id>

  <updated>2006-10-03T13:14:34Z</updated>

  <link href="http://example.org/article3" />

  <content type="xhtml" xmlns:xhtml="http://www.w3.org/1999/xhtml">

 

    <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)" /></xhtml:div>

  </content>

</entry>

 

and I echo $xml->entry->content or $xml->entry->content->asXML() it will not appear properly.  Currently I am getting something like:

 

<content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">

    <xhtml:div>This is also <xhtml:em>very</xhtml:em> exciting. & <xhtml:img src="/img/smiley" alt=":-)"/></xhtml:div>

  </content>

 

I am trying to figure out how to get it to appear as:

 

<div>This is also <em>very</em> exciting. & <img alt=":-)" src="/img/smiley"/></div>

 

There must be an built in way in SimpleXML to do this instead of having me to do it by hand.  This is what I am asking to do.  Is how to get an output that looks like the later instead of what I currently have which is the former.

Link to comment
Share on other sites

  • 2 years later...

I know this topic is now old enough to be legend, but @jrw4 did you ever got anywhere with switching to XMLReader? It seems like libxml's DOM and SimpleXML should be able to handle this.

 

$dom = new DOMDocument('1.0', 'utf-8');

$element = $dom->importNode(dom_import_simplexml($xml->node->here), true);
$dom->appendChild($element);

return $dom->saveHTML(); 

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.