<?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>Thijs Lensselink&#039;s Blog &#187; SimpleXML</title>
	<atom:link href="http://lenss.nl/tag/simplexml/feed/" rel="self" type="application/rss+xml" />
	<link>http://lenss.nl</link>
	<description>Webdevelopment and stuff...</description>
	<lastBuildDate>Thu, 26 Apr 2012 21:48:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Parsing XML CDATA with SimpleXML</title>
		<link>http://lenss.nl/2008/10/parsing-xml-cdata-with-simplexml/</link>
		<comments>http://lenss.nl/2008/10/parsing-xml-cdata-with-simplexml/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 13:19:19 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[CDATA]]></category>
		<category><![CDATA[LIBXML]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SimpleXML]]></category>

		<guid isPermaLink="false">http://lenss.nl/?p=149</guid>
		<description><![CDATA[I was quite amazed today. When loading a SOAP response into a SimpleXMLElement i noticed some fields were left blank. I should have checked the SOAP response first. But instead told our Delphi guy that the response was not filled correctly. This was not the case :) When we both saw the SOAP response was [...]]]></description>
			<content:encoded><![CDATA[<p>I was quite amazed today. When loading a SOAP response into a SimpleXMLElement i noticed some fields were left blank. I should have checked the SOAP response first. But instead told our Delphi guy that the response was not filled correctly. This was not the case :)</p>
<p>When we both saw the SOAP response was in perfect shape. We started to poke around on the PHP side. The strange thing was all tag names were taken from the response correctly. It was just the data that was missing.  Then we noticed the data missing was inside <a href="http://en.wikipedia.org/wiki/CDATA" target="_blank">CDATA</a> tags.</p>
<p>From a first glance at the PHP manual it it wasn&#8217;t clear what was going on. So i did some googleing. And found a good <a href="http://blog.agoraproduction.com/index.php?/archives/53-Quick-Tip-PHP,-SimpleXML-and-CDATA.html" target="_blank">post</a> by <a href="http://blog.agoraproduction.com/" target="_blank">David Coallier</a>. This post solved the problem. The example showed how to add an extra <a href="http://xmlsoft.org/" target="_blank">LIBXML</a> options to the <a href="http://nl2.php.net/manual/en/function.simplexml-load-string.php" target="_blank">simplexml_load_String</a> method. Although David provided the solution. I still wanted to make post. Maybe it will help somebody.</p>
<pre name="code" class="php">
    // parsing with CDATA tags using the *_load_string method
    $xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);

    // parsing with CDATA tags using the OO way
    $xml = new SimpleXMLElement($string, LIBXML_NOCDATA);</pre>
<p>The LIBXML options that can be passed to the *_load methods and constructor can be found in the <a href="http://nl2.php.net/manual/en/libxml.constants.php" target="_blank">php documentation</a>.</p>
<p>It&#8217;s pretty damn weird though. I want to parse the CDATA tags inside my XML. And can only do so by providing the NOCDATA option.</p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/10/parsing-xml-cdata-with-simplexml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP5 SimpleXML and xpath</title>
		<link>http://lenss.nl/2008/08/php5-simplexml-and-xpath/</link>
		<comments>http://lenss.nl/2008/08/php5-simplexml-and-xpath/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 12:55:27 +0000</pubDate>
		<dc:creator>Thijs Lensselink</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SimpleXML]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://we.designandco.de/?p=50</guid>
		<description><![CDATA[Yesterday when working with some rather large XML files i noticed. That it&#8217;s not possible to do -&#62;xpath() calls on sub nodes of a SimpleXMLElement. If that&#8217;s not clear. Here is a small example. $xmlStr = '&#60;root&#62; &#60;fareOption&#62; &#60;fare&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;/fare&#62; &#60;/fareOption&#62; &#60;fareOption&#62; &#60;fare&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;flight&#62;&#60;/flight&#62; &#60;/fare&#62; &#60;/fareOption&#62; &#60;/root&#62;'; $objXML = simplexml_load_string($xmlStr); [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday when working with some rather large XML files i noticed. That it&#8217;s not possible to do -&gt;xpath() calls on sub nodes of a SimpleXMLElement. If that&#8217;s not clear. Here is a small example.</p>
<pre name="code" class="php">$xmlStr = '&lt;root&gt;
	&lt;fareOption&gt;
		&lt;fare&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
		&lt;/fare&gt;
	&lt;/fareOption&gt;
	&lt;fareOption&gt;
		&lt;fare&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
			&lt;flight&gt;&lt;/flight&gt;
		&lt;/fare&gt;
	&lt;/fareOption&gt;
&lt;/root&gt;';

$objXML = simplexml_load_string($xmlStr);
$fareList = $objXML-&gt;xpath('//fareOption');

foreach ($fareList as $fare) {
    $flightList = $fare-&gt;xpath('//flight');
    print_r($flightList);
}</pre>
<p>So first i try to get all the fareOption elements from the XML structure. And loop through the extracted elements. In the loop i want to do a -&gt;xpath call on $fare which is a instance of SimpleXMLElement. The result however is not what i expected. Instead of extracting all the flights of a $fare node. It extracts all flights from the root of the document. So instead of 3 flight nodes i get 6.</p>
<p>I couldn&#8217;t really find a solution for this problem. And from reading some bug reports i understand this is how the PHP implementation of xpath works at the moment. To solve this problem. There is a small work around though. Instead of running the -&gt;xpath() call on the $fare node directly. We can create a new SimpleXMLElement from the $fare node. And do a -&gt;xpath() call on that. This will look something like this.</p>
<pre name="code" class="php">foreach ($fareList as $fare) {
    $tempList = simplexml_load_string($fare-&gt;asXML());
    $flightList = $tempList-&gt;xpath('//flight');
    print_r($flightList);
}</pre>
<p>This creates the expected output.</p>
]]></content:encoded>
			<wfw:commentRss>http://lenss.nl/2008/08/php5-simplexml-and-xpath/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
