Zend_Feed-ModifyingFeed.xml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 XML 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 <code>&lt;?xml ... &gt;</code> prologue) XML
  21. representation of the new entry, including any necessary XML namespaces.
  22. </para>
  23. <para>
  24. Note that the above will work even if the existing entry does not already have an author
  25. tag. You can use as many levels of <code>-&gt;</code> access as you like before getting
  26. to an assignment; all of the intervening levels will be created for you automatically if
  27. necessary.
  28. </para>
  29. </example>
  30. <para>
  31. If you want to use a namespace other than <code>atom:</code>, <code>rss:</code>, or
  32. <code>osrss:</code> in your entry, you need to register the namespace with
  33. <classname>Zend_Feed</classname> using
  34. <classname>Zend_Feed::registerNamespace()</classname>. When you are modifying an existing
  35. element, it will always maintain its original namespace. When adding a new element, it will
  36. go into the default namespace if you do not explicitly specify another namespace.
  37. </para>
  38. <example id="zend.feed.modifying-feed.example.creating">
  39. <title>Creating an Atom Entry with Elements of Custom Namespaces</title>
  40. <programlisting language="php"><![CDATA[
  41. $entry = new Zend_Feed_Entry_Atom();
  42. // id is always assigned by the server in Atom
  43. $entry->title = 'my custom entry';
  44. $entry->author->name = 'Example Author';
  45. $entry->author->email = 'me@example.com';
  46. // Now do the custom part.
  47. Zend_Feed::registerNamespace('myns', 'http://www.example.com/myns/1.0');
  48. $entry->{'myns:myelement_one'} = 'my first custom value';
  49. $entry->{'myns:container_elt'}->part1 = 'first nested custom part';
  50. $entry->{'myns:container_elt'}->part2 = 'second nested custom part';
  51. echo $entry->saveXML();
  52. ]]></programlisting>
  53. </example>
  54. </sect1>
  55. <!--
  56. vim:se ts=4 sw=4 et:
  57. -->