Re: PHP & XML Parsing
| From: | Mike Gifford | Date: | Tue, 22 May 2001 21:09:53 +0000 |
| Subject: | Re: PHP & XML Parsing | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-53928@lists.php.net to get a copy of this message | ||
Hello Fabian,
This was very helpful. Thanks for letting me know about this little glitch.
It almost resolves the problem that I was having. Now I no longer get xml errors. However, I'm not able to output the XML either.
Any suggestions would be appreciated. The updated code is available here:
http://www.airdiv-cupe.org/portal/newsfeed_new_parser.php
And I've included the full script in the email (at the very bottom of this message).
Thanks.
Mike
Fabian Raygosa wrote:
for xml you have to escape certain characters in the file, one is the ampersand ... ----- Original Message ----- From: "Mike Gifford" <mike@openconcept.on.ca> To: <php-general@lists.php.net> Sent: Friday, May 18, 2001 12:24 PM Subject: [PHP] PHP & XML Parsing<?php class xitem { var $xTitle; var $xLink; var $xDescription; // function xitem() { // } } // general vars $sTitle = "Airline Division Newsfeeds"; $sLink = "http://airdiv.cupe.ca/portal/"; $sDescription = "A Portal for the Airline Division of CUPE"; $arItems = array(); $itemCount = 0; // ********* Start User-Defined Vars ************ // rss url goes here $uFile ="http://cupe.ca/xml/cupenews.rdf"; // descriptions (true or false) goes here $bDesc = ""; // If it exists this should be 'true' // font goes here $uFont = "Verdana, Arial, Helvetica, sans-serif"; $uFontSize = "2"; // ********* End User-Defined Vars ************** function startElement($parser, $name, $attrs) { global $curTag; $curTag .= "^$name"; } function endElement($parser, $name) { global $curTag; $caret_pos = strrpos($curTag,'^'); $curTag = substr($curTag,0,$caret_pos); } function characterData($parser, $data) { global $curTag; // get the Channel information first global $sTitle, $sLink, $sDescription; // $titleKey = "^RSS^CHANNEL^TITLE"; // $linkKey = "^RSS^CHANNEL^LINK"; // $descKey = "^RSS^CHANNEL^DESCRIPTION"; $titlekey = "^rdf:RDF^channel^title"; $linkkey = "^rdf:RDF^channel^link"; $desckey = "^rdf:RDF^channel^description"; if ($curTag == $titleKey) {Hello, In looking for a good script to parse XML files I stumbled across thefollowingtutorial (which looks very good): http://www.wirelessdevnet.com/channels/wap/features/xmlcast_php.html I have set this script up here: http://www.airdiv-cupe.org/portal/newsfeed_new_parser.php and I keep getting the following error (even after making a number ofchanges):XML error: not well-formed at line 16 And I'm not sure how to troubleshoot this problem. I'm assuming that it is referring to line 16 on the XML file, in thiscase:http://cupe.ca/xml/cupenews.rdf My parsing definitions are as follows $itemTitleKey = "^rdf^item^title"; $itemLinkKey = "^rdf^item^link"; $itemDescKey = "^rdf^item^description"; Any help would be appreciated. Mike -- Mike Gifford, OpenConcept Consulting, http://openconcept.ca Offering everything your organization needs for an effective web site. Featured Client: http://halifaxinitiative.org A good life is one inspired by love and guided by knowledge - B. Russell -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net For additional commands, e-mail: php-general-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net
$sTitle = $data;
echo $sTitle;
}
elseif ($curTag == $linkKey) {
$sLink = $data;
echo $sLink;
}
elseif ($curTag == $descKey) {
$sDescription = $data;} // now get the items global $arItems, $itemCount; // $itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE"; // $itemLinkKey = "^RSS^CHANNEL^ITEM^LINK"; // $itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION"; $itemtitlekey = "^rdf:RDF^item^title"; $itemlinkkey = "^rdf:RDF^item^link"; $itemdesckey = "^rdf:RDF^item^description"; if ($curTag == $itemTitleKey) {
// make new xitem
$arItems[$itemCount] = new xitem(); // set new item object's properties
$arItems[$itemCount]->xTitle = $data;
echo $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
echo $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
// increment item counter
$itemCount++;
echo $data;
}
} // main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
$data=eregi_replace( "&", "&",$data);
echo $data;
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser); // write out the items
?>
<html>
<head>
<title><?php echo ($sTitle); ?></title>
<meta name = "description" content = "<?php echo ($sDescription); ?>">
</head>
<body bgcolor = "#FFFFFF">
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($sLink); ?>"><?php echo($sTitle); ?></a></font>
<br>
<br>
<?php
for ($i=0;$i<count($arItems);$i++) {
$txItem = $arItems[$i];
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($txItem->xLink); ?>"><?php echo($txItem->xTitle); ?></a></font>
<br>
<?php
if ($bDesc) {
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><?php echo ($txItem->xDescription); ?>
<br>
<?php
}
echo ("<br>");
}
?>
</body>
</html>
--
Mike Gifford, OpenConcept Consulting, http://openconcept.ca
Offering everything your organization needs for an effective web site.
Featured Client: http://halifaxinitiative.org
A good life is one inspired by love and guided by knowledge - B. Russell