Need help reading XML file with PHP

hi I want to read the file below using php. The file is very large (in GB). Please help me as I don’t know much about this.

Here is the file

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Articles> <Article> <header>info about article </header> <metadata> <dc:title>title here</dc:title> <dc:author>author 1</dc:author> <dc:author>author 2</dc:author> <dc:author>author 3</dc:author> <dc:author>author n</dc:author> <dc:subject>subject here</dc:subject> </metadata> </Article> <resume> resume infor </resume> </Articles> 
+4
source share
3 answers

If the file is so large, you probably need an XMLReader to avoid running out of memory, not SimpleXML.

+6
source
 www.w3schools.com/PHP/php_xml_simplexml.asp 

Try reading this page. Its a big help.

Refresh . This snippet was found on StackOverflow.com messages:

 <?php class SimpleDMOZParser { protected $_stack = array(); protected $_file = ""; protected $_parser = null; protected $_currentId = ""; protected $_current = ""; public function __construct($file) { $this->_file = $file; $this->_parser = xml_parser_create("UTF-8"); xml_set_object($this->_parser, $this); xml_set_element_handler($this->_parser, "startTag", "endTag"); } public function startTag($parser, $name, $attribs) { array_push($this->_stack, $this->_current); if ($name == "TOPIC" && count($attribs)) { $this->_currentId = $attribs["R:ID"]; } if ($name == "LINK" && strpos($this->_currentId, "Top/Home/Consumer_Information/Electronics/") === 0) { echo $attribs["R:RESOURCE"] . "\n"; } $this->_current = $name; } public function endTag($parser, $name) { $this->_current = array_pop($this->_stack); } public function parse() { $fh = fopen($this->_file, "r"); if (!$fh) { die("Epic fail!\n"); } while (!feof($fh)) { $data = fread($fh, 4096); xml_parse($this->_parser, $data, feof($fh)); } } } $parser = new SimpleDMOZParser("content.rdf.u8"); $parser->parse(); 
0
source

Good place to start - SimpleXML from PHP

0
source

Source: https://habr.com/ru/post/1344528/


All Articles