Zend_Feed-ModifyingFeed.xml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.feed.modifying-feed">
  4. <title>Modifying Feed and Entry structures</title>
  5. <para>
  6. <classname>Zend_Feed</classname>'s natural syntax extends to constructing and modifying
  7. feeds and entries as well as reading them. You can easily turn your new or modified objects
  8. back into well-formed <acronym>XML</acronym> for saving to a file or sending to a server.
  9. </para>
  10. <example id="zend.feed.modifying-feed.example.modifying">
  11. <title>Modifying an Existing Feed Entry</title>
  12. <programlisting language="php"><![CDATA[
  13. $feed = new Zend_Feed_Atom('http://atom.example.com/feed/1');
  14. $entry = $feed->current();
  15. $entry->title = 'This is a new title';
  16. $entry->author->email = 'my_email@example.com';
  17. echo $entry->saveXML();
  18. ]]></programlisting>
  19. <para>
  20. This will output a full (includes <command>&lt;?xml ... &gt;</command> prologue)
  21. <acronym>XML</acronym> representation of the new entry, including any necessary
  22. <acronym>XML</acronym> namespaces.
  23. </para>
  24. <para>
  25. Note that the above will work even if the existing entry does not already have an author
  26. tag. You can use as many levels of <command>-&gt;</command> access as you like before
  27. getting to an assignment; all of the intervening levels will be created for you
  28. automatically if necessary.
  29. </para>
  30. </example>
  31. <para>
  32. If you want to use a namespace other than <command>atom:</command>, <command>rss:</command>,
  33. or <command>osrss:</command> in your entry, you need to register the namespace with
  34. <classname>Zend_Feed</classname> using
  35. <methodname>Zend_Feed::registerNamespace()</methodname>. When you are modifying an existing
  36. element, it will always maintain its original namespace. When adding a new element, it will
  37. go into the default namespace if you do not explicitly specify another namespace.
  38. </para>
  39. <example id="zend.feed.modifying-feed.example.creating">
  40. <title>Creating an Atom Entry with Elements of Custom Namespaces</title>
  41. <programlisting language="php"><![CDATA[
  42. $entry = new Zend_Feed_Entry_Atom();
  43. // id is always assigned by the server in Atom
  44. $entry->title = 'my custom entry';
  45. $entry->author->name = 'Example Author';
  46. $entry->author->email = 'me@example.com';
  47. // Now do the custom part.
  48. Zend_Feed::registerNamespace('myns', 'http://www.example.com/myns/1.0');
  49. $entry->{'myns:myelement_one'} = 'my first custom value';
  50. $entry->{'myns:container_elt'}->part1 = 'first nested custom part';
  51. $entry->{'myns:container_elt'}->part2 = 'second nested custom part';
  52. echo $entry->saveXML();
  53. ]]></programlisting>
  54. </example>
  55. </sect1>
  56. <!--
  57. vim:se ts=4 sw=4 et:
  58. -->