note 77501 added to function.xml-parse-into-struct

From: Date: Sat, 01 Sep 2007 07:13:10 +0000
Subject: note 77501 added to function.xml-parse-into-struct
Groups: php.notes 
Request: Send a blank email to php-notes+get-129953@lists.php.net to get a copy of this message
HI All , Suppose you need the xml data as the DOM Model structure , the below code will help you to retrieve the xml in any level using the multidimentional array <?php class CSLXmlReader { private $tagstack; private $xmlvals; private $xmlvarArrPos; private $xmlfile; function __construct($filename) // constructor to intialize the stack and val array { $this->tagstack = array(); // contain the open tags till now $this->xmlvals = array(); $this->xmlvarArrPos = $this->xmlvals; // temporary variable to hold the current tag position $this->xmlfile = $filename; } function readDatabase() { // read the XML database $data = implode("", file($this->xmlfile)); $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $data, $values, $tags); xml_parser_free($parser); $tagStackPointer = 0; foreach($values as $key => $val) // { if($val['type'] == "open") { array_push($this->tagstack, $val['tag']); $this->getArrayPath(); if(count($this->xmlvarArrPos) > 0 && (!array_key_exists(0,$this->xmlvarArrPos))) { $temp1 = $this->xmlvarArrPos; $this->xmlvarArrPos = array(); $this->xmlvarArrPos[0] = $temp1; array_push($this->tagstack, 1); } else if( array_key_exists(0,$this->xmlvarArrPos)){ $opncount = count($this->xmlvarArrPos); array_push($this->tagstack, $opncount); } $tagStackPointer += 1; }else if($val['type'] == "close") { while( $val['tag'] != ($lastOpened = array_pop($this->tagstack))){} }else if($val['type'] == "complete") { $this->getArrayPath(); if( array_key_exists($val['tag'],$this->xmlvarArrPos)) { $temp1 = $this->xmlvarArrPos[$val['tag']]; $this->xmlvarArrPos[$val['tag']] = array(); $this->xmlvarArrPos[$val['tag']][0] = $temp1; $this->xmlvarArrPos[$val['tag']][1] = $val['value']; }else if(array_key_exists(0,$this->xmlvarArrPos)) { $elementCount = count($this->xmlvarArrPos); $this->xmlvarArrPos[$val['tag']][$elementCount] = $val['value']; }else { $this->xmlvarArrPos[$val['tag']] = $val['value']; } } } reset($this->xmlvals); return $this->xmlvals; } function getArrayPath() { $tempPos = "--"; reset($this->xmlvals); $this->xmlvarArrPos = &$this->xmlvals; foreach($this->tagstack as $key) { $this->xmlvarArrPos = &$this->xmlvarArrPos[$key]; } } } $readerObj = new CSLXmlReader("htmlpage.xml"); $xmlvals = $readerObj->readDatabase(); echo "########## XML Values In array as the multidimentional array #############\n"; echo "<pre>"; print_r($xmlvals); echo "</pre>"; //echo $xmlvals['home']['welcome']; ?> run this and see the output Dont foget to give a valid xml file path in the input ---- Server IP: 203.16.234.91 Probable Submitter: 122.167.135.72 ---- Manual Page -- http://www.php.net/manual/en/function.xml-parse-into-struct.php Edit -- https://master.php.net/note/edit/77501 Del: integrated -- https://master.php.net/note/delete/77501/integrated Del: useless -- https://master.php.net/note/delete/77501/useless Del: bad code -- https://master.php.net/note/delete/77501/bad+code Del: spam -- https://master.php.net/note/delete/77501/spam Del: non-english -- https://master.php.net/note/delete/77501/non-english Del: in docs -- https://master.php.net/note/delete/77501/in+docs Del: other reasons-- https://master.php.net/note/delete/77501 Reject -- https://master.php.net/note/reject/77501 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#129953) next »