PHP SimpleXML Namespace Problem

The <value> element is not in the same namespace as <cap:parameter>: <cap:parameter> <valueName>VTEC</valueName> <value>/O.CON.KMPX.FL.W.0012.000000T0000Z-110517T1800Z/</value> </cap:parameter> So you have to call children() again. Code (demo) $feed = simplexml_load_file(‘http://alerts.weather.gov/cap/us.php?x=1’); foreach ($feed->entry as $entry){ printf( “ID: %s\nVTEC: %s\n<hr>”, $entry->id, $entry->children(‘cap’, true)->parameter->children()->value ); }

simplexml error handling php

I thinks this is a better way $use_errors = libxml_use_internal_errors(true); $xml = simplexml_load_file($url); if (false === $xml) { // throw new Exception(“Cannot load xml source.\n”); } libxml_clear_errors(); libxml_use_internal_errors($use_errors); more info: http://php.net/manual/en/function.libxml-use-internal-errors.php

Using SimpleXML to create an XML object from scratch

Sure you can. Eg. <?php $newsXML = new SimpleXMLElement(“<news></news>”); $newsXML->addAttribute(‘newsPagePrefix’, ‘value goes here’); $newsIntro = $newsXML->addChild(‘content’); $newsIntro->addAttribute(‘type’, ‘latest’); Header(‘Content-type: text/xml’); echo $newsXML->asXML(); ?> Output <?xml version=”1.0″?> <news newsPagePrefix=”value goes here”> <content type=”latest”/> </news> Have fun.

Loop through an XML object with SimpleXML

SimpleXML doesn’t have a getElementsByTagName() method (DOMDocument does). In SimpleXML, the object (e.g $xml) is treated as the root element. So you can loop through the product items like so: $xml = simplexml_load_string($xmlString); foreach($xml->products->item as $item) { echo (string)$item->product_id; echo (string)$item->model; } Example of building a devices associative array: $devices = array(); $xml = simplexml_load_string($xmlString); … Read more

How to Paginate lines in a foreach loop with PHP

A very elegant solution is using a LimitIterator: $xml = simplexml_load_string($rawxml); // can be combined into one line $ids = $xml->xpath(‘id’); // we have an array here $idIterator = new ArrayIterator($ids); $limitIterator = new LimitIterator($idIterator, $offset, $count); foreach($limitIterator as $value) { // … } // or more concise $xml = simplexml_load_string($rawxml); $ids = new LimitIterator(new … Read more

PHP – Processing Invalid XML

What you need is something that will use libxml’s internal errors to locate invalid characters and escape them accordingly. Here’s a mockup of how I’d write it. Take a look at the result of libxml_get_errors() for error info. function load_invalid_xml($xml) { $use_internal_errors = libxml_use_internal_errors(true); libxml_clear_errors(true); $sxe = simplexml_load_string($xml); if ($sxe) { return $sxe; } $fixed_xml=””; … Read more

Using SimpleXML to load remote URL

The results are coming back as json. Replace simplexml_load_file with json_decode and you will see a proper object. If you want to use xml, you need to specify it in the headers. The following code will return valid xml: $context = stream_context_create(array(‘http’ => array(‘header’ => ‘Accept: application/xml’))); $url=”http://api.ean.com/ean-services/rs/hotel/v3/list?cid=55505&minorRev=12&apiKey=2hkhej72gxyas3ky6hhjtsga&locale=en_US&currencyCode=USD&customerIpAddress=10.184.2.9&customerSessionId=&xml=<HotelListRequest><arrivalDate>01/22/2012</arrivalDate><departureDate>01/24/2012</departureDate><RoomGroup><Room><numberOfAdults>1</numberOfAdults><numberOfChildren>1</numberOfChildren><childAges>4</childAges></Room></RoomGroup><city>Amsterdam</city><countryCode>NL</countryCode><supplierCacheTolerance>MED</supplierCacheTolerance></HotelListRequest> “; $xml = file_get_contents($url, false, $context); $xml … Read more

SimpleXML: Working with XML containing namespaces

You could work with XPath and registerXPathNamespace(): $xml->registerXPathNamespace(“georss”, “http://www.georss.org/georss”); $xml->registerXPathNamespace(“gml”, “http://www.opengis.net/gml”); $pos = $xml->xpath(“/georss:where/gml:Point/gml:pos”); From the docs, emphasis mine: registerXPathNamespace […] Creates a prefix/ns context for the next XPath query. More ways to handle namespaces in SimpleXML can be found here, for example: Stuart Herbert On PHP – Using SimpleXML To Parse RSS Feeds