Created
September 12, 2009 01:19
-
-
Save juarezpaf/185688 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| //Via http://labbs.com.br/ | |
| class xml2Array { | |
| var $arrOutput = array(); | |
| var $resParser; | |
| var $strXmlData; | |
| function parse($strInputXML) { | |
| $this->resParser = xml_parser_create (); | |
| xml_set_object($this->resParser,$this); | |
| xml_set_element_handler($this->resParser, "tagOpen", "tagClosed"); | |
| xml_set_character_data_handler($this->resParser, "tagData"); | |
| $this->strXmlData = xml_parse($this->resParser,$strInputXML ); | |
| if(!$this->strXmlData) { | |
| die(sprintf("XML error: %s at line %d", | |
| xml_error_string(xml_get_error_code($this->resParser)), | |
| xml_get_current_line_number($this->resParser))); | |
| } | |
| xml_parser_free($this->resParser); | |
| return $this->arrOutput; | |
| } | |
| function tagOpen($parser, $name, $attrs) { | |
| $tag=array("name"=>$name,"attrs"=>$attrs); | |
| array_push($this->arrOutput,$tag); | |
| } | |
| function tagData($parser, $tagData) { | |
| if(trim($tagData)) { | |
| if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) { | |
| $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData; | |
| } | |
| else { | |
| $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData; | |
| } | |
| } | |
| } | |
| function tagClosed($parser, $name) { | |
| $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1]; | |
| array_pop($this->arrOutput); | |
| } | |
| } | |
| //depois de colocar nossa classe vamos pegar a URL | |
| //da bovespa passando a acao que nos queremos obter o resultado. | |
| //em seguida vamos dar um print_r nesse resultados | |
| //para ver como que ficou a array | |
| $xml = file_get_contents("http://www.bovespa.com.br/Mercado/RendaVariavel/InfoPregao/ExecutaAcaoAjax.asp?CodigoPapel=PETR4"); | |
| $x = new xml2Array();// instanciamos a classe xml2array | |
| $conteudo = $x->parse($xml); //parseamos o resultado em uma array | |
| echo "<br/>"; | |
| nl2br(print_r($conteudo)); // damos um print_r para ver como ficou o resultado | |
| //da array logo em seguida mostramos apenas os dados que nos queremos | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment