如何在PHP中解析XML文件 [英] How to Parse XML File in PHP

查看:140
本文介绍了如何在PHP中解析XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XML文件位于此处 . 如何在此XML文件中获取源内容?

The XML file is here. How can I get the source content in this XML file?

推荐答案

实际上,有四种相对简单的方式来读取XML文件:

Actually there are four relatively simple ways to read an XML file:

  • DOM ,它使用DOM API(因此具有将整个文档加载到内存中)
  • SimpleXML ,它提供了一种非常简单而优雅的方法解析XML文档(但缺少很多文档处理方法),它也将整个文档加载到内存中
  • XMLReader 是基于流的XML提取解析器.它的用法不像上面的两个其他选项那样直观,但是当您必须解析大型文档时(因为它不需要将整个文档加载到内存中并在XML 流).令人高兴的是,它允许您通过 XMLReader::expand() .
  • XML Parser 是一个非常低级的组件,它允许您创建SAX解析器,这意味着您定义了处理程序函数,这些函数在读取XML文件时将被调用;本质上,它们具有与XMLReader(在流上运行)
  • 相同的优势
  • DOM which uses the DOM API (and therefore has to load the whole document into memory)
  • SimpleXML which provides a very simple and elegant way to parse XML documents (but lacks a lot of document manipulation methods), it also loads the whole document into memory
  • XMLReader is a stream-based XML pull parser. It's usage is not as intuitive as the usage of both other options above, but it can be a life-saver when you have to parse large documents (as it does not need to load the whole document into memory and operates on the XML stream). The nice thing is that it allows you to inter-operate with the DOM via XMLReader::expand().
  • XML Parser is a very low-level component which allows you to create SAX parsers, which means that you define handler functions which will be called when reading the XML file; essentially they have the same benefits as the XMLReader (operating on streams)

我个人的最爱是:

  • SimpleXML解析相对较小的XML文件而无需修改它们时
  • 解析大型XML文件时
  • XMLReader
  • SimpleXML when parsing relatively small XML files without the need to modifiy them
  • XMLReader when parsing large XML files

这篇关于如何在PHP中解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆