/    Sign up×
Community /Pin to ProfileBookmark

PHP & XML (accessing parent attribute??)

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>[/CODE]

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);[/CODE]

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

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Stephen_PhilbinSep 07.2005 — Haven't done much XML with PHP but doing it in PHP5 can differ considerably from PHP4, how your XML implementation of your PHP module behaves also may very heavily depend on how up to date your libxml library that the PHP module is linked to is.

For the best advice, I'd go to http://www.zend.com/ and check the documentation and various articles there.
Copy linkTweet thisAlerts:
@canuck-web-guyauthorSep 07.2005 — sorry - it's version 4 PHP
Copy linkTweet thisAlerts:
@canuck-web-guyauthorSep 08.2005 — here was the error...

$link_href = $attrs["href"];

This line needs to be changed to:

$link_href = $attrs['HREF'];[/QUOTE]
Copy linkTweet thisAlerts:
@nihaOct 12.2006 — hi everyone i am very new with php xml , here is the case

my xml tags in a string. i want to access a certain attribute taht i already know by name to get its value. how can i do that by php

i am using php 5

appreciating any hint

thanx

Niha
×

Success!

Help @canuck-web-guy spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...