homepage Welcome to WebmasterWorld Guest from 23.21.9.44
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
help parsing xml attributes with php
help parsing xml attributes with php
websteve




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

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!

 

Herenvardo




msg:701359
 6:48 pm on May 5, 2006 (gmt 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,
Herenvardö

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved