I am trying to parse a xml attribute but I am stuck..
The XML format is :
<data>
<type name="group">
<item id="1">
<sub name="title">Music/sub >
<sub name="number">1</sub >
</item>
<item id="2">
< sub name="title">Art</sub >
< sub name="number">2</sub >
</item>
</type>
<data>
The PHP looks like:
$xml_file = "group.xml";
$xml_headline_key = "*DATA*TYPE*ITEM";
$xml_description_key = "*DATA*TYPE*ITEM*SUB";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
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;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>Groups</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for ( $counter = 1; $counter <= 1; $counter += 1) {
echo "<center>";
echo "\t<h2>" . $story_array[4]->headline . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $story_array[0]->description . "</i><br>\n";
}
I am able to pull the items from the feed but I can not figure out how to display the attributes/nodes, ie: <sub name="title"><sub name="number">. I want to be able to display title or number on the page. how can I reference these?
Im guessing it will be simular to:
echo "\t<i>" . $story_array[0]->description . "</i><br>
im lost........
Thanks!
The XML format is :
<data>
<type name="group">
<item id="1">
<sub name="title">Music/sub >
<sub name="number">1</sub >
</item>
<item id="2">
< sub name="title">Art</sub >
< sub name="number">2</sub >
</item>
</type>
<data>
The PHP looks like:
$xml_file = "group.xml";
$xml_headline_key = "*DATA*TYPE*ITEM";
$xml_description_key = "*DATA*TYPE*ITEM*SUB";
$story_array = array();
$counter = 0;
class xml_story{
var $headline, $description;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
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;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, filesize($xml_file)) or die("Could not read file");
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
?>
<html>
<head>
<title>Groups</title>
</head>
<body bgcolor="#FFFFFF">
<?php
for ( $counter = 1; $counter <= 1; $counter += 1) {
echo "<center>";
echo "\t<h2>" . $story_array[4]->headline . "</h2>\n";
echo "\t\t\n";
echo "\t<i>" . $story_array[0]->description . "</i><br>\n";
}
I am able to pull the items from the feed but I can not figure out how to display the attributes/nodes, ie: <sub name="title"><sub name="number">. I want to be able to display title or number on the page. how can I reference these?
Im guessing it will be simular to:
echo "\t<i>" . $story_array[0]->description . "</i><br>
im lost........
Thanks!