XML Element Structure Example

This first example displays the structure of the start elements in a document with indentation.

Example #1 Show XML Element Structure

<?php
$file
= "data.xml";
$depth = 0;

function
startElement($parser, $name, $attrs)
{
global
$depth;

for (
$i = 0; $i < $depth; $i++) {
echo
" ";
}
echo
"$name\n";
$depth++;
}

function
endElement($parser, $name)
{
global
$depth;
$depth--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
if (!(
$fp = fopen($file, "r"))) {
die(
"could not open XML input");
}

while (
$data = fread($fp, 4096)) {
if (!
xml_parse($xml_parser, $data, feof($fp))) {
die(
sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>

add a note add a note

User Contributed Notes 3 notes

up
1
Joachim Kruyswijk
2 years ago
If you use strict_types=1, you'll get the warning "Notice: Resource ID#... used as offset, casting to integer".

Use

<?php $depth[(int) $parser?>

instead of

<?php $depth[$parser?>
up
-7
roan at horning dot us
15 years ago
The following example will echo back an xml file using the basic event based parser.  Currently, (at least in the version of PHP5 I'm using) it will skip The XML declaration and the Doctype declaration--they don't seem to be captured by the default handler.

<?php
echo "<pre>";

$file = "test.xml";
echo
$file."\n";
global
$inTag;

$inTag = "";
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);
xml_set_processing_instruction_handler($xml_parser, "pi_handler");
xml_set_default_handler($xml_parser, "parseDEFAULT");
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "contents");

if (!(
$fp = fopen($file, "r"))) {
    if (!
xml_parse($xml_parser, $data, feof($fp))) {
       die(
sprintf("XML error: %s at line %d",
                           
xml_error_string(xml_get_error_code($xml_parser)),
                           
xml_get_current_line_number($xml_parser)));
    }
}
while (
$data = fread($fp, 4096)) {
    if (!
xml_parse($xml_parser, $data, feof($fp))) {
       die(
sprintf("XML error: %s at line %d",
                           
xml_error_string(xml_get_error_code($xml_parser)),
                           
xml_get_current_line_number($xml_parser)));
    }
}
xml_parser_free($xml_parser);

function
startElement($parser, $name, $attrs) {

    global
$inTag;
    global
$depth;
       
   
$padTag = str_repeat(str_pad(" ", 3), $depth);

    if (!(
$inTag == "")) {
        echo
"&gt;";
    }
    echo
"\n$padTag&lt;$name";
    foreach (
$attrs as $key => $value) {
        echo
"\n$padTag".str_pad(" ", 3);
        echo
" $key=\"$value\"";
    }
   
$inTag = $name;
   
$depth++;
}

function
endElement($parser, $name) {

    global
$depth;
   global
$inTag;
    global
$closeTag;
       
   
$depth--;

   if (
$closeTag == TRUE) {
       echo
"&lt/$name&gt;";
      
$inTag = "";
   } elseif (
$inTag == $name) {
       echo
" /&gt;";
      
$inTag = "";
   } else {
        
$padTag = str_repeat(str_pad(" ", 3), $depth);
       echo
"\n$padTag&lt/$name&gt;";
    } 
}
 
function
contents($parser, $data) {

    global
$closeTag;
   
   
$data = preg_replace("/^\s+/", "", $data);
   
$data = preg_replace("/\s+$/", "", $data);

    if (!(
$data == ""))  {
        echo
"&gt;$data";
       
$closeTag = TRUE;
    } else {
       
$closeTag = FALSE;
     }
}

function
parseDEFAULT($parser, $data) {
   
   
$data = preg_replace("/</", "&lt;", $data);
   
$data = preg_replace("/>/", "&gt;", $data);
    echo
$data;
}

function
pi_handler($parser, $target, $data) {

    echo
"&lt;?$target $data?&gt;\n";
}
echo
"</pre>";
?>
up
-23
Sample at hotmail dot com
11 years ago
<?php

public static function xml2array($element, $arr = array())
    {
        if(
is_string($element))
        {
           
$element = (strlen($element) > 5 && substr($element, -4) === '.xml')
                            ?
simplexml_load_file(DATAPATH.$element)
                            :
simplexml_load_string($element);
        }
        
$iter = 0;
        foreach(
$element->children() as $b)
        {
           
$a = $b->getName();
            if(!
$b->children()){
                   
$arr[$a] = trim($b[0]);
            }
            else{
                   
$arr[$a][$iter] = array();
                   
$arr[$a][$iter] = self::xml2array($b,$arr[$a][$iter]);
            }
               
$iter++;
        }
        return
$arr;
    }
}

?>
To Top