Well, it's very easy
You can either do it the old fashion way or using xml functions.
The way I do it is I perforn a loop and detect when the string <Artiste begins and the </Artiste> finishes
This is very easy to do. There are pros and cons about this, the pros is that php doesn't have to have xml extensions installed and some hosts don't so even if hosts don't have xml extension it will work, the draw back is that it's more slow than if you use the xml functions
The other way is for you to use xml function
PHP Code:
<?php
class XMLParser {
var $depth = 0;
var $parser;
var $text;
var $cdata = array();
function XMLParser() {
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($this->parser, 'startElement', 'endElement');
xml_set_character_data_handler($this->parser, 'characterData');
}
function parse($xml) {
$error = xml_parse($this->parser,$xml,true);
if ($error === false) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)));
}
$this->text = '$arr = '.substr($this->text, 0,
strlen($this->text)-2).";\n";
eval($this->text);
return $arr;
}
function startElement($parser, $name, $attrs) {
$attrStr = '';
$this->depth++;
$this->cdata[$this->depth] = '';
foreach($attrs as $key => $value) {
$attrStr .= "'$key' => '$value', ";
}
$this->text .= "array(\n'name' => '$name', 'attributes' => array($attrStr), ";
}
function endElement($parser, $name) {
$this->text .= " 'cdata' => '".$this->cdata[$this->depth]."'),\n";
$this->depth--;
}
function characterData($parser, $data) {
$this->cdata[$this->depth] .= $data;
}
}
$parser = new XMLParser();
$arr = $parser->parse(file_get_contents('sample.xml'));
print_r($arr);
?>
|