Best XML Parser for PHP
I would have to say SimpleXML takes the cake because it is firstly an extension, written in C, and is very fast. But second, the parsed document takes the form of a PHP object. So you can "query" like $root->myElement
.
What is the fastest XML parser in PHP?
The fastest parser will be SAX -- it doesn't have to create a dom, and it can be done with partial xml, or progressively. Info on the PHP SAX parser (Expat) can be found here. Alternatively there is a libxml based DOM parser named SimpleXML. A DOM based parser will be easier to work with but it is typically a few orders of magnitude slower.
What XML parser to use with PHP
You should look at SimpleXML:
http://php.net/manual/en/class.simplexmlelement.php
Take a look here:
http://debuggable.com/posts/parsing-xml-using-simplexml:480f4dfe-6a58-4a17-a133-455acbdd56cb
http://www.w3schools.com/PHP/php_xml_simplexml.asp
But most importantly, searching SO often has answers to what you need:
How to Parse XML File in PHP
Parsing XML with PHP's simpleXML
How to parse xml with php?
You can use SimpleXML_Load_String.
<?php // RAY_temp_burhan.php
error_reporting(E_ALL);
echo '<pre>';
$xml = <<<ENDXML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<eqlist>
<earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANISA) Ilksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0" />
<earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ Ilksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1" />
<earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) Ilksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4" />
</eqlist>
ENDXML;
// CONVERT TO AN OBJECT
$obj = SimpleXML_Load_String($xml);
// PARSE OUT SOME ATTRIBUTES
foreach ($obj as $quake)
{
// ATTRIBUTE NAMES ARE CASE-SENSITIVE
$loc = $quake->attributes()->lokasyon;
$dep = $quake->attributes()->Depth;
echo PHP_EOL . "$loc $dep";
}
Best way to process large XML in PHP
For a large file, you'll want to use a SAX parser rather than a DOM parser.
With a DOM parser it will read in the whole file and load it into an object tree in memory. With a SAX parser, it will read the file sequentially and call your user-defined callback functions to handle the data (start tags, end tags, CDATA, etc.)
With a SAX parser you'll need to maintain state yourself (e.g. what tag you are currently in) which makes it a bit more complicated, but for a large file it will be much more efficient memory wise.
Parsing extremely large XML files in php
In PHP, you can read in extreme large XML files with the XMLReader
Docs:
$reader = new XMLReader();
$reader->open($xmlfile);
Extreme large XML files should be stored in a compressed format on disk. At least this makes sense as XML files have a high compression ratio. For example gzipped like large.xml.gz
.
PHP supports that quite well with XMLReader
via the compression wrappersDocs:
$xmlfile = 'compress.zlib://path/to/large.xml.gz';
$reader = new XMLReader();
$reader->open($xmlfile);
The XMLReader
allows you to operate on the current element "only". That means it's forward-only. If you need to keep parser state, you need to build it your own.
I often find it helpful to wrap the basic movements into a set of iterators that know how to operate on XMLReader
like iterating through elements or child-elements only. You find this outlined in Parse XML with PHP and XMLReader.
See as well:
- PHP open gzipped XML
PHP what is the best approach to using XML? Need to create and parse XML responses
PHP supports a number of XML libraries.
If memory is an issue, for instance due to large files, use an Event-based parser over a tree-based one.Tree-based parsers must fully load the file into memory in order to parse the XML. Event-based parsers do not need to load the entire file into memory to begin parsing.
See this article about what's new with XML in PHP5 and a discussion of their pros and cons.
You might also reconsider where you store the data, since filesystem might be too slow for millions of calls. How about Xindice?
Better way to parse XML using PHP to create hierarchy with data table in it
get an array of unique <categoryname>
-nodes with xpath, then loop through it and select all <pricesheet>
-nodes with that specific category, letting xpath do that job, again:
$xml = simplexml_load_string($x);
$cat = array_unique($xml->xpath("//categoryname"));
foreach ($cat as $c) {
echo "$c<br />";
foreach ($xml->xpath("//pricesheet[categoryname='$c']") as $p) {
echo $p->productId."<br />";
}
}
see a live-demo @ http://codepad.viper-7.com/m9ruRU
Of course, you have to add code for creating tables...
Related Topics
How to Create a Pdo Parameterized Query With a Like Statement
Sort Array of Objects by One Property
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Creating One Array from Another Array in PHP
How to Use Pdo Connection in Other Classes
PHP - Check If Two Arrays Are Equal
How to Convert an Object to an Array
Best Way to Avoid Duplicate Entry into MySQL Database
File_Put_Contents - Failed to Open Stream: Permission Denied
Create Subdomains on the Fly With .Htaccess (PHP)
Preferred Method to Store PHP Arrays (Json_Encode VS Serialize)