<?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>Will Fitch&#039;s Blog &#187; xml</title>
	<atom:link href="http://www.willfitch.com/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>What is a Web Service?</title>
		<link>http://www.willfitch.com/what-is-a-web-service.html</link>
		<comments>http://www.willfitch.com/what-is-a-web-service.html#comments</comments>
		<pubDate>Tue, 29 Apr 2008 20:01:26 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[soap]]></category>
		<category><![CDATA[web service]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=53</guid>
		<description><![CDATA[A web service is a way for systems to communicate over a network. They can be as simple as an API (e.g. Google Maps) or as complex as a brokered system that handles transactions for multiple requesters and/or providers. Web services bring with them some huge advantages. Data is packaged via HTTP or HTTPS and [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/images/WS-Standards-2007-02-medium.jpg" style="padding:2px;" width="200" height="142" alt="Web Services" align="left" border="0" />A web service is a way for systems to communicate over a network.  They can be as simple as an API (e.g. Google Maps) or as complex as a brokered system that handles transactions for multiple requesters and/or providers.</p>
<p>Web services bring with them some huge advantages.  Data is packaged via HTTP or HTTPS and can usually bypass firewalls since port 80 and 443 are commonly allowed open.  This plays a big role where intranet systems need to communicate but have a very strict set of firewall rules.  An example might be an Oracle DB link needing to reach a MySQL server.  Typically this is a daunting task as you have to go through the bureaucracy of getting the proper ports opened, get added to one or more access-controlled lists, receive/provide access privileges at the database level, etc.  Then there is the issue of direct access to data, either by view, table or procedure;  the list goes on and on.</p>
<p>Most of these problems can be eliminated by the use of web services.  This article&#8217;s objective is to explain, at a high-level, when, why and how web services are used.</p>
<p><span id="more-249"></span></p>
<h3>Table of Contents</h3>
<ol>
<li><a href="#wso">Web Service Overview</a></li>
<li><a href="#adc">Authoritative Data Control</a></li>
<li><a href="#mdaws">Multi-Data Access Web Services</a></li>
<li><a href="#bpws">Business Platform Web Services</a></li>
<li><a href="#wstct">Web Service Technologies and Common Terms</a></li>
</ol>
<p><strong><u><a name="wso">Web Service Overview</a></u></strong></p>
<p>As stated above, web services can be as simple as an API call similar to <a href="http://code.google.com/apis/maps/">Google Maps</a> where a client requests an action be performed and data sent/received.  You may have a system that displays a location based on a zip code related to a row in the database, and you want to display that precise location on a map to the end user.  You would send a request to the provider, Google in this case, and it would return a map based on that criteria.  This is a very common form of web services: a client requests data from a provider and the provider returns data based on the client&#8217;s request.</p>
<p><strong><u><a name="adc">Authoritative Data Control</a></u></strong></p>
<p>A not-so-common form of web services is authoritative data control.  This is when two or more systems have a unique set of data, but share a common subset of data elements.</p>
<p>Take the automotive industry, for example, and mix in auto insurance and the DMV (Department of Motor Vehicles).  All three sources might share a car&#8217;s VIN (Vehicle Identification Number), make, model, license plate number, and insurance policy number.  Today, I&#8217;ll bet there are few, if any, systems setup to share this type of authoritative data.  Instead, you are forced to give this information to each source.  Let&#8217;s analyze.</p>
<p>When you pull into a car lot, the following data elements are known prior to even selecting your car:</p>
<ol>
<li>VIN</li>
<li>Make</li>
<li>Model</li>
</ol>
<p>Identifying the origin of data is extremely important.  This will define the business logic behind identifying authoritative data sources.  We know the VIN is unique to every car on the road, so it will act as our &#8220;primary&#8221; or &#8220;unique&#8221; key.  All sources that need information relating to a single vehicle can use this to request and send data.  The make and model are absolutely not unique, so they will be accompanied by a VIN number.  If you are familiar with databases, you should be seeing a pattern here.</p>
<p>Now you have purchased the car of choice and ready to get on the road!  Wait!  You need a temporary license tag!  This information will make your car legal on the road for a short period of time.  In order to get this, however, you will need insurance!  You call up your insurance agent or company, and inform them of the new purchase.  They ask you for the automobiles VIN, and make a request to the dealer/manufacturer and get the information relating to the car.  They provide you with your policy number, which you hand off to the dealer.  They register your car with the DMV by providing the VIN and insurance policy number.  Yay!  You can drive off the lot.</p>
<p>Now that you&#8217;ve had the car for a few days, you need to get a permanent license plate.  Why not visit the DMV&#8217;s website and order it?  You visit the website, enter your VIN number and pay for your plate.  The DMV then pulls the information, grabs a license plate, sends the updated registration information to the dealer and insurance and sends your license plate.  Isn&#8217;t it a perfect world!!  Well, not exactly.</p>
<p>The difficult part about web services is not the technology;  it&#8217;s the business logic.  The DMV may think it should house all of the data and provide it to the insurance and dealer while the dealer thinks it should own it because it&#8217;s the originating source.  It takes dedication and sacrifice by those who make business decisions in order for scenarios like this to happen.</p>
<p><strong><u><a name="mdaws">Multi-Data Access Web Services</a></u></strong></p>
<p>This is another common scenario that is rarely addressed with web services:  a single data source that multiple systems access.  This situation is a no-brainer if you are building a new system, but can be hell if you are converting existing ones.</p>
<p>Let&#8217;s say you have a system that has a massive PostgreSQL database with views, procedures and tables and has a relatively old or unstable platform.  You have five systems that all use their own views and procedures to access and manipulate the data, and they are all mature sites.</p>
<p>This is a very hard situation to convert to web services, but it has its benefits.  What if you&#8217;re ready to (or have to) convert this system to MySQL?  You now have five codebases to change, views and stored procedures to reverse-engineer and months of development on your hands.</p>
<p>Now, imagine if you had a single source where a series of web services provide the data and same functionality as the views and procedures from the database.  All five systems manipulate and read the data from those web services.  You have only one place to change the code once the database is converted over to MySQL.  All of those views and procedures probably wouldn&#8217;t be necessary if these services provided that functionality.</p>
<p><strong><u><a name="bpws">Business Platform Web Services</a></u></strong></p>
<p>A Business Platform Web Service (that&#8217;s what I call it) is when most or all aspects of a business are driven by web services.  This is a very complex scenario and requires a mature set of programs.  You should have fail-overs in place for this, but if a single program fails, panic would arise in the programmer&#8217;s den.</p>
<p>In order to illustrate this, I will give you a fictitious company and scenario:</p>
<p><strong>Example Scenario</strong></p>
<p>A tire company called &#8220;ABC Tires&#8221; is located in the state of Arizona, and resells its tires to 200 stores in Tennessee, Alabama, Florida and Texas.  This tire company provides a combined average of 500,000 tires per month to all of its locations.  The tire factory has enough employees to produce that amount and no more.  It is crucial that each of these resellers not get backlogged on orders or tires will not be delivered on time and business will be lost.</p>
<p>To make things a little more complicated, tires are only shipped out in bulks of 5,000, and this tire company uses 4 different freight companies to deliver its tires.  Each of these freights attend to other clients, so they need as much notification as possible for pickup.</p>
<p>Take not of the diagram below:</p>
<p><center><img src="/images/tire_reseller.jpg" border="1" style="padding:10px;" /></center></p>
<p><strong>Let&#8217;s run through the scenario:</strong></p>
<ol>
<li>Reseller makes tire sale.  Point-of-sale system has a web service hook and sends the request to the provider.</li>
<li>Provider tracks sale and sends request for tire to be made.</li>
<li>Plant creates tire and sends it to the warehouse.  It then informs the provider that the tire has been created/replaced.</li>
<li>Provider checks the database for the total tires in the warehouse.  If the count is getting close to 5,000, it sends a request to freight providers until a confirmed shipment is setup.  The freight returns the pickup time back to the provider.</li>
<li>Provider informs reseller(s) of shipment.  The reseller can then hold tires for potential customers and can provide a date for installation/purchase.</li>
<li>Freight delivers tires to reseller(s).</li>
</ol>
<p>The are many advantages to this setup: single data source, automated work flow and no firewall rules to worry about.  Setting up a system like this requires time, planning and dedication on the part of the company implementing it.</p>
<p>There are other examples of more advanced web service systems where UDDIs (Universal Description, Discovery and Integration), an XML-based registry for businesses worldwide to list themselves, are involved but that is beyond the scope of this article.  Let&#8217;s take a look at some technologies associated with web services.</p>
<p><strong><u><a name="wstct">Web Service Technologies and Common Terms</a></u></strong></p>
<p>There are many technologies associated with web services, and I will attempt to touch some of them.  I will not go in-depth on any single technology as I will reserve a future article for them.</p>
<p><strong>Specifications<sup><a href="#wikipedia_footnote">1</a></sup></strong></p>
<ul>
<li>WS-Security &#8211; Defines how to use XML Encryption and XML Signature in SOAP to secure message exchanges, as an alternative or extension to using HTTPS to secure the channel.</li>
<li>WS-Reliability &#8211; An OASIS standard protocol for reliable messaging between two Web services.</li>
<li>WS-ReliableMessaging &#8211; A protocol for reliable messaging between two Web services, issued by Microsoft, BEA and IBM it is currently being standardized by the OASIS organization.</li>
<li>WS-Addressing &#8211; A way of describing the address of the recipient (and sender) of a message, inside the SOAP message itself.</li>
<li>WS-Transaction &#8211; A way of handling transactions</li>
</ul>
<p><strong>Styles of Use<sup><a href="#wikipedia_footnote">1</a></sup></strong></p>
<p><strong>Remote procedure calls</strong> &#8211; RPC Web services present a distributed function (or method) call interface that is familiar to many developers. Typically, the basic unit of RPC Web services is the WSDL operation.</p>
<p>The first Web services tools were focused on RPC, and as a result this style is widely deployed and supported. However, it is sometimes criticised for not being loosely coupled, because it was often implemented by mapping services directly to language-specific functions or method calls. Many vendors felt this approach to be a dead end, and pushed for RPC to be disallowed in the WS-I Basic Profile.</p>
<p><strong>Service-oriented architecture</strong> &#8211; Web services can also be used to implement an architecture according to Service-oriented architecture (SOA) concepts, where the basic unit of communication is a message, rather than an operation. This is often referred to as &#8220;message-oriented&#8221; services.</p>
<p>SOA Web services are supported by most major software vendors and industry analysts. Unlike RPC Web services, loose coupling is more likely, because the focus is on the &#8220;contract&#8221; that WSDL provides, rather than the underlying implementation details.</p>
<p><strong>Representational state transfer</strong> &#8211; Finally, RESTful Web services attempt to emulate HTTP and similar protocols by constraining the interface to a set of well-known, standard operations (e.g., GET, PUT, DELETE). Here, the focus is on interacting with stateful resources, rather than messages or operations. RESTful Web services can use WSDL to describe SOAP messaging over HTTP, which defines the operations, or can be implemented as an abstraction purely on top of SOAP (e.g., WS-Transfer).</p>
<p>WSDL version 2.0 offers support for binding to all the HTTP request methods (not only GET and POST as in version 1.1) so it enables a better implementation of RESTful Web services[1]. However support for this specification is still poor in software development kits, which often offer tools only for WSDL 1.1.</p>
<p><sup>1</sup> &#8211; <i><a name="wikipedia_footnote">http://en.wikipedia.org/wiki/Web_service</a></i></p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/what-is-a-web-service.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple XML (SimpleXML) Tutorial Part 1</title>
		<link>http://www.willfitch.com/simplexml-tutorial-part1.html</link>
		<comments>http://www.willfitch.com/simplexml-tutorial-part1.html#comments</comments>
		<pubDate>Wed, 23 Apr 2008 21:40:28 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Services]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[simplexml]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xmlreader]]></category>
		<category><![CDATA[xmlwriter]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/simple-xml-simplexml-tutorial.html</guid>
		<description><![CDATA[With the increasing use of web services and communications via HTTP, XML has become an industry standard for sending and receiving data among systems. Every language seems to have multiple ways to parse XML, each more complicated than the other. This presents a problem for developers. The majority of the time, developers need to read/write [...]]]></description>
			<content:encoded><![CDATA[<p>With the increasing use of web services and communications via HTTP, XML has become an industry standard for sending and receiving data among systems.  Every language seems to have multiple ways to parse XML, each more complicated than the other.  This presents a problem for developers.</p>
<p>The majority of the time, developers need to read/write XML files without complex features such as advanced namespacing and XSD validation.  It seems that opening an XML file, reading and using the content takes forever as you constantly need to reference hundreds of pages of documentation, Google examples of each method or function in the documentation;  it never ends.</p>
<p>Since the release of PHP5, SimpleXML has been available for creating and editing XML files or strings.  Don&#8217;t let the name fool you, it doesn&#8217;t represent the simplicity of its capabilities.  It does, however, mean the implementation of this extension is simple!  PHP has other XML capabilities such as DOM, XMLWriter and XMLReader in the event you need those advanced features not offered by the native SimpleXML extension.</p>
<p><span id="more-246"></span></p>
<p><strong><u>Configuring PHP to Use SimpleXML</u></strong></p>
<p><a href="http://us3.php.net/manual/en/book.simplexml.php">SimpleXML</a> is an extension that was added as of PHP 5 and requires no special libraries.  If for some reason you wish not to have SimpleXML available to your <a href="http://www.php.net/">PHP</a> configuration, you can disable it at compile-time using <code>--disable-simplexml</code>.  The primary class you will work with is called SimpleXMLElement.</p>
<p><strong><u>There are Four Ways to Get an Instance of SimpleXMLElement:</u></strong></p>
<ol>
<li><strong>simplexml_load_file</strong> &#8211; Load an XML document from a file.  You may also access a remote file (HTTP for example).</li>
<li><strong>simplexml_load_string</strong> &#8211; Load and XML string.  Please keep in mind the XML needs to be well-formed.</li>
<li><strong>SimpleXMLElement </strong>- Directly instantiate the SimpleXMLElement class.  This requires a parameter starting the XML document.  If you directly instantiate this class, you are typically using SimpleXML for XML output.</li>
<li><strong>simplexml_import_dom</strong> &#8211; Returns and instance of SimpleXMLElement from a DOM object.  That&#8217;s right, you can bring over that advanced object and make life simple!</li>
</ol>
<p><strong><u>Sample XML File Used</u></strong></p>
<p>The following XML data will be contained in the referenced &#8220;test_file.xml&#8221;:</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;ISO-8859-1&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cars<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;make</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Ford&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;model<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Mustang<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/model<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/make<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;make</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Honda&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;model<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Accord<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/model<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/make<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cars<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong><u>Loading XML Data Using simplexml_load_file</u></strong></p>
<p>This function is typically used for loading XML data either from a file, or a remote call.  In this example, we will load a flat XML file:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Location of the XML file on the file system</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test_file.xml'</span><span style="color: #339933;">;</span>
<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: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>That&#8217;s it.  No need to clean anything up, PHP will take care of that for you.</p>
<p><strong><u>Loading XML Using simplexml_load_string</u></strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// XML String</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;cars&gt;
	&lt;make name=&quot;Ford&quot;&gt;
		&lt;model&gt;Mustang&lt;/model&gt;
	&lt;/make&gt;
	&lt;make name=&quot;Honda&quot;&gt;
		&lt;model&gt;Accord&lt;/model&gt;
	&lt;/make&gt;
&lt;/cars&gt;
'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Pretty easy, huh?  This function loads an XML string and returns and instance of SimpleXMLElement.  You could also directly instantiate the SimpleXMLElement class a similar way, as demonstrated below.</p>
<p><strong><u>Loading XML Using the SimpleXMLElement Class</u></strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;cars&gt;
	&lt;make name=&quot;Ford&quot;&gt;
		&lt;model&gt;Mustang&lt;/model&gt;
	&lt;/make&gt;
	&lt;make name=&quot;Honda&quot;&gt;
		&lt;model&gt;Accord&lt;/model&gt;
	&lt;/make&gt;
&lt;/cars&gt;
'</span><span style="color: #339933;">;</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: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>It&#8217;s your choice on using this method, or simplexml_load_string.</p>
<p><strong><u>Loading XML Data Using simplexml_import_dom</u></strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;cars&gt;
	&lt;make name=&quot;Ford&quot;&gt;
		&lt;model&gt;Mustang&lt;/model&gt;
	&lt;/make&gt;
	&lt;make name=&quot;Honda&quot;&gt;
		&lt;model&gt;Accord&lt;/model&gt;
	&lt;/make&gt;
&lt;/cars&gt;
'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loadXML</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_import_dom</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dom</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>This is handy when you are working with the advanced XML features of DOM.  When you&#8217;re ready to move from those precocious features, just load the DOM object into simplexml_import_dom, and start programming the easy way!</p>
<p><strong><u>Loading XML Data Using simplexml_load_file</u></strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test_file.xml'</span><span style="color: #339933;">;</span>
<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: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
The $xml object now has the top, parent node &lt;cars&gt;
All nodes are referenced as properties, and node attributes are within an associative array in each of those properties.
If there are multiple nodes with the same name (&lt;make&gt; in this example), you will need to specify the index value to reference.  This is done as an array index.  The following code refences the &quot;name&quot; attribute in the first &lt;make&gt; node
*/</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">make</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;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Prints out &quot;Ford&quot; (without quotes)</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The above example print out &#8220;Ford&#8221;.  We referenced the first <make> node, and then printed out the &#8220;name&#8221; attribute.  Now, let&#8217;s print out &#8220;Mustang&#8221; from the <model> node.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test_file.xml'</span><span style="color: #339933;">;</span>
<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: #000088;">$file</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;">make</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;">model</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Prints out &quot;Mustang&quot; (without quotes)</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Reading XML doesn&#8217;t get any easier.  The object-oriented design provided by SimpleXML makes code very elegant and maintainable.</p>
<p>The above examples assume you know exactly which node, down to the index, that contains the data you need to access.  In the real world, this is an unlikely scenario.  Normally, a developer knows the node names they are working with, but do not know (nor care) as to the number of those nodes.</p>
<p>Just like with normal arrays, we can count the number of elements.  We will do this prior to iterating over the <make> node.  Let&#8217;s iterate!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'
&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;
&lt;cars&gt;
	&lt;make name=&quot;Ford&quot;&gt;
		&lt;model&gt;Mustang&lt;/model&gt;
	&lt;/make&gt;
	&lt;make name=&quot;Honda&quot;&gt;
		&lt;model&gt;Accord&lt;/model&gt;
	&lt;/make&gt;
&lt;/cars&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">make</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">make</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// This prints out each of the models</span>
    	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">model</span><span style="color: #339933;">.</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong><u>Important Information Regarding Values Returned from SimpleXML</u></strong></p>
<p>If you have debugged any of these examples, you would have noticed that everything, nodes and attributes, are an instance of SimpleXMLElement.  This may or may not be a feature in your opinion, but it&#8217;s the nature of SimpleXML.  If you plan on storing any of these node/attribute values, you will need to typecast them:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'test_file.xml'</span><span style="color: #339933;">;</span>
<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: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$value_to_store</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">make</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;">model</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// This converts the &quot;Mustang&quot; SimpleXMLElement object to a string, making it disk storable.</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You should certainly keep this in mind as you are working with this extension.</p>
<p><strong><u>Writing XML with SimpleXML</u></strong></p>
<p>Writing or producing XML is just as easy as reading it.  While you can add additional data to existing XML files, the examples in this section will create fresh XML data.  In part two, we will discuss more advanced options such as DOM, CDATA and namespaces.  Now, on to writing XML data.</p>
<p>You will quickly learn that SimpleXML expects <strong>well-formed XML</strong>.  There are two PHP functions you should familiarize with if you are not already:</p>
<ol>
<li><strong><a href="http://www.php.net/utf8_encode">utf8_encode</a></strong> &#8211; encode a string with UTF-8 standards.</li>
<li><strong><a href="http://www.php.net/htmlspecialchars">htmlspecialchars</a></strong> &#8211; encode HTML characters with their proper entities.
</ol>
<p>You will need to use htmlspecialchars followed by utf8_encode to ensure proper data encoding.  The following example illustrates using this technique to ensure proper XML output:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</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;cars&gt;&lt;/cars&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</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;">'make'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ford'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span><span style="color: #339933;">,</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$some_db_variable</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// This will output the XML</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In order to create a new instance of SimpleXMLElement for writing, you will need to specify the top  parent node.  No need for the XML declaration, SimpleXML will add this for you.  In the above example, we ensured that data coming from a database variable had non-UTF8 encoded characters encoded, and HTML entities properly converted.  We also introduced a few additional methods.  Let me explain:</p>
<p>The <a href="http://us2.php.net/manual/en/function.simplexml-element-addChild.php">addChild</a> method adds a new child to an existing node.  This takes an optional second parameter of the value to place inside that node.</p>
<p>Each time you create a new child, a reference to the SimpleXMLElement is returned for use.  Here, you can add additional child nodes, attributes and more.  The initial SimpleXML object ($xml in the above example) contains the primary instance.  You will use this object to output the data.</p>
<p>The <a href="http://us2.php.net/manual/en/function.simplexml-element-addAttribute.php">addAttribute</a> method adds an attribute to a node.  If you aren&#8217;t sure of the data being added to the attribute (and just to be safe), you should also encode that value.</p>
<p>The <a href="http://us2.php.net/manual/en/function.simplexml-element-asXML.php">asXML</a> method saves/outputs the XML to the proper buffer.  If you specify a file name in this method, the XML will be written to it.  Here is an example:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</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;cars&gt;&lt;/cars&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</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;">'make'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'Ford'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$make</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'model'</span><span style="color: #339933;">,</span><span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$some_db_variable</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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: #0000ff;">'test_file.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// This will save the XML to a file called test_file.xml</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong><u>Conclusion</u></strong></p>
<p>Through examples and definitions, you have familiarized yourself with the basics of SimpleXML.  In part two (which is written yet), I will explain more advanced features of SimpleXML and DOM.</p>
<p>If you have any questions, feel free to post comments.</p>
<p>Will</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/simplexml-tutorial-part1.html/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

