Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help parsing xml attributes

Status
Not open for further replies.

webkidd

Programmer
Apr 27, 2006
1
US
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!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top