3

which one of the two is more spread? I want to read out the version number from http://freshmeat.net/projects-xml/mysql/mysql.xml?branch_id=46519 but I want to use the one which more people have.

If you know another way to get the latest stable version number from mysql please tell me ;)

Thomaschaaf
  • 17,847
  • 32
  • 94
  • 128

4 Answers4

6

For this kind of task, reading the document into a DomDocument and using DomXPath is probably more suitable.

To answer your question, both libraries (as well as DomDocument + DomXPath) are standard outfit with PHP5, so they would be equally fine choices.

troelskn
  • 115,121
  • 27
  • 131
  • 155
  • I would specifically reccomend the LoadHTML file method as a good starting point as it was designed to work with this and wont be confused with sub tags. http://us3.php.net/manual/en/domdocument.loadhtml.php – preinheimer Nov 15 '08 at 20:08
  • 1
    Since this is XML and not HTML, loadXml would be more appropriate: http://docs.php.net/manual/en/domdocument.loadxml.php – troelskn Nov 15 '08 at 20:18
3

It has to be SimpleXML. It is enabled by default, is quicker to load XML documents than the Dom methods, has a smaller memory foot-print than the Dom methods, and has much simpler xpath methods than the Dom methods:

$xml = simplexml_load_file(
    'http://freshmeat.net/projects-xml/mysql/mysql.xml?branch_id=46519'
    );
$result = $xml->xpath('//latest_release/latest_release_version'); 
// or '//latest_release/*' if you'd rather loop through all release information.

while(list( , $node) = each($result))
    echo $node, "\n";
Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134
1

For large files use XMLreader, simpleXML will consume all memory on large files.

Grumpy
  • 2,140
  • 1
  • 25
  • 38
0

SimpleXML was introduced in PHP5 while XmlReader was only included by default in version 5.1, so the former is probably the best way to go:

$struct = simplexml_load_string($xml);
$version = (string)$struct->project->latest_release->latest_release_version;

However if you're not doing any other XML processing and want to maximise compatibility you could just regex the XML:

if(preg_match('/<latest_release_version>(.*?)<\\/latest_release_version>/', $xml, $matches)){
$version = $matches[1];
}

It's messier than processing the XML properly but is probably faster and supported by nearly all PHP installations.

Walf
  • 8,535
  • 2
  • 44
  • 59
Ciaran McNulty
  • 18,698
  • 6
  • 32
  • 40