0

I need to read an XML file from the top to the bottom, while doing my reseach I found out about SAX Parser, and more exaclty I found this link:

http://www.brainbell.com/tutorials/php/Parsing_XML_With_SAX.htm which works well and is exactly what I need.

But I can't find an exemple of SAX Parser with a PHP class or in a more "PHP7's way" , I was wondering if it was depreciate or it there are any better alternatives nowadays ?

M.Pau
  • 147
  • 1
  • 3
  • 12
  • 1
    Have a look at XMLReader (http://php.net/manual/en/book.xmlreader.php) – Nigel Ren Mar 08 '18 at 15:58
  • Initating the parser is done using `xml_parser_create` in the given example, and thats a function still present in PHP. Is that already fine for you or are you using something more specific? – Nico Haase Mar 08 '18 at 16:05
  • It's fine I was just wondering if there are sometings better, I'm going to have a look at XMLReader – M.Pau Mar 08 '18 at 16:09

1 Answers1

3

XMLReader (http://php.net/manual/en/book.xmlreader.php) provides a much more modern approach to processing XML piece by piece. Even the article you refer to has this at the end...

PHP 5.1 comes with XMLReader included by default. This is a wrapper on libxml2 and mimics the application programming interface (API) of the C# component for reading XML, XmlTextReader. It is much faster than SAX and just easier to use.

There are a few examples round about how to use XMLReader- How to use XMLReader in PHP?

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55