published about 1 year ago
(27.03.2007 07:43)
PHP’s XML workhorse, SimpleXML can be extended. you can supply a class of your own for PHP to wrap all XML nodes instead of the default SimpleXMLElement. do this by passing the desired class name as a string to the simplexml_load_(file|string) methods:
<?php
class MySimpleXMLElement extends SimpleXMLElement
{
// extending parent method
public function xpath($path)
{
echo "evaluating the following xpath expression: $path\n";
$result = parent::xpath($path);
echo "found " . sizeof($result) . " nodes";
return $result;
}
}
$doc = simplexml_load_file('products.xml', 'MySimpleXMLElement');
// calling an extended method
$nodes = $doc->xpath('//articles/article/shortdesc');
echo "\n";
// calling a parent method
echo $doc->root->getName();
echo "\n";
?>
i didn’t poke around in the internals of the parent class too much, no idea how well that would. on the other hand, in many cases it might be a better solution to delegate to SimpleXML instead of inheriting and extending from it.
Posted in php, coding | Tags php, simplexml, xml | no comments
published about 1 year ago
(18.03.2007 11:20)
update: here’s the same for PHP’s XML Parser.
a quick comparison of the two libraries available for processing XML in ruby shows dramatic performance differences.
am i missing something, is there a fundamental flaw in the test? of course REXML is pure ruby, while libxml is C; but can the difference really be so huge?
loading an xml file
| file size |
libxml |
REXML |
factor |
| 10KB |
0,83 |
39,17 |
47,0 |
| 100KB |
6,67 |
306,56 |
46,0 |
| 1.6MB |
71,88 |
3954,21 |
55,0 |
simple xpath expression
| file size |
libxml |
REXML |
factor |
| 10KB |
0,12 |
124,68 |
1004,7 |
| 100KB |
0,67 |
678,11 |
1016,8 |
| 1.6MB |
6,21 |
22578,18 |
3633,6 |
the test code
def benchmark
start = Time.new.to_f
10.times { yield }
puts ((Time.new.to_f - start) / 10) * 1000
end
doc = nil
File.read('products.xml')
require 'rubygems'
require 'xml/libxml'
benchmark do
doc = XML::Document.file("products.xml")
end
benchmark do
doc.find('//articles/article/shortdesc').each do |node|
end
end
require "rexml/document"
benchmark do
doc = REXML::Document.new File.read("products.xml")
end
benchmark do
doc.elements.each("//articles/article/shortdesc") do |node|
end
end
Posted in ruby, php, coding | Tags benchmark, libxml, rexml, ruby, xml | no comments