Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

help parsing xml attributes with php

help parsing xml attributes with php

12:07 pm on Apr 27, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 16, 2005
votes: 0

I am trying to parse a xml attribute but I am stuck..

The XML format is :

<type name="group">

<item id="1">
<sub name="title">Music/sub >
<sub name="number">1</sub >

<item id="2">
< sub name="title">Art</sub >
< sub name="number">2</sub >



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;
case $xml_headline_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->headline = $data;
case $xml_description_key:
$story_array[$counter]->description = $data;

$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));




<body bgcolor="#FFFFFF">

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........


6:48 pm on May 5, 2006 (gmt 0)

Full Member

10+ Year Member

joined:July 7, 2003
votes: 0

I don't know which parser are you using (the one that you refer as $xml_parser), but if it doesn't gives you good tools to navigate & parse your document, then it's useless.

I'll give you some lines to use the XML-DOM that comes with most PHP distributions, because it's the one I best know of, and it would allow you to save a lot of work (based on the code you posted).
You can find the full documentation in the php net site (http://www.php.net/domxml).
The simplest way to work would be start with a call:
$xfile = domxml_open_file("group.xml");
which would load the file into memory in a structured way and return that structure to $xfile. Then, you may use this functions to navigate and read the document:
$pointer = $xfile->document_element; // $pointer points to <data>
$pointer = $pointer->first_child(); //$pointer points to <type>
$pointer = $pointer->first_child(); //points to the first <item>
while ($pointer) { //will break when $pointer is null
$itemid = $pointer->get_attribute_node("id")->value(); //$itemid is the id attribute of current item
$sub = $pointer->first_child();
while ($sub){
$sn = $sub->get_attribute_node("name")->value();
echo sprintf("%s: %s\n", $sn, $sub->node_value());
$sub = $sub->next_sibling();
$pointer = $pointer->next_sibling(); //points to next item, or null if no more items
That would output all the content of the xml. I hope you'll be able to add any additional formatting, filtering or processing, which is beyond xml, you'll be able to solve it by yourself. Anyhow, if you need further help, let me know either by PM or reply.
Please note that I've put this code looking at the reference but without testing it; and without the aid of any specialized tool (such as editor w/ syntax highlight). For these reasons, I apologize for any possible error on it.

Hoping be useful,


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members