Hi. I am trying to parse XML data using PHP. I have site that will have product shots and with those images there will be a title and a description. What I want is to have one XML file that gets parsed and just the info for that data will be displayed within DIV and have CSS applied for formatting. So when someone clicks product 1 for example, the product 1 image will appear and the title and description will appear in that DIV. Here is a link to the test page so you can get a better idea:

http://www.williamverbist.com/_ethno/ethnic_spring.php

I found this tutorial on kirupa.com - http://www.kirupa.com/web/xml_php_parse_intermediate.htm. I think this is very close to what I want to do, but I am new to XML/PHP, so I really do not understand how to break out this data. I am pasting in the code in case you don't feel like looking at the link:

PHP
<?php

// Simple enough, the location of the XML file
$xml_file = "xml_intermediate.xml";

// These are both keys that we will use later.
$xml_headline_key = "NEWSSTORYHEADLINE";
$xml_description_key = "
NEWSSTORYDESCRIPTION";

// An array for storing our information. An array is nice to use here
// because it allows us to parse the XML and then temporarily forget about it
// allowing use greater freedom to edit and maniplulate the output.
$story_array = array();

// A counter that will come into use later.
$counter = 0;

// A simple class that will make our life easier. We could use an
// associative array as well, but I prefer to just write up the class. =)
class xml_story{
var $headline, $description;
}

// Once again, this is what we want our parser to do when it reaches a start tag
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}

// Same thing here as well. This tells the parser what to do when it trips over an end tag.
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}

// When the parser hits the contents of the tags it will perform this function.
// This will all be explained word for word in the tutorial
function contents($parser, $data){
global $current_tag, $xml_headline_key, $xml_description_key, $counter, $story_array;
switch($current_tag){
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
break;
case $xml_description_key:
$story_array[$counter]->description = $data;
$counter++;
break;
}
}

// Creates the parser
$xml_parser = xml_parser_create();

// Sets the element handlers for the start and end tags
xml_set_element_handler($xml_parser, "startTag", "endTag");

// Sets the data handler, same as before...
xml_set_character_data_handler($xml_parser, "contents");

// Opens the file or gives an error message
$fp = fopen($xml_file, "r") or die("Could not open file");

// Reads the file or gives an error message
$data = fread($fp, filesize($xml_file)) or die("Could not read file");

// This if statement is exactly the same as before. It parses the xml document
// according to the functions we have defined; and it returns an error message
// if the parsing fails
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}

// Frees up the memory
xml_parser_free($xml_parser);

// Closes the file
fclose($fp);

?>

<html>
<head>
<title>CNT HEADLINE NEWS</title>
</head>
<body bgcolor="#FFFFFF">
<?
// A simple for loop that outputs our final data.
for($x=0;$x<count($story_array);$x++){
echo "\t<h2>" . $story_array[$x]->headline . "</h2>\n";
echo "\t\t<br />\n";
echo "\t<i>" . $story_array[$x]->description . "</i>\n";
}
?>

</body>
</html>


XML

<?xml version="1.0"?>
<news>
<story>
<headline>Godzilla Attacks LA!</headline>
<description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>
</story>
<story>
<headline>Phil Jayhan's Death Star Update</headline>
<description>After years of contracting set-backs and searching for hard-to-get parts, Mr. Jayhan is nearing completion on his world-destroying Death-Star. We are all looking forward to the future enslaving and erradication of the human race. </description>
</story>
<story>
<headline>Bigfoot Spoted at M.I.T. Dining Area</headline>
<description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing.</description>
</story>
<story>
<headline>London Angel Saves England</headline>
<description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her.</description>
</story><story>
<headline>Six-eyed Man to be Wed to an Eight-armed Woman</headline>
<description>Uhhhmmm... No comment really... just a little creepy to see them together...</description>
</story>
<story>
<headline>Ahmed's Birthday Extravaganza!</headline>
<description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides.</description>
</story>

</news>

Any help would be appreciated. Thanks in advance.

    Well you have 3 easy choices.

    1. SimpleXML in php 5
    2. Use XPATH - in php 4
    3. Use XSLT in 4 or 5 : I use it a lot but won't go into it.

    XML can be a bit of a murky world in 4. A lot of things changed.

    php4
    http://uk.php.net/manual/en/function.xpath-eval.php

    php5http://uk.php.net/manual/en/ref.simplexml.php

    
    //PHP 4 example as it's a bit of a pain to nail down from the documentation. I am also using a 4 wrapper in 5 for backwards compatability but never had any problems moving from 4 to 5 using the following
    
    $xml = <<<XML
    <?xml version="1.0"?>
    <news>
    <story>
    <headline>Godzilla Attacks LA!</headline>
    <description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>
    </story>
    <story>
    <headline>Phil Jayhan's Death Star Update</headline>
    <description>After years of contracting set-backs and searching for hard-to-get parts, Mr. Jayhan is nearing completion on his world-destroying Death-Star. We are all looking forward to the future enslaving and erradication of the human race. </description>
    </story>
    <story>
    <headline>Bigfoot Spoted at M.I.T. Dining Area</headline>
    <description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing.</description>
    </story>
    <story>
    <headline>London Angel Saves England</headline>
    <description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her.</description>
    </story><story>
    <headline>Six-eyed Man to be Wed to an Eight-armed Woman</headline>
    <description>Uhhhmmm... No comment really... just a little creepy to see them together...</description>
    </story>
    <story>
    <headline>Ahmed's Birthday Extravaganza!</headline>
    <description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides.</description>
    </story>
    
    </news>
    XML;
    
    //php 4 method if you don't have 5.. 
    
    $doc     = domxml_open_mem($xml);
    $xpath   = xpath_new_context( $doc );
    $nodes   = $xpath->xpath_eval('/news/story'); 
    
    if( is_object( $nodes ) )
    {
        foreach( $nodes->nodeset AS $node )
        {
            $storyXML   = $doc->dump_node($node);  
    //echo $storyXML;die(); $storyXPath = xpath_new_context( domxml_open_mem( $storyXML ) ); $title = GetTextValue( $storyXPath, '/story/headline/text()' ); $description = GetTextValue( $storyXPath, '/story/description/text()' ); var_dump( $title ); var_dump( $description ); echo "\n\n\n<br/><br/><br/>"; } } function GetTextValue( $xpathObject , $xpath_equation ) { $node_set = $xpathObject->xpath_eval($xpath_equation); if( !isset($node_set->nodeset[0] ) ) return false; return $node_set->nodeset[0]->content; }
      Write a Reply...