Hướng dẫn php xml stream reader
I have the following XML file, the file is rather large and i haven't been able to get simplexml to open and read the file so i'm trying XMLReader with no success in php
I've unfortunately not found a good tutorial on this for PHP and would love to see how I can get each element content to store in a database. BenMorel 32.6k48 gold badges170 silver badges302 bronze badges asked Dec 2, 2009 at 19:17 Shadi AlmosriShadi Almosri 11.5k16 gold badges57 silver badges79 bronze badges 1 It all depends on how big the unit of work, but I guess you're trying to treat each For that, the simplest way would be to use XMLReader to get to each node, then use SimpleXML to access them. This way, you keep the memory usage low because you're treating one node at a time and you still leverage SimpleXML's ease of use. For instance:
Quick overview of pros and cons of different approaches: XMLReader only
XMLReader + SimpleXML
XMLReader + DOM
My advice: write a prototype with SimpleXML, see if it works for you. If performance is paramount, try DOM. Stay as far away from XMLReader as possible. Remember that the more code you write, the higher the possibility of you introducing bugs or introducing performance regressions. answered Dec 2, 2009 at 19:45 13 For xml formatted with attributes... data.xml:
php code:
answered Oct 5, 2012 at 17:06 try5tan3try5tan3 1932 silver badges9 bronze badges 1 The accepted answer gave me a good start, but brought in more classes and more processing than I would have liked; so this is my interpretation:
answered Jun 20, 2014 at 6:07 Francis LewisFrancis Lewis 8,6728 gold badges53 silver badges62 bronze badges Most of my XML parsing life is spent extracting nuggets of useful information out of truckloads of XML (Amazon MWS). As such, my answer assumes you want only specific information and you know where it is located. I find the easiest way to use XMLReader is to know which tags I want the information out of and use them. If you know the structure of the XML and it has lots of unique tags, I find that using the first case is the easy. Cases 2 and 3 are just to show you how it can be done for more complex tags. This is extremely fast; I have a discussion of speed over on What is the fastest XML parser in PHP? The most important thing to remember when doing tag-based
parsing like this is to use
answered Oct 15, 2014 at 18:02 JosiahJosiah 2,8481 gold badge32 silver badges45 bronze badges
answered Oct 10, 2013 at 7:04 sebobsebob 4755 silver badges13 bronze badges XMLReader is well documented on PHP site. This is a XML Pull Parser, which means it's used to iterate through nodes (or DOM Nodes) of given XML document. For example, you could go through the entire document you gave like this:
It is then up to you to decide how to deal with the node returned by XMLReader::expand(). answered Dec 2, 2009 at 19:42 PercutioPercutio 9817 silver badges8 bronze badges 4 This Works Better and Faster For Me
**The Backend File **
answered Aug 19, 2020 at 12:50 Not the answer you're looking for? Browse other questions tagged php xml parsing simplexml xmlreader or ask your own question. |