<?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>code-diesel &#187; xml</title>
	<atom:link href="http://www.codediesel.com/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Beautifying XML documents</title>
		<link>http://www.codediesel.com/pear/beautifying-xml-documents/</link>
		<comments>http://www.codediesel.com/pear/beautifying-xml-documents/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 04:48:52 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=1590</guid>
		<description><![CDATA[how to beautify and format xml documents]]></description>
			<content:encoded><![CDATA[<p>I frequently write php code to access various web services and the most common response data I encounter is in xml, which most of the time is not formatted. I use <a target="_blank" href="http://www.wmhelp.com/">xmlPad</a> to format and analyze xml documents, but many times I need to format xml documents on the production server wherein xmlPad is of no use. What one needs is a library that would allow you to beautify your untidy xml documents within your php code. <a target="_blank" href="http://pear.php.net/manual/en/package.xml.xml-beautifier.php">XML_Beautifier</a> provides that solution.<br />
<span id="more-1590"></span></p>
<h4>Installation</h4>
<p>XML_Beautifier being a Pear package we will use the Pear installer as below. I recommend to always use the Pear installer to download packages rather than dowloading it manually as the Pear installer automatically downloads any dependent packages.</p>

<div class="wp_codebox"><table><tr id="p15901"><td class="code" id="p1590code1"><pre class="text" style="font-family:monospace;">pear install XML_Beautifier</pre></td></tr></table></div>

<h4>Usage</h4>
<p>You can beautify a xml file or a on-the-fly generated xml string. The following will nicely format a untidy xml file. The input file must be a valid xml document or the parser will flag an error.</p>

<div class="wp_codebox"><table><tr id="p15902"><td class="code" id="p1590code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'unformated.xml'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beautified.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</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: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatting a xml string is quite as simple.</p>

<div class="wp_codebox"><table><tr id="p15903"><td class="code" id="p1590code3"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/* Unformatted xml string */</span>
<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;rootNode&gt;&lt;foo   bar = &quot;pear&quot;&gt;hello world!&lt;/foo&gt;&lt;/rootNode&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Formatted xml string.</p>

<div class="wp_codebox"><table><tr id="p15904"><td class="code" id="p1590code4"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;foo</span> <span style="color: #000066;">bar</span>=<span style="color: #ff0000;">&quot;pear&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>hello world!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/foo<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rootNode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>If you frequently need to format xml documents than it would be better to have a command line access to the beautifier as below.</p>

<div class="wp_codebox"><table><tr id="p15905"><td class="code" id="p1590code5"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** xmlformat.php
 *  usage: php xmlformat.php untidy.xml beautified.xml
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$argc</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">3</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>Usage: <span style="color: #006699; font-weight: bold;">$argv[0]</span> unformatted.xml formatted.xml<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">require_once</span> <span style="color: #0000ff;">&quot;XML/Beautifier.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Formatting <span style="color: #006699; font-weight: bold;">{$argv[1]}</span>...&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fmt</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">formatFile</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$argv</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>PEAR<span style="color: #339933;">::</span><span style="color: #004000;">isError</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: #000000; font-weight: bold;">echo</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #0000ff;">&quot;Done&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<h4>Options</h4>
<p>The <em>XML_Beautifier</em> class also accepts a array of options which you can pass to the constructor during initialization. The details of the options can be found <a target="_blank" href="http://pear.php.net/manual/en/package.xml.xml-beautifier.options.php">here</a>.</p>

<div class="wp_codebox"><table><tr id="p15906"><td class="code" id="p1590code6"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">&quot;caseFolding&quot;</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;caseFoldingTo&quot;</span>     <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;uppercase&quot;</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;normalizeComments&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$fmt</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XML_Beautifier<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>One thing to note in closing is that the formatting of big xml documents can take some time depending on your machine configuration, so if you plan on formatting documents on a live server you need to take that into account.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/pear/beautifying-xml-documents/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Posting XML from PHP</title>
		<link>http://www.codediesel.com/php/posting-xml-from-php/</link>
		<comments>http://www.codediesel.com/php/posting-xml-from-php/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 08:27:41 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=7</guid>
		<description><![CDATA[There is a frequent need to post xml data to a url. Below is shown an example which lets you post xml data using CURL. . Download code]]></description>
			<content:encoded><![CDATA[<p>There is a frequent need to post xml data to a url. Below is shown an example which lets you post xml data using CURL.<span id="more-7"></span></p>
<hr size="1" /><img src="http://www.codediesel.com/data/images/xmlpost.gif" alt="sdsd" align="middle" />.</p>
<hr size="1" />
<a href="http://www.codediesel.com/data/code/xml_post.sphp" class="download">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/posting-xml-from-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

