Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_php thread: XML to PHP


Message #1 by "vidya" <vidya_kgg@y...> on Wed, 19 Dec 2001 11:37:10
Hi Vidia,

I've seen your posts about this on every Wrox PHP mailing list several times
recently, hopefully I can be of help.

I don't think you are using the XML parser correctly.

__YOU__ need to define what happens when the xml file is read.  Your xml
parser will only signal to you when certain things happen in a file.  It
will NOT create some data structure which holds your xml data.  YOU need to
define how to do that.

The xml parser will read through the file and just let you know when it
finds an open tag, finds an attribute, finds an end tag, etc.

These lines from your code

  xml_set_element_handler($xml_parser, "Data", "Data");
  xml_set_character_data_handler($xml_parser, "Resultcode");

look wrong.

What you're saying in the first line is that the xml parser will call a
function "Data()" when it finds both an open AND end tag while it reads your
xml file.  This doesn't seem right.

I don't think you'd have the same function handle open tags and end tags
together (though it IS entirely possible.)

What you're saying in the second line is that your XML parser will send all
of the text found within tags to a function called "Resultcode()".

For example, consider this XML text:

  <tagname attr1="attr1value" attr2="attr2value">tag text</tagname>

The xml parser will do the following when it parses it, in this order.

1)  Call your open tag function with three arguments:
    The parser object,
    A string: "tagname",
    An array:  ("attr1" => "attr1value", "attr2"=>"attr2value")

2)  Call your character data function with two arguments:
    The parser object,
    A string:  "tag text"

3)  Call your end tag function with two arguments:
    The parser object,
    A string:  "tagname"


You should read through the XML documentation on php.net.  Using an XML
parser seems a lot like using custom session handlers...  You write the
functions, and some other object calls them when they encounter specific
events.


Take a look at the code at the bottom of this letter.  It should be
sufficient to get you started.  I just whipped it up really quickly just now
and you really should figure out what's going on and how to improve it.

For now, you can use it and access $xml_tags["ProductID"] and
$xml_tags["stock"].



<?php

$current_tag;
$xml_tags;

// Simple open tag.  Assuming no tag nesting and no attributes.
function open_tag($parser, $tag, $attributes)
{
    global $current_tag;

    $current_tag = "$tag";
}

// Simple cdata.  I think I read somewhere that $text is
// at most 1024 characters, so we use the append operator
// to insert text, not a simple assignment, which would
// overwrite it.
function char_data($parser, $text)
{
    global $current_tag;
    global $xml_tags;

    $xml_tags[$current_tag] .= "$text";
}

// Simple end tag.
function end_tag($parser, $tag)
{
    global $current_tag;

    $tag_depth--;
}

$xml_parser = xml_parser_create();

xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
xml_set_element_handler($xml_parser, "open_tag", "end_tag");
xml_set_character_data_handler($xml_parser, "char_data");

$xmltext = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" .
           "<Data>\n<ResultCode>4525</ResultCode>\n" .
           "<Result>ProductID not found</Result>\n" .
           "<ProductID>1000</ProductID>\n<Sort></Sort>\n" .

"<Category></Category>\n<Title></Title>\n<Stock>0</Stock>\n</Data>";


xml_parse($xml_parser, $xmltext);

print_r($xml_tags);
?>


If there's any function called that you don't understand, read the
documentation about it.  There's a LOT of examples on php.net.

Take care,

Nik


  Return to Index