hello all…
I am having a heck of a time getting at a attribute of a parent in my XML from PHP.
here is a sample of the XML:
[CODE]<result href=”http://www.icbc.com/Licensing/index.html”>
<title>ICBC – Driver licensing</title>
<score>54%</score>
<size>33.3K</size>
<summary>This section of icbc.com contains all necessary information about driver licensing in British Columbia.</summary>
<date>2005-05-20T18:00:09Z</date>
<indexed>2005-05-25T03:07:34Z</indexed>
<flags>2</flags>
<nlinks>1</nlinks>
<quality>18</quality>
<publisher></publisher>
<keywords>best hit, test best, motor vehicle,
road test, graduate license, terrain)act, vehicle all, all terrain, drive record, vehicle driver, driver service, b.c.driver</keywords>
<in_collection>bcgovt</in_collection>
<term><text>test</text><frequency>25.0</frequency></term>
</result>
I have no issues accsing the children information…
FYI: Here is what i am using to access the XML via PHP
[CODE]$strSearchTerm = $_POST[“qt”];
$insideitem = false;
$link_href = “”;
$tag = “”;
$title = “”;
$description = “”;
$link = “”;
$score = “”;
$size = “”;
$summary = “”;
$date = “”;
$indexed = “”;
$flags = “”;
$nlinks = “”;
$quality = “”;
$publisher = “”;
$keywords = “”;
$in_collection = “”;
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $link_href, $title, $highlight, $description, $link, $score, $size, $summary, $date, $indexed, $flags, $nlinks, $quality, $publisher, $keywords, $in_collection, $term, $text, $frequency;
if ($insideitem) {
$tag = $name;
} elseif ($name == “RESULT”) {
$link_href = $attrs[“href”];
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $link_href, $title, $highlight, $description, $link, $score, $size, $summary, $date, $indexed, $flags, $nlinks, $quality, $publisher, $keywords, $in_collection, $term, $text, $frequency;
if ($name == “RESULT”) {
printf(“<ul><li><b>URL: <a href=’%s’>%s</a></b></li>”,htmlspecialchars(trim($link_href)),htmlspecialchars(trim($link_href)));
printf(“<li><b>Title:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($title)));
printf(“<li><b>Score:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($score)));
printf(“<li><b>Size:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($size)));
printf(“<li><b>Summary:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($summary)));
printf(“<li><b>Date:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($date)));
printf(“<li><b>Date Indexed:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($indexed)));
printf(“<li><b>Flags:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($flags)));
printf(“<li><b>Number of Links:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($nlinks)));
printf(“<li><b>Quality:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($quality)));
// – commented out because data contains no content – printf(“<li><b>%s</li>”,htmlspecialchars(trim($publisher)));
printf(“<li><b>Keywords:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($keywords)));
printf(“<li><b>Collection:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($in_collection)));
printf(“<li><b>Search Term:</b><font color=#3300CC> %s</font></li>”,htmlspecialchars(trim($text)));
printf(“<li><b>Frequency:</b><font color=#3300CC> %s</font></li></ul><hr size=1 color=#999999 noshade />”,htmlspecialchars(trim($frequency)));
$link_href = “”;
$title = “”;
$description = “”;
$link = “”;
$score = “”;
$size = “”;
$summary = “”;
$date = “”;
$indexed = “”;
$flags = “”;
$nlinks = “”;
$quality = “”;
$publisher = “”;
$keywords = “”;
$in_collection = “”;
$term = “”;
$text = “”;
$frequency = “”;
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $link_href, $title, $highlight, $description, $link, $score, $size, $summary, $date, $indexed, $flags, $nlinks, $quality, $publisher, $keywords, $in_collection, $term, $text, $frequency;
if ($insideitem) {
switch ($tag) {
// $href = $tag[“href”]
//case “href”:
// $href .= $data
//break;
case “TITLE”:
$title .= $data;
case “HIGHLIGHT”:
$highlight .= $data;
break;
case “DESCRIPTION”:
$description .= $data;
break;
case “LINK”:
$link .= $data;
break;
case ‘SCORE’:
$score .= $data;
break;
case ‘SIZE’:
$size .= $data;
break;
case ‘SUMMARY’:
$summary .= $data;
break;
case ‘DATE’:
$date .= $data;
break;
case ‘INDEXED’:
$indexed .= $data;
break;
case ‘FLAGS’:
$flags .= $data;
break;
case ‘NLINKS’:
$nlinks .= $data;
break;
case ‘QUALITY’:
$quality .= $data;
break;
case ‘PUBLISHER’:
$publisher .= $data;
break;
case ‘KEYWORDS’:
$keywords .= $data;
break;
case ‘IN_COLLECTION’:
$in_collection .= $data;
break;
case ‘TERM’:
$term .= $data;
case ‘TEXT’:
$text .= $data;
break;
case ‘FREQUENCY’:
$frequency .= $data;
break;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, “startElement”, “endElement”);
xml_set_character_data_handler($xml_parser, “characterData”);
// build the query for searchfeed
// Parse type
// XML (which is what this parser was built to read)
$strQuery = “http://[domain]/[page].xml?”;
$strQuery .= “qt=” . $_POST[“qt”] . “&”;
$strQuery .= “charset=iso-8859-1”;
$fp = fopen($strQuery, “r”)
or die(“Error reading RSS data.”);
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf(“XML error: %s at line %d”,
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
xml_parser_free($xml_parser);
It works fine as it is right now – but i need to get at the only attribute of the parent in the XML (href).
Hoping someone can help me with this one. Google turned up nothing as did my PHP books. they all seem to cover children attributes – not parent attributes.
Thanks. ?