Zend_Feed-ModifyingFeed.xml 2.7 KB

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