Web Hosting Talk







View Full Version : xml, attributes


getbusy
10-06-2004, 03:26 PM
Hello, I use this xml parser.. but I can't select "attributes".. any thoughts?


you will notice "while ($node = GetElementByName($data, "<article>", "</article>"))" well obviously this doesn't work since there is an attribute in the <article> thing :-), I have no idea how I can get this attribute. The parser works great without attributes though.

Thanks a lot!


[/PHP]
$file = "xmlnews.html";


function GetElementByName ($xml, $start, $end) {

global $pos;
$startpos = strpos($xml, $start);
if ($startpos === false) {
return false;
}
$endpos = strpos($xml, $end);
$endpos = $endpos+strlen($end);
$pos = $endpos;
$endpos = $endpos-$startpos;
$endpos = $endpos - strlen($end);
$tag = substr ($xml, $startpos, $endpos);
$tag = substr ($tag, strlen($start));

return $tag;

}





// Open and read xml file. You can replace this with your xml data.


$pos = 0;
$Nodes = array();

if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($getline = fread($fp, 4096)) {
$data = $data . $getline;






}

$count = 0;
$pos = 0;
$timer2="0";

// Goes throw XML file and creates an array of all <XML_TAG> tags.

while ($node = GetElementByName($data, "<article>", "</article>")) {

$Nodes[$count] = $node;
$count++;
$data = substr($data, $pos);
}

// Gets infomation from tag siblings.
for ($i=0; $i<$count; $i++) {

$title = GetElementByName($Nodes[$i], "<url>", "</url>");

echo "bla $title <br>";

}
[/PHP]




piece of xml file
<gc>
<article id="_0">
<url>http://www.x.com</url>

.......

mayonaise
10-06-2004, 04:39 PM
without looking thru your code in detail, all i can suggest is to look into using $attrs["ATTRIBUTENAME"], where "ATTRIBUTENAME" is obviously the name of the attribute you're looking for, in all caps. that should give you access to all the attributes of the current tag that is being read.

good luck!

Burhan
10-07-2004, 02:33 AM
Is there any reason that you aren't using the built-in XML parser (http://www.php.net/xml)?