PHP Classes

How to get Attributes?

Recommend this page to a friend!

      Generic XML parser class  >  All threads  >  How to get Attributes?  >  (Un) Subscribe thread alerts  
Subject:How to get Attributes?
Summary:It isn't obvious on how to get attributes.
Messages:13
Author:steven shiflett
Date:2004-12-21 20:40:23
Update:2009-05-28 09:00:49
 
  1 - 10   11 - 13  

  1. How to get Attributes?   Reply   Report abuse  
Picture of steven shiflett steven shiflett - 2004-12-21 20:40:23
Has anyone done this using this generic parser?

  2. Re: How to get Attributes?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2004-12-22 01:46:11 - In reply to message 1 from steven shiflett
Each XML tag is returned in a node of the array structure that the parser builds with the XML document definition.

Each node is represented by a string that identifies the path of the tag in the document.

That path string should be used to access the XML tag definition. That definition is stored also as an associative array. That array element with the name Attributes contains another associative array with all the names and values of the attributes.

Here is an example of script that parses the example.xml file and if successful it dumps the array attributes of the XML document root tag, if there are any.

$file_name="example.xml";
$error=XMLParseFile($parser,$file_name,1);
if(strlen($error))
echo "Parser error: $error\n";
else
{
/* path of the XML tag node that you want examine, "0" is the root tag */
$path="0";

/* dump the associative array of attributes */
var_dump($parser->structure[$path]["Attributes"]);
}

  3. Re: How to get Attributes?   Reply   Report abuse  
Picture of Kaurov Eugene Kaurov Eugene - 2004-12-27 07:54:29 - In reply to message 1 from steven shiflett
I use xmllib (in my oun edition), becouse:
1)This class not need DomXml,
2)Becouse class is fast end text
3)support any encoding -- it's work as text' parser
4)It parse very hard structures of XML, forexample http://currency.trader.in.ua/export.php or http://ingvar.com.ua/settings/export_yml.php
5) I'm not author of this script, but I use it and clean bugs = I take almost 4 hours to understood class code.

This is examlpe for parsing:
import_nbu("http://currency.trader.in.ua/export.php");
function import_nbu($yml_file)
{
global $currency_array;
$xml = new xmlParser($yml_file);
$doc = $xml->getDocument();

//get <yml_currencies>
if ($doc->nodeName!='yml_currencies'){echo "А где заглавие? Ошибка."; return false;}

//check for date
$last_update = "-1 day";
$yml_date=$doc->attributes["date"];
if (($yml_date=='')||(strtotime($yml_date)<strtotime($last_update))){echo "<br>Устаревший yml-документ либо нарушена дата его генерации"; return false;}
$nbu = $doc->firstChild('nbu');

if ($nbu) {
$source=$nbu->firstChild('source');
if ($source) $source=strtolower(trim($source->nodeValue()));
if ($source!='http://currency.trader.in.ua') {echo "Недопустимый источник."; return false;}
$currencies=$nbu->firstChild('currencies');
if ($currencies){
foreach ($currencies->childNodes() as $currency)
{
$name=$currency->getAttribute("code");
$code=$currency->getAttribute("num");
if (get_nbu($name)&&$code==get_nbu($name))
{
$value=$currency->getAttribute("value");
$nbu_tmp=@mysql_result(mysql_query("SELECT count(id) FROM nbu_course WHERE code='$code' AND name='$name'"),0);
if ($nbu_tmp>0)
mysql_query("UPDATE nbu_course SET (value='$value') WHERE code='$code' AND name='$name'");
else
mysql_query ("INSERT INTO nbu_course (code, name, value) VALUES ('$code', '$name', '$value')");
}
}
}
}
}


function get_nbu($name)
{
global $currency_array;
for ($i=1;$i<=count($currency_array);$i++)
{
if ($currency_array[$i]["code"]==$name) return $currency_array[$i]["id"];
}
return false;
}

  4. Re: How to get Attributes?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2004-12-27 17:16:18 - In reply to message 3 from Kaurov Eugene
Kaurov,

In case you have not noticed, this is the forum just for the class named "Generic XML parser class", not really about all XML parser classes.

Anyway, this Generic XML parser class does not require DOM extensions either. It uses the Expat parser that comes with all PHP distributions since PHP 3.

It provides means to cache the resulting parsed XML document. This provides an huge speedup if you need to parse the same XML document repeatedly.

It does not store document nodes as objects, but rather in arrays. This is good in terms of speed and memory because objects tend to waste more memory and take more time to create. If you have many nodes in a XML document, this detail may make an huge difference.

  5. Re: How to get Attributes?   Reply   Report abuse  
Picture of Kaurov Eugene Kaurov Eugene - 2004-12-28 06:49:21 - In reply to message 4 from Manuel Lemos
:)
Excuse me boss.
Kaurov Eugene.

  6. Re: How to get Attributes?   Reply   Report abuse  
Picture of kenatm kenatm - 2006-12-15 16:04:20 - In reply to message 2 from Manuel Lemos
this generic xml class is absolutelly STUPID:)
what does this mean?

[124,8,2773]<t>[124,11,2776]Cloudy</t>
there is no way you can access the damn values in a human readable way..
either attac documentation either remote it.
PHPclasses.org isn't recycle bin....

thank you

  7. Re: How to get Attributes?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-12-15 17:13:35 - In reply to message 6 from kenatm
If you would like to be clarified or obtain documentation, there are plenty of educated ways to request it without being rude.

A class is not stupid just because you do not understand how it works.

FYI, this class is actually used by PHPClasses site code.

The numbers that you see in the path string are the number of the parent nodes of a given node of the XML document. Now that you know what it means, you can see that it is readable to by any human.

  8. Re: How to get Attributes?   Reply   Report abuse  
Picture of Ivan Ivanov Ivan Ivanov - 2007-03-05 11:48:52 - In reply to message 7 from Manuel Lemos
Hi
Your Class looks good. But I realy can not understanbd how to use an information froom XML file. May I use same like $asoc_array[tag1][tag2].
A can get this whit print_r function. So have you some documentation of its functionality.
And one more question: can I parce a XML from URL

  9. Re: How to get Attributes?   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2007-03-05 17:49:04 - In reply to message 8 from Ivan Ivanov
The class builds a single associative array with all tags and data in the document.

The root tag has index "0". The child tags have indexes like "0,0", "0,1", "0,2", etc..

You can parse any XML document that can be opened as a file. So if you specify a XML document with a path like http://www.remotesite.com/uri , it will open that URL and parse it as if it was a local XML file.

  10. Re: How to get Attributes?   Reply   Report abuse  
Picture of posin posin - 2009-05-28 05:37:25 - In reply to message 9 from Manuel Lemos
sir , i am using your class . I am not able to fetch attributes from each node. what to do Please suggest . thanks !

 
  1 - 10   11 - 13