如何在PHP中解析XML文件

时间:2023-01-15 00:07:45

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

XML文件在这里。如何获取此XML文件中的源内容?

2 个解决方案

#1


15  

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

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

  • DOM which uses the DOM API (and therefore has to load the whole document into memory)
  • 使用DOM API的DOM(因此必须将整个文档加载到内存中)

  • 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
  • SimpleXML提供了一种非常简单而优雅的方式来解析XML文档(但缺少大量的文档操作方法),它还将整个文档加载到内存中

  • 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().
  • XMLReader是一个基于流的XML拉解析器。它的用法并不像上面其他两个选项的使用那样直观,但是当你必须解析大文档时它可以节省生命(因为它不需要将整个文档加载到内存中并在XML流上运行) 。好处是它允许您通过XMLReader :: expand()与DOM进行交互操作。

  • 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)
  • XML Parser是一个非常低级的组件,它允许您创建SAX解析器,这意味着您可以定义在读取XML文件时将被调用的处理函数;基本上它们与XMLReader(在流上运行)具有相同的好处

My personal favorites are:

我个人的最爱是:

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

  • XMLReader when parsing large XML files
  • 解析大型XML文件时的XMLReader

#2


4  

PHP already includes XML parsers in the core or as extensions (i recommend SimpleXML).

PHP已经在核心中包含XML解析器或作为扩展(我推荐使用SimpleXML)。

Try to use it and then ask us if you have any specific problem.

尝试使用它,然后询问我们您是否有任何具体问题。

#1


15  

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

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

  • DOM which uses the DOM API (and therefore has to load the whole document into memory)
  • 使用DOM API的DOM(因此必须将整个文档加载到内存中)

  • 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
  • SimpleXML提供了一种非常简单而优雅的方式来解析XML文档(但缺少大量的文档操作方法),它还将整个文档加载到内存中

  • 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().
  • XMLReader是一个基于流的XML拉解析器。它的用法并不像上面其他两个选项的使用那样直观,但是当你必须解析大文档时它可以节省生命(因为它不需要将整个文档加载到内存中并在XML流上运行) 。好处是它允许您通过XMLReader :: expand()与DOM进行交互操作。

  • 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)
  • XML Parser是一个非常低级的组件,它允许您创建SAX解析器,这意味着您可以定义在读取XML文件时将被调用的处理函数;基本上它们与XMLReader(在流上运行)具有相同的好处

My personal favorites are:

我个人的最爱是:

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

  • XMLReader when parsing large XML files
  • 解析大型XML文件时的XMLReader

#2


4  

PHP already includes XML parsers in the core or as extensions (i recommend SimpleXML).

PHP已经在核心中包含XML解析器或作为扩展(我推荐使用SimpleXML)。

Try to use it and then ask us if you have any specific problem.

尝试使用它,然后询问我们您是否有任何具体问题。