Click to See Complete Forum and Search --> : XML Parsing Attributes , Please help


subeeshkk
11-28-2008, 04:06 AM
Hi all,

I want to parse an XML file which contains many set of data including attributes for some tags. I need to retrieve these data based on a tag name and this array returned has to be used for insertion into table.

Following I am posting my code

********************************************
<?php

require_once("class.XMLParser.php");

$xml = new XMLParser();

$results = $xml->readItems('squads.xml','Player');

foreach($results as $player) {

$names = $player->getElementsByTagName( "Name" );
$name = $names->item(0)->nodeValue;

$positions = $player->getElementsByTagName( "Position" );
$position = $positions->item(0)->nodeValue;

$stats = $player->getElementsByTagName( "Stat" );
$stat = $stats->item(0)->nodeValue;

echo "$name - $position - $stat ".'<br>';
}

?>

Parsing - class.XMLParser.php
**************************************************************

<?php

class XMLParser {

private $fileName;

private $tagName;

private $readResult;


function __construct() {
}


function readItems($file_name,$tag_name) {

$doc = new DOMDocument();

$this->fileName = $file_name;

$this->tagName = $tag_name;

$doc->load( $this->fileName );

$this->readResult= $doc->getElementsByTagName( $this->tagName );

return $this->readResult;

}

}
?>


XML File sample - squads.xml
***************************************************************
<Team>
<Player uID="p12450">

<Name>Kolo Toure</Name>

<Position>Defender</Position>

<Stat Type="first_name">Kolo</Stat>

<Stat Type="last_name">Toure</Stat>

<Stat Type="known_name"></Stat>

<Stat Type="birth_date">1981-03-19</Stat>

<Stat Type="weight">74</Stat>

<Stat Type="height">180</Stat>

<Stat Type="jersey_num">5</Stat>

<Stat Type="real_position">Centre Back</Stat>

</Player>

<Player uID="p17336">

<Name>Gaël Clichy</Name>

<Position>Defender</Position>

<Stat Type="first_name">Gaël</Stat>

<Stat Type="last_name">Clichy</Stat>

<Stat Type="known_name"></Stat>

<Stat Type="birth_date">1985-07-26</Stat>

<Stat Type="weight">72</Stat>

<Stat Type="height">181</Stat>

<Stat Type="jersey_num">22</Stat>

<Stat Type="real_position">Left Back</Stat>

</Player>
</Team>


Here, the "Stat" tag has some attributes "Typ"e repeating, I need to read this, For example,If I pass the attribute "Player", the array should contain all things including the attributes for me to insert.. currently with my code it only returns first "Stat" value.

Please help me, Thanks for your time,

Subeesh

rpgfan3233
11-28-2008, 05:26 AM
Since you're using the DOM, you can just use the attributes property of each DOMElement. This property is inherited from DOMNode, so almost any DOM class should have it.

Here's an example:
$xmlstr = "<hello google="search" foo="bar">\n\t<subHello band="music"/>\n</hello>";
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false; //Normally this shouldn't be false without a good reason.
$doc->loadXML($xmlstr) or die("Couldn't load XML document!");
if ($doc->documentElement->hasAttributes())
{
foreach ($doc->documentElement->attributes as $attribute)
{
echo $attribute->nodeName, '="', $attribute->nodeValue, '"<br>';
}
}
foreach ($doc->documentElement->childNodes as $childnode)
{
if ($childnode->hasAttributes())
{
foreach ($childnode->attributes as $attribute)
{
echo $attribute->nodeName, '="', $attribute->nodeValue, '"<br>';
}
}
}

I hope this helps!