为什么要使用 XML 解析器?
我是一个有点经验丰富的 PHP 脚本编写者,但是我只是专注于解析 XML 和所有这些好东西。
我似乎无法理解为什么人们会使用单独的 XML 解析器而不是仅仅使用看起来一样简单的 explode
函数。这就是我一直在做的事情(假设路径 xml.php
处有一个有效的 XML 文件):
$contents = file_get_contents("xml.php");
$array1 = explode("<a_tag>", $contents);
$array2 = explode("</a_tag>", $array1[1]);
$data = $array2[0];
所以我的问题是,如果您可以将 XML 解析器分开,那么 XML 解析器的实际用途是什么?将值放入数组并从该点提取数据?
提前致谢! :)
I'm a somewhat experienced PHP scripter, however I just dove into parsing XML and all that good stuff.
I just can't seem to wrap my head around why one would use a separate XML parser instead of just using the explode
function, which seems to be just as simple. Here's what I've been doing (assuming there is a valid XML file at the path xml.php
):
$contents = file_get_contents("xml.php");
$array1 = explode("<a_tag>", $contents);
$array2 = explode("</a_tag>", $array1[1]);
$data = $array2[0];
So my question is, what is the practical use for an XML parser if you can just separate the values into arrays and extract the data from that point?
Thanks in advance! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请原谅我不详细介绍,但对于初学者来说,请尝试
使用爆炸方法进行解析。当你完成后,我们可以继续做一些更棘手的事情;-)
Excuse me for not going into details but for starters try parsing
with your explode-approach. When you're done we can continue with some trickier things ;-)
简而言之,就是它的一致性。在 XML 广泛使用之前,有许多未记录的格式用于在文件中保存信息。 XML 背后的动机之一是创建一种定义明确的标准文档格式。有了这种明确定义的格式,就可以开发出一套通用的解析工具,只要文档遵循上述明确定义的格式,这些工具就可以在文档上一致地工作。
在某些特定情况下,您的示例代码将起作用。但是,如果文档发生更改,
您的解析代码可能会中断。使用正确定义的 XML 解析器编写的代码不会。
In a nutshell, its consistency. Before XML came into wide use there were numerous undocumented formats for keeping information in files. One of the motivators behind XML was to create a well defined, standard document format. With this well defined format in place, a general set of parsing tools could be developed that would work consistently on documents so long as the documents adhered to the aforementioned well defined format.
In some specific cases, your example code will work. However, if the document changes
Your parsing code will probably break. Code written using a correctly defined XML parser will not.
XML 解析器:
XML parsers:
如果
a_tag
有属性,您将如何分解同一个文件?毕竟,
explode("" ...
的工作方式与explode("" ...
不同。XML 解析器了解 XML 规范,只能处理最简单的情况,并且很可能在这种情况下的许多实例中失败。
How would you explode the same file if
a_tag
had an attribute?explode("<a_tag>" ...
will work differently thanexplode("<a_tag attr='value'>" ...
, after all.XML Parsers understand the XML specification. Explode can only handle the simplest of cases, and will most likely fail in a lot of instances of that case.
使用经过验证的 XML 解析方法将使代码更易于维护且易于阅读。如果架构发生变化,它也将使其更容易适应,并且可以更轻松地确定错误条件。 XPath 和 XSLT 的存在是有原因的,它们是经过验证的以合理、易读的方式处理 XML 数据的方法。我建议您使用适用于您的特定情况的任何一个。请记住,仅仅因为您认为自己只是为了一个特定目的而编写代码,您永远不知道一段编写良好的代码会演变成什么。
Using a proven XML parsing method will make the code more maintainable and easy to read. It will also make it more easily adaptable should the schema change, and it can make it easier to determine error conditions. XPath and XSLT exist for a reason, they are proven ways to deal with XML data in a sensible, legible manner. I'd suggest you use whichever is applicable in your given situation. Remember, just because you think you're only writing code for one specific purpose, you never know what a piece of well-written code could evolve into.