<?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>Krio Media &#187; XML</title>
	<atom:link href="http://www.krio.me/category/development-info/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.krio.me</link>
	<description></description>
	<lastBuildDate>Wed, 01 Jun 2011 16:24:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>How to add data to an XML file in PHP</title>
		<link>http://www.krio.me/how-to-add-data-to-an-xml-file-in-php/</link>
		<comments>http://www.krio.me/how-to-add-data-to-an-xml-file-in-php/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 15:55:52 +0000</pubDate>
		<dc:creator>Kevin Rio</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.krio.me/?p=974</guid>
		<description><![CDATA[Manipulating XML files is not something all developers deal with on a daily basis, but it is something that must be done. This series of posts will each showcase the most popular XML manipulation techniques that a developer should know. In this entry you will learn how to add elements and text to an XML [...]]]></description>
			<content:encoded><![CDATA[<p>Manipulating XML files is not something all developers deal with on a daily basis, but it is something that must be done. This series of posts will each showcase the most popular XML manipulation techniques that a developer should know. In this entry you will learn how to add elements and text to an XML file from PHP.<span id="more-974"></span></p>
<h3>Our XML File</h3>
<p>Here is our XML file.</p>
<p>We are going to insert a new &lt;Link&gt; child element that holds sibling elements &lt;title&gt; and &lt;url&gt;.</p>
<pre class="brush: xml; title: ;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;MyLinksCentral&gt;
    &lt;Links&gt;
        &lt;Link&gt;
            &lt;title&gt;Krio Media&lt;/title&gt;
            &lt;url&gt;http://www.krio.me&lt;/url&gt;
        &lt;/Link&gt;
        &lt;Link&gt;
            &lt;title&gt;SEO Miami&lt;/title&gt;
            &lt;url&gt;http://www.mercadeoporinternet.com/&lt;/url&gt;
        &lt;/Link&gt;
    &lt;/Links&gt;
&lt;/MyLinksCentral&gt;
</pre>
<h3>How To Add The XML Elements</h3>
<p>And here is how we add the new elemnts to the document</p>
<pre class="brush: xml; title: ;">
  $xdoc = new DomDocument;
  $xdoc-&gt;Load('MyLinksCentral.xml');

        $links = $xdoc-&gt;getElementsByTagName('Links')-&gt;item(0);
        $newLinkElement = $xdoc -&gt;createElement('Link');
        $newTitleElement = $xdoc -&gt;createElement('title');
        $newURLElement = $xdoc -&gt;createElement('url');

        $urlNode = $xdoc -&gt;createTextNode ('Google');
        $titleNode = $xdoc -&gt;createTextNode ('http://www.google.com');

        $newTitleElement -&gt; appendChild($titleNode);
        $newURLElement -&gt; appendChild($urlNode);

        $newLinkElement -&gt; appendChild($newTitleElement);
        $newLinkElement -&gt; appendChild($newURLElement);

        $links -&gt; appendChild($newLinkElement);

$xdoc-&gt;save('MyLinksCentral.xml');
</pre>
<h3>Step-By-Step Analysis Of The XML</h3>
<p>Load &#8211; We load the XML file.</p>
<p>getElementsByTagName &#8211; We get the Links element, which holds all of our links.</p>
<p>createElement &#8211; We create the elements that we will be inserting into our XML file and save them into a variable.</p>
<p>createTextNode &#8211; We place the values of the elements into variables. In this case it is the URL and Title we want. We we will inserting Google as the Title and http://google.com as the URL.</p>
<p>appendChild &#8211; We put the text we just created into the actual elements &lt;title&gt; and &lt;url&gt;.</p>
<p>appendChild &#8211; The next set of appendChild&#8217;s are to place the complete &lt;title&gt; and &lt;url&gt; tags inside of our &lt;Link&gt; element.</p>
<p>The final appendChild is to insert the final &lt;Link&gt; tag into the &lt;Links&gt; element of the XML file to be saved.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.krio.me/how-to-add-data-to-an-xml-file-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

