Jump to content


XML parsing help

  • Please log in to reply
No replies to this topic

#1 jimbob26

  • Members
  • Pip
  • Newbie
  • 3 posts

Posted 19 April 2006 - 04:16 PM

Hello, I currently have this XML file for images:
<?xml version="1.0"?>
    <image src="a.png">
        <description>Jim a</description>
    <image src="b.png">
        <description>Jim b</description>

And also have this php file:

$open_stack = array();
$parser = xml_parser_create();
xml_parse($parser,implode("",file("images.xml"))) or die(format_error($parser));
function start_handler($p,$name,$atts) {
    global $open_stack;
    $open_stack[] = array($name,"");
function character_handler($p,$txt) {
    global $open_stack;
    $cur_index = count($open_stack)-1;
    $open_stack[$cur_index][1] .= $txt;
function end_handler($p,$name) {
    global $open_stack;
    $el = array_pop($open_stack);
    if($name=="name") {
        print "<b>$el[1]</b>";
    if($name=="description") {
        print "<i>$el[1]</i><br>";
function format_error($p) {
    $code = xml_error_code($p);
    $str = xml_error_string($code);
    $line = xml_get_current_line_number($p);
    return "XML ERROR ($code): $str at line $line";

This works fine but im not sure how to extract the src attribute from the image tag (<image src="a.png">). Everything else is working fine, both the name and description of the file is being outputted correctly.

Any help is greatly appreciated.
Thanks in advance, jim

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users