How to Parse XML File in PHP

后端 未结 2 1689
故里飘歌
故里飘歌 2020-12-06 14:58

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

相关标签:
2条回答
  • 2020-12-06 15:27

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

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

    0 讨论(0)
  • 2020-12-06 15:28

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

    • 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)

    My personal favorites are:

    • SimpleXML when parsing relatively small XML files without the need to modifiy them
    • XMLReader when parsing large XML files
    0 讨论(0)
提交回复
热议问题