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