<?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>JaffnaCampus.com &#187; PHP XML</title>
	<atom:link href="http://jaffnacampus.com/category/php/php-xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://jaffnacampus.com</link>
	<description>Gateway to your IT potential</description>
	<lastBuildDate>Tue, 27 Sep 2011 13:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP SimpleXML</title>
		<link>http://jaffnacampus.com/php/php-xml/php-simplexml/</link>
		<comments>http://jaffnacampus.com/php/php-xml/php-simplexml/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:43:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP XML]]></category>
		<category><![CDATA[PHP SimpleXML]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-xml/php-simplexml/</guid>
		<description><![CDATA[SimpleXML handles the most common XML tasks and leaves   the rest   for other extensions.

What is SimpleXML?
SimpleXML [...]]]></description>
			<content:encoded><![CDATA[<p>SimpleXML handles the most common XML tasks and leaves   the rest   for other extensions.</p>
<hr />
<h2>What is SimpleXML?</h2>
<p>SimpleXML is new in PHP 5. It is an easy way of getting an element&#8217;s   attributes and text, if you   know the XML document&#8217;s layout.</p>
<p>Compared to DOM or the Expat parser, SimpleXML just takes a few lines   of code   to read text data from an element. </p>
<p>SimpleXML converts the XML document into an object, like this:</p>
<ul>
<li>Elements &#8211; Are converted to single attributes of the   SimpleXMLElement   	object. When there&#8217;s more than one element on one level, they&#8217;re   	placed inside an array</li>
<li>Attributes &#8211; Are accessed using associative arrays, where an index   	corresponds to the attribute name</li>
<li>Element Data &#8211; Text data from elements are converted to strings. If   an   	element has more than one text node, they will be arranged in the order   they   	are found</li>
</ul>
<p>SimpleXML is fast and easy to use when performing basic tasks like:</p>
<ul>
<li>Reading XML files</li>
<li>Extracting data from XML   strings</li>
<li>Editing text nodes or attributes</li>
</ul>
<p>However, when dealing with advanced XML, like namespaces, you are   better off   using the Expat parser or the XML DOM.</p>
<hr />
<h2>Installation</h2>
<p>As of PHP 5.0, the SimpleXML functions are part of the PHP core.   There is no installation   needed to use these functions.</p>
<hr />
<h2>Using SimpleXML</h2>
<p>Below is an XML file:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;note&gt;<br />
        &lt;to&gt;Tove&lt;/to&gt;<br />
        &lt;from&gt;Jani&lt;/from&gt;<br />
        &lt;heading&gt;Reminder&lt;/heading&gt;<br />
        &lt;body&gt;Don&#8217;t forget me this weekend!&lt;/body&gt;<br />
        &lt;/note&gt; </td>
</tr>
</tbody>
</table>
<p>We want to output the element names and data from the XML file above.</p>
<p>Here&#8217;s what to do:</p>
<ol>
<li>Load the XML file</li>
<li>Get the name of the first element</li>
<li>Create a loop that will trigger on each child node, using the   children()   	function</li>
<li>Output the element name and data for each child node</li>
</ol>
<p><strong>Example</strong></p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $xml = simplexml_load_file(&quot;test.xml&quot;);</p>
<p>        echo $xml-&gt;getName() . &quot;&lt;br /&gt;&quot;;</p>
<p>        foreach($xml-&gt;children() as $child)<br />
        {<br />
        echo $child-&gt;getName() . &quot;: &quot; . $child . &quot;&lt;br /&gt;&quot;;<br />
        }<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> note<br />
        to: Tove<br />
        from: Jani<br />
        heading: Reminder<br />
        body: Don&#8217;t forget me this weekend! </td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-xml/php-simplexml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP XML DOM</title>
		<link>http://jaffnacampus.com/php/php-xml/php-xml-dom/</link>
		<comments>http://jaffnacampus.com/php/php-xml/php-xml-dom/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:42:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP XML]]></category>
		<category><![CDATA[PHP XML DOM]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=360</guid>
		<description><![CDATA[The built-in DOM parser makes it possible to process   XML   documents in PHP.

What is DOM?
The W3C [...]]]></description>
			<content:encoded><![CDATA[<p>The built-in DOM parser makes it possible to process   XML   documents in PHP.</p>
<hr />
<h2>What is DOM?</h2>
<p>The W3C DOM provides a standard set of objects for HTML and XML   documents,   and a standard interface for accessing and manipulating them.</p>
<p>  The W3C DOM is separated into different parts (Core, XML, and HTML) and   different levels (DOM Level 1/2/3):</p>
<p>  * Core DOM &#8211; defines a standard set of objects for any structured   document<br />
  * XML DOM &#8211; defines a standard set of objects for XML documents<br />
  * HTML DOM &#8211; defines a standard set of objects for HTML documents
</p>
<hr />
<h2>XML Parsing</h2>
<p>To read and update &#8211; create and manipulate &#8211; an XML document, you   will need   an XML parser.</p>
<p>There are two basic types of XML parsers:</p>
<ul>
<li>Tree-based parser: This parser transforms an XML document into a   tree   	structure. It analyzes the whole document, and provides access to the   tree   	elements</li>
<li>Event-based parser: Views an XML document as a series of events.   When a   	specific event occurs, it calls a function to handle it</li>
</ul>
<p>The DOM parser is an   tree-based parser.</p>
<p>Look at the following XML document fraction:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;from&gt;Jani&lt;/from&gt; </td>
</tr>
</tbody>
</table>
<p>The XML DOM sees the XML above as a tree structure: </p>
<ul>
<li>Level 1: XML Document</li>
<li>Level 2: Root element: &lt;from&gt;</li>
<li>Level 3: Text element: &quot;Jani&quot;</li>
</ul>
<hr />
<h2>Installation</h2>
<p>The DOM XML parser functions are part of the PHP core. There is no   installation needed to use these functions.</p>
<hr />
<h2>An XML File</h2>
<p>The XML file below will be used in our example:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;note&gt;<br />
        &lt;to&gt;Tove&lt;/to&gt;<br />
        &lt;from&gt;Jani&lt;/from&gt;<br />
        &lt;heading&gt;Reminder&lt;/heading&gt;<br />
        &lt;body&gt;Don&#8217;t forget me this weekend!&lt;/body&gt;<br />
        &lt;/note&gt; </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Load and Output XML</h2>
<p>We want to initialize the XML parser, load the xml, and output it:</p>
<h3><strong>Example</strong></h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $xmlDoc = new DOMDocument();<br />
        $xmlDoc-&gt;load(&quot;note.xml&quot;);</p>
<p>        print $xmlDoc-&gt;saveXML();<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> Tove Jani Reminder Don&#8217;t forget   me this weekend! </td>
</tr>
</tbody>
</table>
<p>If you select &quot;View source&quot; in the browser window, you will see the   following   HTML:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;note&gt;<br />
        &lt;to&gt;Tove&lt;/to&gt;<br />
        &lt;from&gt;Jani&lt;/from&gt;<br />
        &lt;heading&gt;Reminder&lt;/heading&gt;<br />
        &lt;body&gt;Don&#8217;t forget me this weekend!&lt;/body&gt;<br />
        &lt;/note&gt; </td>
</tr>
</tbody>
</table>
<p>The example above creates a DOMDocument-Object and loads the XML from   &quot;note.xml&quot;   into it.</p>
<p>Then the saveXML() function puts the internal XML document into a   string,   so we can output it. </p>
<hr />
<h2>Looping through XML</h2>
<p>We want to initialize the XML parser, load the XML, and loop through   all elements   of the &lt;note&gt; element:</p>
<h3><strong>Example</strong></h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $xmlDoc = new DOMDocument();<br />
        $xmlDoc-&gt;load(&quot;note.xml&quot;);</p>
<p>        $x = $xmlDoc-&gt;documentElement;<br />
        foreach ($x-&gt;childNodes AS $item)<br />
        {<br />
        print $item-&gt;nodeName . &quot; = &quot; . $item-&gt;nodeValue . &quot;&lt;br   /&gt;&quot;;<br />
        }<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> #text = <br />
        to = Tove<br />
        #text = <br />
        from = Jani<br />
        #text = <br />
        heading = Reminder<br />
        #text = <br />
        body = Don&#8217;t forget me this weekend!<br />
        #text = </td>
</tr>
</tbody>
</table>
<p>In the example above you see that there are empty text nodes between   each   element.</p>
<p>When XML generates, it often contains white-spaces between the nodes.   The XML   DOM parser treats these as ordinary elements, and if you are not aware   of them,   they sometimes cause problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-xml/php-xml-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP XML Expat Parser</title>
		<link>http://jaffnacampus.com/php/php-xml/php-xml-expat-parser/</link>
		<comments>http://jaffnacampus.com/php/php-xml/php-xml-expat-parser/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:40:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP XML]]></category>
		<category><![CDATA[PHP XML Expat Parser]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=358</guid>
		<description><![CDATA[The built-in Expat parser makes it possible to process   XML   documents in PHP.

What is XML?
XML is [...]]]></description>
			<content:encoded><![CDATA[<p>The built-in Expat parser makes it possible to process   XML   documents in PHP.</p>
<hr />
<h2>What is XML?</h2>
<p>XML is used to describe data and to focus on what data is. An XML   file describes   the structure of the data.</p>
<p>In XML, no tags are predefined. You must define your own tags.</p>
<p>If you want to learn more about XML, please visit our XML tutorial.</p>
<hr />
<h2>What is Expat?</h2>
<p>To read and update &#8211; create and manipulate &#8211; an XML document, you   will need   an XML parser.</p>
<p>There are two basic types of XML parsers:</p>
<ul>
<li>Tree-based parser: This parser transforms an XML document into a   tree   	structure. It analyzes the whole document, and provides access to the   tree   	elements. e.g. the Document Object Model (DOM)</li>
<li>Event-based parser: Views an XML document as a series of events.   When a   	specific event occurs, it calls a function to handle it</li>
</ul>
<p>The Expat parser is an   event-based parser.</p>
<p>Event-based parsers focus on the content of the XML documents, not   their structure. Because of this, event-based parsers can access data   faster than   tree-based parsers.</p>
<p>Look at the following XML fraction:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;from&gt;Jani&lt;/from&gt; </td>
</tr>
</tbody>
</table>
<p>An event-based parser reports the XML above as a series of three   events: </p>
<ul>
<li>Start element: from</li>
<li>Start CDATA section, value: Jani</li>
<li>Close element: from</li>
</ul>
<p>The XML example above contains well-formed XML. However, the example   is not valid   XML, because there is no Document Type Definition (DTD) associated with   it.</p>
<p>However, this makes no difference when using the Expat parser. Expat   is a non-validating parser, and ignores any DTDs.</p>
<p>As an event-based, non-validating XML parser, Expat is fast and   small, and a   perfect match for PHP web applications.</p>
<p><strong>Note:</strong> XML documents    must be well-formed or Expat will generate an error.</p>
<hr />
<h2>Installation</h2>
<p>The XML Expat parser functions are part of the PHP core. There is no   installation needed to use these functions.</p>
<hr />
<h2>An XML File</h2>
<p>The XML file below will be used in our example:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;note&gt;<br />
        &lt;to&gt;Tove&lt;/to&gt;<br />
        &lt;from&gt;Jani&lt;/from&gt;<br />
        &lt;heading&gt;Reminder&lt;/heading&gt;<br />
        &lt;body&gt;Don&#8217;t forget me this weekend!&lt;/body&gt;<br />
        &lt;/note&gt; </td>
</tr>
</tbody>
</table>
<p></p>
<hr />
<h2>Initializing the XML Parser</h2>
<p>We want to initialize the XML parser in PHP, define some handlers for   different   XML events, and then parse the XML file.</p>
<h3><strong>Example</strong></h3>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        //Initialize the XML parser<br />
        $parser=xml_parser_create();</p>
<p>        //Function to use at the start of an element<br />
        function start($parser,$element_name,$element_attrs)<br />
        {<br />
        switch($element_name)<br />
        {<br />
        case &quot;NOTE&quot;:<br />
        echo &quot;&#8211; Note &#8211;&lt;br /&gt;&quot;;<br />
        break;<br />
        case &quot;TO&quot;:<br />
        echo &quot;To: &quot;;<br />
        break;<br />
        case &quot;FROM&quot;:<br />
        echo &quot;From: &quot;;<br />
        break;<br />
        case &quot;HEADING&quot;:<br />
        echo &quot;Heading: &quot;;<br />
        break;<br />
        case &quot;BODY&quot;:<br />
        echo &quot;Message: &quot;;<br />
        }<br />
        }</p>
<p>        //Function to use at the end of an element<br />
        function stop($parser,$element_name)<br />
        {<br />
        echo &quot;&lt;br /&gt;&quot;;<br />
        }</p>
<p>        //Function to use when finding character data<br />
        function char($parser,$data)<br />
        {<br />
        echo $data;<br />
        }</p>
<p>        //Specify element handler<br />
        xml_set_element_handler($parser,&quot;start&quot;,&quot;stop&quot;);</p>
<p>        //Specify data handler<br />
        xml_set_character_data_handler($parser,&quot;char&quot;);</p>
<p>        //Open XML file<br />
        $fp=fopen(&quot;test.xml&quot;,&quot;r&quot;);</p>
<p>        //Read data<br />
        while ($data=fread($fp,4096))<br />
        {<br />
        xml_parse($parser,$data,feof($fp)) or <br />
        die (sprintf(&quot;XML Error: %s at line %d&quot;, <br />
        xml_error_string(xml_get_error_code($parser)),<br />
        xml_get_current_line_number($parser)));<br />
        }</p>
<p>        //Free the XML parser<br />
        xml_parser_free($parser);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>The output of the code above will be:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> &#8212; Note &#8211;<br />
        To: Tove<br />
        From: Jani<br />
        Heading: Reminder<br />
        Message: Don&#8217;t forget me this weekend! </td>
</tr>
</tbody>
</table>
<p>How it works:</p>
<ol>
<li>Initialize the XML parser with the xml_parser_create() function</li>
<li>Create functions to use with the different event handlers</li>
<li>Add the xml_set_element_handler() function to specify which   function   	will be executed when the parser encounters the opening and closing   tags</li>
<li>Add the xml_set_character_data_handler() function to specify which   	function will execute when the parser encounters character data</li>
<li>Parse the file &quot;test.xml&quot; with the xml_parse() function</li>
<li>In case of an error, add  xml_error_string() function to convert an     	XML error to a textual description</li>
<li>Call the xml_parser_free() function to release the memory allocated   with the xml_parser_create()   	function</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-xml/php-xml-expat-parser/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

