<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Web Cash &#187; simplexml</title>
	<atom:link href="http://www.earn-web-cash.com/tag/simplexml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Tips to Ensure Your XML RSS Feed is Valid</title>
		<link>http://www.earn-web-cash.com/2008/03/15/valid-xml-rss-feed/</link>
		<comments>http://www.earn-web-cash.com/2008/03/15/valid-xml-rss-feed/#comments</comments>
		<pubDate>Sat, 15 Mar 2008 14:01:06 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[standards]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/03/15/valid-xml-rss-feed/</guid>
		<description><![CDATA[Last month, I wrote on article on creating an RSS feed for your site. Some people have reported problems with the process &#8211; but these all come from malformed XML, not the PHP code supplied in the article. There are a few things you need to keep in mind while writing an XML document, or [...]]]></description>
			<content:encoded><![CDATA[<p>Last month, I wrote on article on <a href="http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/">creating an RSS feed for your site</a>.  Some people have reported problems with the process &#8211; but these all come from malformed XML, not the PHP code supplied in the article.</p>
<p>There are a few things you <strong>need</strong> to keep in mind while writing an XML document, or else the RSS reader will not be able to understand it.  It&#8217;s better to <a href="http://hsivonen.iki.fi/producing-xml/">think of writing XML as writing code than text</a>, because there are some strict syntax rules you need to adhere to.<br />
<span id="more-177"></span></p>
<h4>Balance Opening and Closing Tags</h4>
<p>One strict syntax rule of XML is that opening and closing tags must be balanced.  For every opening tag, there must be a closing tag.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In this example, the first item element is invalid &#8211; because description has no closing tag.  The second example is valid, because description is closed.  You could also use a self-closing tag if description doesn&#8217;t contain a value.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>With a simple feed, it can be easy enough to keep tags balanced.  However, it&#8217;s a good idea to look into <a href="http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/">using an XML parser like SimpleXML to build the feed</a>.  This ensures that the tags are balanced and that the XML is well-formed.</p>
<h4>Ampersands Are Not Allowed</h4>
<p>The lovely ampersand &#8211; &amp; &#8211; is not allowed to appear in an XML document without being escaped (<code>&amp;amp;</code>).</p>
<p>If a browser or feed reader encounters an ampersand that isn&#8217;t escaped and that isn&#8217;t being used to form a valid entity, the application will simply stop reading.  You might overlook this possible problem if your URLs use query strings &#8211; and thus have ampersands inside them.</p>
<p>The solution is to escape all of the URLs and data that is going into your feed.  The reader will convert <code>&amp;amp;</code> back into &amp; so that your links work correctly.  PHP offers a function to help you do that.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$escapedText</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$unescapedText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Pass all of the text that needs to be escaped (like your URLs) through the htmlentities function, and your problems will be solved.  Incidentally, this will also escape &gt; and &lt; characters.</p>
<h4>No Non-Breaking Spaces!</h4>
<p>The final tip deals with invalid entities, like <code>&amp;nbsp;</code>.  As mentioned before, ampersands are a strict no-no.  They can be used to form entity codes, but they cannot be used by themselves.</p>
<p>If you attempt to create an entity that the XML processor doesn&#8217;t recognize, it won&#8217;t read it as an entity &#8211; it will read it as an ampersand with some text following.  For example, <code>&amp;bob;</code> would be read by an XML processor as an ampersand and the word &#8220;bob;&#8221;.</p>
<p>It would then promptly stop processing your feed.</p>
<p>One common way for this to break your feed is the non-breaking space character &#8211; <code>&amp;nbsp;</code>.  These often serve no purpose in the document, but you might get them littered around your source code if you use a WYSIWYG editor.</p>
<p>You could get rid of these by filtering your output through a function that does a str_replace of <code>&amp;nbsp;</code> with &#8216; &#8216;.</p>
<p>If the non-breaking space character serves a purpose in your document, you can preserve it for the browser but make the XML processor ignore it.  This is where CDATA comes in.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #339933;">&lt;![CDATA[This is a description with content that</span>
<span style="color: #339933;">&lt;strong&gt;shouldn't&lt;/strong&gt; be interpeted</span>
<span style="color: #339933;">by the &amp;nbsp; XML processor.]]&gt;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When you wrap text inside that tag &#8211; <code>&lt;![CDATA[</code> and <code>]]&gt;</code> &#8211; you tell the processor to ignore the text and spit it out as is.  This can help your non-breaking space character make it to the user.  You may also want to use this if your descriptions and content include HTML tags.</p>
<h4>Browse the Spec</h4>
<p>These are a couple of tips to help you address some of the more common things that will break your XML documents.</p>
<p>However, there are many other minor errors you can come across.  The only way to learn about them and solve them is through two things &#8211; research and trial and error.</p>
<p>You may want to take some time to browse the <a href="http://www.xml.com/axml/testaxml.htm">XML specification</a> as well as read forums for problems that other people have.  Then, test things out and find out what doesn&#8217;t work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/03/15/valid-xml-rss-feed/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>RSS Feed: Building an RSS Data Feed in PHP with SimpleXML</title>
		<link>http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/</link>
		<comments>http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 01:48:07 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/</guid>
		<description><![CDATA[RSS feeds are a must-have for modern websites. It&#8217;s easy enough to make an RSS feed of recent articles in PHP. But did you know an RSS feed can simply be information &#8211; not links to articles? This article will look at how to build a basic feed of data &#8211; for our purposes, we&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>RSS feeds are a must-have for modern websites.  It&#8217;s easy enough to <a href="http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/">make an RSS feed of recent articles in PHP</a>.  But did you know an RSS feed can simply be information &#8211; not links to articles?</p>
<p>This article will look at how to build a basic feed of data &#8211; for our purposes, we&#8217;ll consider random quotes.  It will also illustrate how to use SimpleXML to build the feed for us.<br />
<span id="more-145"></span></p>
<h5>The RSS 2.0 Standard</h5>
<p>RSS feeds, like XML in general, have specific standards.  In order to be use-able across platforms, it is imperative that standard syntax and naming patterns are used.  If you&#8217;re unfamiliar with the RSS 2.0 standard, you should look through a <a href="http://cyber.law.harvard.edu/rss/rss.html">copy here</a>.</p>
<p>Before we get to building the feed, let&#8217;s identify what tags we&#8217;re going to use.</p>
<p>The entire feed is wrapped in an <strong>&lt;rss&gt;</strong> tag.  Within that element is a <strong>&lt;channel&gt;</strong> element.</p>
<p>The <strong>&lt;channel&gt;</strong> element has a number of optional child elements.  The basic ones we&#8217;ll want to include are &#8211; <strong>&lt;title&gt;</strong>, <strong>&lt;description&gt;</strong>, <strong>&lt;link&gt;</strong>, and <strong>&lt;pubDate&gt;</strong>.</p>
<p>The actual data (our random quotes) is wrapped in <strong>&lt;item&gt;</strong> elements.  These also have a bunch of optional parameters.  We&#8217;ll be using the basics &#8211; <strong>&lttitle;&gt;</strong>, <strong>&lt;description&gt;</strong>, and <strong>&lt;pubDate&gt;</strong>.  Notice that there&#8217;s no link &#8211; because we&#8217;re feeding simple data, not articles!</p>
<h5>Creating the SimpleXML Object</h5>
<p>In order to create the SimpleXML object, you need to feed it some kind of XML string.  Normally this would be a completed file or string &#8211; like an existing feed.</p>
<p>Since we&#8217;re <strong>building</strong> the feed with SimpleXML, we don&#8217;t have a lot to start with.  We can give it a very basic string to create the object &#8211; an opening and closing <strong>rss</strong> element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;rss version=&quot;2.0&quot;&gt;&lt;/rss&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Adding Child Elements</h5>
<p>The <code>addChild()</code> method of SimpleXML allows us to add child elements to existing elements.  In addition to adding a child element, the method returns a reference to the new child &#8211; allowing us to manipulate it in turn.</p>
<p>The first child we&#8217;ll need to create is the <strong>channel</strong> element.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$channel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'channel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That creates the <strong>channel</strong> element and stores a reference to it in $channel.  We can then use the $channel variable to add more children &#8211; the information defining the <strong>channel</strong> element itself.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Test Feed'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'This is a feed testing how to build an RSS feed in SimpleXML'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You may want to include other optional parameters that are included in the specification.  Just add another $channel->addChild() call with the parameter&#8217;s name and its value.</p>
<h5>Creating the Random Quote Items</h5>
<p>At this point, we just need to add our <strong>item</strong> elements and we&#8217;re done.  To do that, we&#8217;ll loop through an array of data, create an <strong>item</strong> element, and add the appropriate children to it.</p>
<p>Let&#8217;s assume that I retrieved information from a database table with three columns &#8211; title, description, and pubDate.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$newItem</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Again, you can add extra data to your feed by adding extra child elements.</p>
<h5>Outputting the Feed</h5>
<p>Now that the entire feed is stored in the $xml variable, we just need to output it.</p>
<p>In doing this, we&#8217;ll do two things.  We need to define the content-type as xhtml/xml.  Then we use the asXML() method to print the information in an xml string.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/xhtml+xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You could alternatively save this to an xml file.  There are two ways to do this &#8211; either file_put_contents the output from asXML() or pass a filename as the parameter for asXML().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//  or ...</span>
<span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h5>Start Feeding</h5>
<p>With this basic script, you can serve up any kind of information as an RSS feed.  This makes it easy for people to read it in their RSS readers or even access the information on the cell phones.</p>
<p>There are plenty of types of data you could use this for &#8211; quotes, jokes, question of the day, trivia, tip of the day, etc.  By publishing tiny bits of data on a daily basis, you keep your users coming back for more &#8211; but you don&#8217;t need to publish full size articles every day.</p>
<p>In case you ran into trouble putting the script together, here&#8217;s the entire source code with an array of data built into it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Mark Twain on Profanity'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Under certain circumstances, profanity provides a relief denied even to prayer.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Woody Allen on People'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'It seemed the world was divided into good and bad people. The good ones slept better... while the bad ones seemed to enjoy the waking hours much more.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Will Rogers on Vets'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'The best doctor in the world is the veterinarian. He can\'t ask his patients what is the matter - he\'s got to just know.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Create the root element - RSS</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;rss version=&quot;2.0&quot;&gt;&lt;/rss&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Create the channel element, a child of RSS</span>
<span style="color: #000088;">$channel</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'channel'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Add random channel information</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Random Quotes'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.earn-web-cash.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'This is a feed of random, interesting quotes.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pubDate'</span><span style="color: #339933;">,</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D, d M Y H:i:s T&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Loop through and create item elements</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$items</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$newItem</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$newItem</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'description'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//  Output</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/xhtml+xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/21/create-rss-feed-simplexml/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Access XML Attributes in PHP Using SimpleXML</title>
		<link>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/</link>
		<comments>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/#comments</comments>
		<pubDate>Fri, 15 Feb 2008 19:29:49 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[HTML Tutorials]]></category>
		<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/</guid>
		<description><![CDATA[A user on Programming Talk had a problem the other day.  He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).

His parser wasn't working - because all of the information was stored in attributes instead of child elements.  What's the difference between the two?  And how can we fix it?

Use SimpleXML.]]></description>
			<content:encoded><![CDATA[<p>A user on Programming Talk had a problem the other day.  He was trying to build an XML parser to work with an xml data file (a character sheet from the WoW Armory).</p>
<p>His parser wasn&#8217;t working &#8211; because all of the information was stored in attributes instead of child elements.  What&#8217;s the difference between the two?  And how can we fix it?</p>
<p>Use SimpleXML.<br />
<span id="more-114"></span></p>
<h5>Difference Between a Child and an Attribute</h5>
<p>For starters, let&#8217;s take a look at a sample XML file.  This will help illustrate the difference between a child element and an attribute, and it will give us something to work with later on.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;library<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>John Steinback<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Of Mice and Men<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book</span> <span style="color: #000066;">lang</span>=<span style="color: #ff0000;">&quot;en&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>F. Scott Fitzgerald<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Great Gatsby<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>  
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/library<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This simple XML file is a &#8220;library.&#8221;  It contains a list of books &#8211; each of which has an author, a title, and a &#8220;lang&#8221; attribute.</p>
<p>In this example, &#8220;book&#8221; is an element.  It is a <strong>child</strong> element of library &#8211; which means it is an element created <strong>inside</strong> the open library element.  You could also refer to the library element as the <strong>parent</strong> of all the book elements.</p>
<p>To create a child element, you simply create a set of tags within a larger set of tags.  The book is a child of library, while the author is a child of book.  We could create more children for &#8220;Author&#8221; &#8211; like age, sex, and hair color.</p>
<p>By contrast, &#8220;lang&#8221; is an attribute.  It is defined inside the opening tag of an element.  By declaring <code>lang="en"</code> we are saying that the book has the attribute of being written in English.</p>
<p>It is simply another (perhaps more annoying) way to store information in an XML file.  Most basic XML parsers look for tags and content &#8211; but the attribute is neither.  It&#8217;s contained inside a tag, so it needs to be handled specially.  </p>
<p>Thankfully, SimpleXML is designed to help us do just that.</p>
<h5>Loading the File in SimpleXML</h5>
<p>I saved the xml file above in &#8220;xmltest.xml.&#8221;  In my php script, I can load it into a SimpleXML object with this function call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'xmltest.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This creates a SimpleXML object which is a tree of connected objects.  The root object represents the &#8220;library&#8221; element.  It then has an array of children called &#8220;book,&#8221; and each &#8220;book&#8221; element has two children element of its own.</p>
<p>Using the SimpleXML object structure, we can access some pieces of our XML document like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  All of the info in Book 1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">author</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// First book, John Steinbeck</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Second book, The Great Gatsby</span></pre></div></div>

<h5>Accessing the Lang Attribute in the Object Tree</h5>
<p>The initial question now resurfaces &#8211; how do we access an attribute, like the &#8216;lang&#8217; attribute?</p>
<p>All of the elements are nicely arranged in a tree.  The parent element has a child element, which has a child element, so on and so forth.  We can easily access these as related objects like we did above.</p>
<p>The attributes are exploded into an array and attached to the element that they define.  So, for example, we defined a &#8216;lang&#8217; attribute for our book element.  Therefore, the value of lang will reside in an array attached to that specific book element.</p>
<p>We can access it like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">book</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lang'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// en</span></pre></div></div>

<p>To find an attribute of an object with SimpleXML, you navigate to the element in question and then access an array indexed by the names of the attributes.  In this case, our attribute was named &#8216;lang&#8217; so we use &#8216;lang&#8217; as a key in the array to get the value.</p>
<p>For simplicity&#8217;s sake, it is usually better to define all of your XML data as child elements.  This allows you to easily expand data types in the future, and you won&#8217;t have to mix methods for accessing the data.</p>
<p>However, if you&#8217;re working with someone else&#8217;s XML file, you may need to look up attributes.  Now you know how.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/15/xml-attributes-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

