<?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>PHP Simple Tutorials &#187; simplexml</title>
	<atom:link href="http://php.elegosproject.org/tag/simplexml/feed/" rel="self" type="application/rss+xml" />
	<link>http://php.elegosproject.org</link>
	<description>Free and user-friendly PHP tutorials</description>
	<lastBuildDate>Sun, 19 Jul 2009 13:12:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Data storage: read an XML file</title>
		<link>http://php.elegosproject.org/2009/06/23/data-storage-read-an-xml-file/</link>
		<comments>http://php.elegosproject.org/2009/06/23/data-storage-read-an-xml-file/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 22:18:57 +0000</pubDate>
		<dc:creator>Giacomo</dc:creator>
				<category><![CDATA[Advanced]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://php.elegosproject.org/?p=83</guid>
		<description><![CDATA[More and more in the new concept of the web sites share data. The most common type of this data is shared via XML file documents. You&#8217;ll for sure know about RSS: they are xml files. What about sites like WowHead? They allow you to access all their database via XML. Well, let&#8217;s learn how [...]]]></description>
			<content:encoded><![CDATA[<p>More and more in the new concept of the web sites share data. The most common type of this data is shared via XML file documents. You&#8217;ll for sure know about RSS: they are xml files. What about sites like <a title="WowHead homepage" href="http://www.wowhead.com" target="_blank">WowHead</a>? They allow you to access all their database via XML. Well, let&#8217;s learn how to read an XML file!</p>
<p><span id="more-83"></span></p>
<p>First of all: XML has its own syntax. It has tags, just like HTML. The first line says which version of XML you&#8217;re reading and the encoding you&#8217;re using, like this:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</pre>
<p>This shouldn&#8217;t be that important for you but if you&#8217;re looking for a not common XML file.</p>
<p>The second line should begin the data. This is the example we&#8217;re going to use:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;articles&gt;
    &lt;article id=&quot;1&quot;&gt;
        &lt;title&gt;Article one&lt;/title&gt;
        &lt;author&gt;Myself&lt;/author&gt;
    &lt;/article&gt;
    &lt;article id=&quot;2&quot;&gt;
        &lt;title&gt;Article two&lt;/title&gt;
        &lt;author&gt;My friend&lt;/author&gt;
    &lt;/article&gt;
    &lt;article id=&quot;3&quot;&gt;
        &lt;title&gt;Article three&lt;/title&gt;
        &lt;author&gt;The friend of a friend&lt;/author&gt;
    &lt;/article&gt;
&lt;/articles&gt; </pre>
<p>As you can see there are categories, variables and so on. We&#8217;ll talk more about it in the tutorial about writing an XML file.</p>
<p>So, there are more then one classes in PHP on how to read an XML file. Recently PHP5 intrduced the SimpleXML class. It&#8217;s a powerful class and I learnt it very quickly.</p>
<p>The first thing to do is to create this new class entity, loading the external file: it can be loaded from a relative or absolute path or from an URL.</p>
<pre class="brush: php;">$xml = simplexml_load_file('articles.xml');</pre>
<p>Ok, now we have the file loaded in the system! You can call the variables in a very easy way, using the arrows ($xml->article[0]->title). Of course the &#8220;properties&#8221; in the &#8220;tags&#8221; can be accessed like an array ($xml->article[0]["id"]).</p>
<pre class="brush: php;">foreach($xml-&gt;article as $article)
{
    echo &quot;Article ID: &quot;.$article['id'].&quot;&lt;br /&gt;&quot;;
    echo &quot;Article title: &quot;.$article-&gt;title.&quot;&lt;br /&gt;&quot;;
    echo &quot;Article author: &quot;.$article-&gt;author.&quot;&lt;br /&gt;&quot;;
}</pre>
<p>I&#8217;ll talk more about the foreach cycle in another tutorial, but the basics are that foreach takes as arguments &#8220;$array as $new_variable&#8221;. The cycle ends when all the objects in the array have been processed. In this case $xml is an array with three &#8220;article&#8221; sub-variables. The cycle will take them and treat each of them as a stand-alone variable.</p>
<p>Easy, isn&#8217;t it?</p>
]]></content:encoded>
			<wfw:commentRss>http://php.elegosproject.org/2009/06/23/data-storage-read-an-xml-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
