Re: PHP & XML Parsing

From: Date: Tue, 22 May 2001 21:16:08 +0000
Subject: Re: PHP & XML Parsing
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-53932@lists.php.net to get a copy of this message
Hello Peter, Peter Dudley wrote:
The problem is in your link URL, where you pass CGI parameters. When XML sees the & character, it assumes it's a special character thing such as & or ", so it's expecting a semicolon.
replacing it with & fixed the XML error. Thanks!
<link>http://cupe.ca/news/cupenews/showitem.asp?ID=2823&cl=1</link> Just yesterday I discovered a program called XML Spy which you can get on a 30-day eval from www.tucows.com. This was what pointed out the error to me in your file.
Interesting.. So to be consistent with proper XML formatting, should the server hosting the XML file convert the & into &amp; ?
When I was using XSL last year for the first time, I had a devil of a time figuring out how to get URLs, particularly with query strings attached, through the XML parsers. Try using the %codes in your URLs instead of & and other special characters; that should help, if I remember correctly. (Similarly, building HREF tags using XSL stylesheets seemed pretty awkward, but I'm sure I was missing some crucial tidbit of information.)
I certainly feel like I'm missing something... Although I'm a bit at odds as to how to troubleshoot this new file. I no longer get the errors, but I'm not getting the results I'm looking for either. The updated script is running here: http://www.airdiv-cupe.org/portal/newsfeed_new_parser.php I've also attached the full script (since I've made some changes to it). Any additional help would be appreciated. Mike
"Mike Gifford" <mike@openconcept.on.ca> wrote in message news:3B057708.3090903@openconcept.on.ca...
Hello, In looking for a good script to parse XML files I stumbled across the
following
tutorial (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 of
changes):
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 this
case:
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
<?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) {
    $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( "&", "&amp;",$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

« previous php.general (#53932) next »