Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Evert Pot:
An XML library for PHP you may not hate.
Apr 02, 2015 @ 16:13:55

Evert Pot has posted about an XML library you may not hate, the sabre/xml library.

If you are writing or consuming API's in PHP, chances are that you need to work with XML. In some cases you may even prefer it. You may have started with SimpleXML and after a while switched to using the DOM after realizing SimpleXML is really not that simple if you strictly use xml namespaces everywhere.

For writing XML, you may have found that using the DOM requires far too much code, or you may simply generate your XML by echoing strings, knowing that it may not be the best idea. sabre/xml hopes to solve your issues, by wrapping XMLReader and XMLWriter, and providing standard design patterns.

He includes some example code showing how it works, extending the XMLReader/Writer functionality with a simplified interface. He includes examples of both writing a new XML file or reading in and working with the contents of a given one. He does point out one issue, though - the library cannot really read in XML contents, modify it and send it back out (it's a "single pass" system). He wraps up the post talking about the various interfaces and elements in the library and some of the overall benefits it provides.

tagged: xml library xmlreader xmlwriter interface simple

Link: http://evertpot.com/an-xml-library-you-may-not-hate/

Christian Schaefer's Blog:
Simply iterate over XML with plain PHP using little memory and CPU
Mar 10, 2011 @ 14:11:31

In a new post to his Test.ical.ly blog Christian Schaefer shows you how to iterate over XML in a more efficient way with the help of the XMLReader and Iterator features that come with PHP.

One of the things I have been working on lately was a simple XML parser. It’s a simple XML structure in my case though it could be more complex without much change. My solution was a quite powerful yet simple combination of XMLReader and the Iterator interface.

He includes a sample XML document similar to the one he was working with and shows how XMLReader can handle it, keeping only the currently needed information in memory at one time. His sample class (CustomXml) loads the file and defines all of the iterator methods to work with the data like "next", "prev" and "rewind".

tagged: iterate xml xmlreader iterator memory tutorial

Link:

PHPBuilder.com:
A Quick PHP XMLWriter Class Tutorial: XML & RSS
Jan 19, 2009 @ 17:19:52

The PHPBuilder.com has posted today looking to introduce you to a powerful XML creation tool in PHP5 installations - XMLWriter.

They jump right in and show it in action by creating a simple RSS document with one element inside. A call to the flush() method on your XMLWriter object is all it takes to output the XML.

They also change things up and bit and swap out the procedural code for a more object-oriented approach with a constructor that sets up the object and adds in some of the common RSS information. Their addItem() method is called to add in a sample item (in this case, an entry for a CD player product) and _endRSS() closes things off and runs that flush() to send it all back out.

You can find out more about XMLWriter (and its sister, XMLReader) in the PHP manual.

tagged: xmlwriter tutorial rss class oop php5 xmlreader

Link:

Jeremy Johnstone's Blog:
PHP parser for OS X plist XML files
Oct 27, 2008 @ 14:35:56

Jeremy Johnstone has come up with a simple plist xml parser. The plist format is what iTunes (and some other application) uses for its library files.

Normally SimpleXML is enough to handle most XML parsing needs, but the plist XML format is just broken enough to make parsing it with SimpleXML virtually impossible. Since I hadn't played with XMLReader much, I thought it would be a good chance to give it a spin. For the anxious types, the code is available on github in my php_class_lib project, so dig right in.

His parser takes in the name of the file to fetch and a parser() method is called to do the actual work. The contents of the file are returned as an array (he includes a print_r() of that output too).

tagged: osx parse xmlreader plist xml file tutorial github

Link:

Christian Weiske's Blog:
Importing huge XML files using PHP5 - efficiently and conveniently
Aug 25, 2008 @ 14:34:38

Christian Weiske has a quick tip on how to get larger XML files to pull into PHP5 and be usable:

At work I had the task to implement the synchronization between an online shop and a commodity management system. Data exchange format was XML - one big XML file for all of the products (some thousands with dozens of attributes). Big question: How do I import the file in a way that is most convenient for me as a programmer - and without exceeding the machine's RAM when loading a 1 GiB file?

The newer alternatives both use the same technology (DOM and SimpleXML - with DOM behind it) so he goes more "low tech" than that and opts for the XMLReader extension to pull in the large amounts of data. Available in PHP5, the XMLReader extension, which he combines with an Iterator from the SPL to makes for a simple, quick little parser.

tagged: import xml file php5 efficient convenient xmlreader spl iterator

Link:

Zend Developer Zone:
XML and PHP 5
Aug 01, 2007 @ 15:13:00

The Zend Developer Zone has a new tutorial posted looking at PHP5 and the new XML tools that it has to offer - as broken up into categories:

Things are a bit different using XML in PHP 5. In a similar scenario, you could use one of the new native extensions, like DOM or SimpleXML, to manipulate the XML data tree. This tree can then be passed directly to and used by the XSL extension without incurring any additional overhead. There is no serialization nor copying involved; ext/xsl is able to work directly with the XML tree it is given, resulting in a significant improvement in use of system resources as compared to coding a similar task in PHP 4. So now that you have an idea why XML support got a face lift, you might like to know about the different tools available to you.
tagged: xml php5 tutorial introduction dom xsl xmlreader xmlwriter simplexml xml php5 tutorial introduction dom xsl xmlreader xmlwriter simplexml

Link:

Zend Developer Zone:
XML and PHP 5
Aug 01, 2007 @ 15:13:00

The Zend Developer Zone has a new tutorial posted looking at PHP5 and the new XML tools that it has to offer - as broken up into categories:

Things are a bit different using XML in PHP 5. In a similar scenario, you could use one of the new native extensions, like DOM or SimpleXML, to manipulate the XML data tree. This tree can then be passed directly to and used by the XSL extension without incurring any additional overhead. There is no serialization nor copying involved; ext/xsl is able to work directly with the XML tree it is given, resulting in a significant improvement in use of system resources as compared to coding a similar task in PHP 4. So now that you have an idea why XML support got a face lift, you might like to know about the different tools available to you.
tagged: xml php5 tutorial introduction dom xsl xmlreader xmlwriter simplexml xml php5 tutorial introduction dom xsl xmlreader xmlwriter simplexml

Link:


Trending Topics: