PHP 的最佳 XML 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best XML Parser for PHP
提问by Murat Ayfer
I have used the XML Parser before, and even though it worked OK, I wasn't happy with it in general, it felt like I was using workarounds for things that should be basic functionality.
我以前使用过 XML 解析器,尽管它工作正常,但总体上我对它并不满意,感觉就像我对应该是基本功能的事情使用了变通方法。
I recently saw SimpleXML but I haven't tried it yet. Is it any simpler? What advantages and disadvantages do both have? Any other parsers you've used?
我最近看到了 SimpleXML 但我还没有尝试过。有没有更简单的?两者各有什么优缺点?您使用过的任何其他解析器?
采纳答案by Robert K
I would have to say SimpleXMLtakes 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.
我不得不说SimpleXML很重要,因为它首先是一个用 C 编写的扩展,而且速度非常快。但其次,解析的文档采用 PHP 对象的形式。所以你可以像$root->myElement.
回答by Gordon
Have a look at PHP's available XML extensionsand see http://devzone.zend.com/243/and http://devzone.zend.com/1035/for a discussion of those.
查看 PHP 的可用 XML 扩展并查看http://devzone.zend.com/243/和http://devzone.zend.com/1035/以了解有关这些扩展的讨论。
The main difference between XML Parser and SimpleXML is that the latter is not a pull parser. SimpleXML is built on top of the DOM extensions and will load the entire XML file into memory. XML Parser like XMLReader will only load the current node into memory. You define handlers for specific nodes which will get triggered when the Parser encounters it. That is faster and saves on memory. You pay for that with not being able to use XPath.
XML Parser 和 SimpleXML 之间的主要区别在于后者不是拉式解析器。SimpleXML 构建在 DOM 扩展之上,并将整个 XML 文件加载到内存中。像 XMLReader 这样的 XML Parser 只会将当前节点加载到内存中。您为特定节点定义处理程序,当解析器遇到它时将被触发。这更快并节省内存。由于无法使用 XPath,您为此付出了代价。
Personally, I find SimpleXml quite limiting (hence simple) in what it offers over DOM. You can switch between DOM and SimpleXml easily though, but I usually dont bother and go the DOM route directly. DOM is an implementation of the W3C DOM API, so you might be familiar with it from other languages, for instance JavaScript.
就我个人而言,我发现 SimpleXml 在它提供的 DOM 方面非常有限(因此很简单)。您可以轻松地在 DOM 和 SimpleXml 之间切换,但我通常不会打扰并直接走 DOM 路线。DOM 是 W3C DOM API 的一种实现,因此您可能从其他语言(例如 JavaScript)中熟悉它。
回答by NexusRex
This is a useful function for quick and easy xml parsing when an extension is not available:
当扩展不可用时,这是一个有用的功能,可用于快速轻松地解析 xml:
<?php
/**
* Convert XML to an Array
*
* @param string $XML
* @return array
*/
function XMLtoArray($XML)
{
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $XML, $vals);
xml_parser_free($xml_parser);
// wyznaczamy tablice z powtarzajacymi sie tagami na tym samym poziomie
$_tmp='';
foreach ($vals as $xml_elem) {
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_level!=1 && $x_type == 'close') {
if (isset($multi_key[$x_tag][$x_level]))
$multi_key[$x_tag][$x_level]=1;
else
$multi_key[$x_tag][$x_level]=0;
}
if ($x_level!=1 && $x_type == 'complete') {
if ($_tmp==$x_tag)
$multi_key[$x_tag][$x_level]=1;
$_tmp=$x_tag;
}
}
// jedziemy po tablicy
foreach ($vals as $xml_elem) {
$x_tag=$xml_elem['tag'];
$x_level=$xml_elem['level'];
$x_type=$xml_elem['type'];
if ($x_type == 'open')
$level[$x_level] = $x_tag;
$start_level = 1;
$php_stmt = '$xml_array';
if ($x_type=='close' && $x_level!=1)
$multi_key[$x_tag][$x_level]++;
while ($start_level < $x_level) {
$php_stmt .= '[$level['.$start_level.']]';
if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
$php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
$start_level++;
}
$add='';
if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete')) {
if (!isset($multi_key2[$x_tag][$x_level]))
$multi_key2[$x_tag][$x_level]=0;
else
$multi_key2[$x_tag][$x_level]++;
$add='['.$multi_key2[$x_tag][$x_level].']';
}
if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes', $xml_elem)) {
if ($x_type == 'open')
$php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
else
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
if (array_key_exists('attributes', $xml_elem)) {
if (isset($xml_elem['value'])) {
$php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
eval($php_stmt_main);
}
foreach ($xml_elem['attributes'] as $key=>$value) {
$php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
eval($php_stmt_att);
}
}
}
return $xml_array;
}
?>
回答by Vahan
Hi I think the SimpleXml is very useful . And with it I am using xpath;
嗨,我认为 SimpleXml 非常有用。有了它,我正在使用xpath;
$xml = simplexml_load_file("som_xml.xml");
$blocks = $xml->xpath('//block'); //gets all <block/> tags
$blocks2 = $xml->xpath('//layout/block'); //gets all <block/> which parent are <layout/> tags
I use many xml configs and this helps me to parse them really fast.
SimpleXmlis written on Cso it's very fast.
我使用了许多 xml 配置,这有助于我非常快速地解析它们。
SimpleXml写在C所以它非常快。
回答by dcousineau
It depends on what you are trying to do with the XML files. If you are just trying to read the XML file (like a configuration file), The Wicked Flea is correct in suggesting SimpleXML since it creates what amounts to nested ArrayObjects. e.g. value will be accessible by $xml->root->child.
这取决于您尝试对 XML 文件执行的操作。如果您只是试图读取 XML 文件(如配置文件),The Wicked Flea 建议使用 SimpleXML 是正确的,因为它创建了相当于嵌套 ArrayObjects 的内容。例如,$xml->root->child 可以访问值。
If you are looking to manipulate the XML files you're probably best off using DOM XML
如果您想操作 XML 文件,您可能最好使用DOM XML
回答by Sandeep.C.R
the crxml parser is a real easy to parser.
crxml 解析器是一个真正易于解析的解析器。
This class has got a search function, which takes a node name with any namespace as an argument. It searches the xml for the node and prints out the access statement to access that node using this class. This class also makes xml generation very easy.
这个类有一个搜索功能,它接受一个带有任何命名空间的节点名称作为参数。它在 xml 中搜索节点并打印出访问语句以使用此类访问该节点。这个类也使 xml 生成变得非常容易。
you can download this class at
你可以下载这个类
http://freshmeat.net/projects/crxml
http://freshmeat.net/projects/crxml
or from phpclasses.org
或来自 phpclasses.org
http://www.phpclasses.org/package/6769-PHP-Manipulate-XML-documents-as-array.html
http://www.phpclasses.org/package/6769-PHP-Manipulate-XML-documents-as-array.html

