如何使用 XML::LibXML 通过 SAX 解析 XML?
到目前为止,我发现的唯一示例代码太旧了,无法再工作(使用已弃用的类)。我所需要的只是演示一些基本内容:
从文件加载并解析 XML
定义 SAX 事件处理程序
读取传递给事件处理程序的元素的属性或文本值
The only example code I have found so far is so old it won't work anymore (uses deprecated classes). All I need is something basic that demonstrates:
Loading and parsing the XML from a file
Defining the SAX event handler(s)
Reading the attributes or text values of the element passed to the event handler
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发行版本身怎么样?
转到 XML::LibXML 分发页面 并单击 浏览。
请注意文档中的以下警告:
还有 XML::SAX 附带 不错的文档。我用过它几次,效果很好,达到了我的目的。
How about the distribution itself?
Go to XML::LibXML distribution page and click browse.
Note the following caution in the documentation:
There is also XML::SAX which comes with nice documentation. I used it a few times and worked well for my purposes.
司南的建议很好,但并没有把所有的点联系起来。下面是我拼凑而成的一个非常简单的程序:
文件 1:处理程序 (MySAXHandler.pm)
文件 2:测试程序 (test.pl)
注意:如何获取元素属性的值。这没有以我可以使用的方式描述。我花了一个多小时才弄清楚语法。这里是。在我的 XML 文件中,属性是 ss:Index。 ss 的命名空间定义为 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"。因此,为了获得愚蠢的 Index 属性,我需要这个:
那很痛苦。
Sinan's suggestion was good, but it didn't connect all the dots. Here is a very simple program that I cobbled together:
file 1: The handlers (MySAXHandler.pm)
file 2: The test program (test.pl)
Note: How to get the values of an element attribute. This was not described in a way that I could use. It took me over an hour to figure out the syntax. Here it is. In my XML file, the attribute was ss:Index. The namespace definition for ss was xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet". Thus, in order to get the silly Index attribute, I needed this:
That was painful.
XML::LibXML::Sax 实现 Perl SAX 接口 并且有一个很好的文档。
XML::LibXML::Sax implements the Perl SAX interface and there is a nice document.