Suppose we have following XML file:
<?xml version="1.0" encoding="UTF-8"?>
<main>
<config>
<controlsWidth type="Number">640</controlsWidth>
<controlsHeight type="Number">480</ controlsHeight>
</config></main>
We want to extract 640 in controlsWidth using PHP.
Define a PHP function:
function get_camtasia_width($file){
//call xml2Array class
$objXML = new xml2Array();
$data=implode("", file($file));
$configxml = $objXML->parse($data);
$lecture_map=$configxml[0][' childNodes'];
// print_r($lecture_map);
foreach($lecture_map as $lecture){
foreach($lecture['childNodes'] as $childNode){
$objXML = new xml2Array();
$data=implode("", file($file));
$configxml = $objXML->parse($data);
$lecture_map=$configxml[0]['
// print_r($lecture_map);
foreach($lecture_map as $lecture){
foreach($lecture['childNodes'] as $childNode){
if ($childNode['name']==" CONTROLSWIDTH")
$slide_width = $childNode['text'];
}
}
return $slide_width;
}
}
}
return $slide_width;
}
External class xml2Array class as following:
<?php
/* Usage
Grab some XML data, either from a file, URL, etc. however you want.
Assume storage in $strYourXML;
$objXML = new xml2Array();
$arrOutput = $objXML->parse($strYourXML);
print_r($arrOutput); //print it out, or do whatever!
*/
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
//var $tag_name;
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);
//$tag=array($name=>$attrs);
//$this->tag_name = $name;
array_push($this->arrOutput,$ tag);
}
function tagData($parser, $tagData) {
if(trim($tagData) != '') {
if(isset($this->arrOutput[ count($this->arrOutput)-1][' text'])) {
$this->arrOutput[count($this-> arrOutput)-1]['text'] .= $tagData;
}
else {
$this->arrOutput[count($this-> arrOutput)-1]['text'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this-> arrOutput)-2]['childNodes'][] =
$this->arrOutput[count($this-> arrOutput)-1];
array_pop($this->arrOutput);
}
}
?>
Grab some XML data, either from a file, URL, etc. however you want.
Assume storage in $strYourXML;
$objXML = new xml2Array();
$arrOutput = $objXML->parse($strYourXML);
print_r($arrOutput); //print it out, or do whatever!
*/
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
//var $tag_name;
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->
xml_set_element_handler($this-
xml_set_character_data_
$this->strXmlData = xml_parse($this->resParser,$
if(!$this->strXmlData) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_
xml_get_current_line_number($
}
xml_parser_free($this->
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"
//$tag=array($name=>$attrs);
//$this->tag_name = $name;
array_push($this->arrOutput,$
}
function tagData($parser, $tagData) {
if(trim($tagData) != '') {
if(isset($this->arrOutput[
$this->arrOutput[count($this->
}
else {
$this->arrOutput[count($this->
}
}
}
function tagClosed($parser, $name) {
$this->arrOutput[count($this->
$this->arrOutput[count($this->
array_pop($this->arrOutput);
}
}
?>
No comments:
Post a Comment