Jump to content

tcwadams

New Members
  • Posts

    4
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

tcwadams's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I hope i'm not too late to help you! I had a real problem with XML parsing myself, but i think i've got everything you need to get the data out of the XML in this script: function unhtmlspecialchars( $string ) { $string=preg_replace("/(&#)([0-9]*)(;)/esiU","chr(intval('\\2'))",$string); $string = str_replace ( '&', '&', $string ); $string = str_replace ( '&#039;', '\'', $string ); $string = str_replace ( '"', '\"', $string ); $string = str_replace ( '<', '<', $string ); $string = str_replace ( '>', '>', $string ); return $string; } //'" Highlighting fix function checkamend($xmlitem , $count , $data){ //Deals with the parser breaking up a tag into multiple items as XML_parse breaks with special characters if (isset($xmlitem[$count])){ $xmlitem[$count].=$data; } elseif (!isset($xmlitem[$count])){ $xmlitem[$count]=$data; } return $xmlitem[$count]; } function startElement($parser, $name, $attrs) { global $curTag; //track the tag we are currently in $curTag .= "^$name"; } // XML Parser element end function function endElement($parser, $name) { global $curTag; // remove the tag we are ending from the "tag tracker" $caret_pos = strrpos($curTag,'^'); $curTag = substr($curTag,0,$caret_pos); } // get the xml information first global $curTag, $itemCount; global $item, $item2; /* Assuming the XML looks like: <Element> <Element1>Info</Element1> <Element1>Info</Element2> </Element> Then the route is:*/ $root = "^ELEMENT^"; $item=$root."ELEMENT1"; //must be in capitals to work, no idea why $item2=$root."ELEMENT2"; if ($curTag == $item) { $item[$itemCount]=checkamend ($item , $itemCount , $data); } elseif ($curTag == $itemCountryKey) { $item2[$itemCount]=checkamend ($item2, $itemCount , $data); $itemCount++; //make sure this is in the last elseif } } /*For each piece of data you want to pull out you have to go to the correct element and add an extra elseif statement. I had to use a seperate array for each because i was losing data and it was not being transferred properly Create the parser and parse the file*/ $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); $uFile = "URL to parse here"; if (!($fp = fopen($uFile,"r"))) { die ("could not open file for Input"); } // Read the XML file 4KB at a time while ($data = fread($fp, 4096)) { // Parse each 4KB chunk with the XML parser created above 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))); } } // Close the XML file fclose($fp); // Free up memory used by the XML parser xml_parser_free($xml_parser); // To output the data all you need to do is go through the arrays of items with a "for" loop for ($i=0;$i<$itemCount;$i++) { echo $item[$i]; echo $item2[$i]; } I hope this helps/works, i've editted it down a bit so it would be shorter to copy and paste. If you can give me the structure of the XML document I will be able to help more
  2. You can also produce a css sheet for each browser and use browser-sniffing techniques to distribute the correct CSS to the correct browser. Hope that helps!
  3. I think i'm trying to do precisely the same thing as you. I have come across a lot of similar scripts and http://www.tylerbutler.com/projects/Pages/ParsingXFDFinPHP.aspx seems to be one of the better ones. The code i am using at the moment is: class xItem { var $xTitle; var $xLink; var $xDescription; var $xUrl; var $xLng; var $xLat; } // general vars $sTitle = ""; $sLink = ""; $sDescription = ""; $sUrl = ""; $sLng = ""; $sLat = ""; $arItems = array(); $itemCount = 0; // rss url goes here $lng=-0.126236; $lat=51.500152; $rad=500; $trustedkey="5c289fd31415f9408bf803f7d7876e74"; $uFile = "http://trustedplaces.com/api/rest/place/geo/?lng=".$lng."&lat=".$lat."&rad=".$rad."&key=".$trustedkey; /* BEGIN XML PROCESSING */ // XML Parser element start function function startElement($parser, $name, $attrs) { global $curTag; //track the tag we are currently in $curTag .= "^$name"; } // XML Parser element end function function endElement($parser, $name) { global $curTag; // remove the tag we are ending from the "tag tracker" $caret_pos = strrpos($curTag,'^'); $curTag = substr($curTag,0,$caret_pos); } // XML Parser characterData function function characterData($parser, $data) { // get the xml information first global $curTag, $sTitle, $sLink, $sDescription, $sUrl, $sLng, $sLat; $titleKey= "^PLACES^PLACE^NAME"; $linkKey = "^PLACES^PLACE^WEBSITE"; $descKey = "^PLACES^PLACE^DESCRIPTION"; $urlKey = "^PLACES^PLACE^URL"; $lngKey = "^PLACES^PLACE^LONGITUDE"; $latKey = "^PLACES^PLACE^LATITUDE"; // we are in the value tag, so put the value in the array if ($curTag == $titleKey) { $sTitle = $data; } elseif ($curTag == $linkKey) { $sLink = $data; } elseif ($curTag == $descKey) { $sDescription = $data; } elseif ($curTag == $urlKey) { $sUrl = $data; } elseif ($curTag == $lngKey) { $sLng = $data; } elseif ($curTag == $latKey) { $sLat = $data; } // now get the items global $arItems, $itemCount; $itemTitleKey="^PLACES^PLACE^NAME"; $itemLinkKey="^PLACES^PLACE^WEBSITE"; $itemDescKey="^PLACES^PLACE^DESCRIPTION"; $itemUrlKey= "^PLACES^PLACE^URL"; $itemLngKey= "^PLACES^PLACE^LONGITUDE"; $itemLatKey= "^PLACES^PLACE^LATITUDE"; if ($curTag == $itemTitleKey) { // make new xItem $arItems[$itemCount] = new xItem(); // set new item objects properties $arItems[$itemCount]->xTitle = $data; } elseif ($curTag == $itemLinkKey) { $arItems[$itemCount]->xLink = $data; } elseif ($curTag == $itemDescKey) { $arItems[$itemCount]->xDescription = $data; } elseif ($curTag == $itemUrlKey) { $arItems[$itemCount]->xUrl = $data; } elseif ($curTag == $itemLngKey) { $arItems[$itemCount]->xLng = $data; } elseif ($curTag == $itemLatKey) { $arItems[$itemCount]->xLat = $data; } //increment item counter $itemCount++; } // Create the parser and parse the file $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 file for, nput"); } while ($data = fread($fp, 4096)) { $data = str_replace("&","[amp];",$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 for ($i=0;$i<count($arItems);$i++) { $txItem = $arItems[$i]; if (!empty($txItem->xLink)) echo("<a href=".$txItem->xLink.">".$txItem->xTitle."</a><br/>"); else echo("<h3>".$txItem->xTitle."</h3>"); if (!empty($txItem->xDescription)) echo ("<p>".$txItem->xDescription."</p>"); if (!empty($txItem->xUrl)) echo ("<p>".$txItem->xUrl."</p>"); if (!empty($txItem->xLng)) echo ("<p>".$txItem->xLng."</p>"); if (!empty($txItem->xLat)) echo ("<p>".$txItem->xLat."</p>"); } ?> The problem i'm having at the moment is that i'm losing data half way through the script for descriptions, ... I know it's using objects, but as this seems to be the most widely used code for parsing XML. It will also deal with special characters as for some reason "&" breaks the parser. I hope this helps, and any solutions to my problem would help out! function startElement($parser, $name, $attrs) { global $curTag, $field; //track the tag we're currently in $curTag .= "^$name"; if( $curTag == "^XFDF^FIELDS^FIELD" ) { //save the name of the field in a global var $field = $attrs['NAME']; } } If anybody can explain what the difference is and what the if statement is doing in this portion of Tyler Butler's code that might help me along too
  4. I have fought and struggled with the xml_parse function in php for about 2 days now, eventually i beat the ampersand problem (thank the internet) but now i appear to have lost a large amount of the variables when trying to print. I have 0 idea where in the code the error is, so I thought you lot might be able to help. The aim of the code is to parse the below xml file and output the name of the place (linked or not linked depending if there is a link), a description, a latitude and longitude and a link to the places website, assuming they have all of those things. I started the day outputting broken descriptions... now i've fixed the descriptions but for some reason i'm losing the data at some point in the code. Latitude, longitude and title are all passing through the script fine. Help?! The key for this page is fresh specifically for this post, so follow the link if you need to http://trustedplaces.com/api/rest/place/geo/?lng=-0.126236&lat=51.500152&rad=500&key=01fc6651eb1d60150df2b5a7d7ef9799 The code is: <?php class xItem { var $xTitle; var $xLink; var $xDescription; var $xUrl; var $xLng; var $xLat; } // general vars $sTitle = ""; $sLink = ""; $sDescription = ""; $sUrl = ""; $sLng = ""; $sLat = ""; $arItems = array(); $itemCount = 0; // rss url goes here $lng=-0.126236; $lat=51.500152; $rad=500; $trustedkey="5c289fd31415f9408bf803f7d7876e74"; $uFile = "http://trustedplaces.com/api/rest/place/geo/?lng=".$lng."&lat=".$lat."&rad=".$rad."&key=".$trustedkey; 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) { // get the Channel information first global $curTag; // global $sTitle, $sLink, $sDescription; $titleKey= "^PLACES^PLACE^NAME"; $linkKey = "^PLACES^PLACE^WEBSITE"; $descKey = "^PLACES^PLACE^DESCRIPTION"; $urlKey = "^PLACES^PLACE^URL"; $lngKey = "^PLACES^PLACE^LONGITUDE"; $latKey = "^PLACES^PLACE^LATITUDE"; if ($curTag == $titleKey) { $sTitle = $data; } elseif ($curTag == $linkKey) { $sLink = $data; } elseif ($curTag == $descKey) { $sDescription = $data; } elseif ($curTag == $urlKey) { $sUrl = $data; } elseif ($curTag == $lngKey) { $sLng = $data; } elseif ($curTag == $latKey) { $sLat = $data; } // now get the items global $arItems, $itemCount; $itemTitleKey="^PLACES^PLACE^NAME"; $itemLinkKey="^PLACES^PLACE^WEBSITE"; $itemDescKey="^PLACES^PLACE^DESCRIPTION"; $itemUrlKey= "^PLACES^PLACE^URL"; $itemLngKey= "^PLACES^PLACE^LONGITUDE"; $itemLatKey= "^PLACES^PLACE^LATITUDE"; if ($curTag == $itemTitleKey) { // make new xItem $arItems[$itemCount] = new xItem(); // set new item objects properties $arItems[$itemCount]->xTitle = $data; } elseif ($curTag == $itemLinkKey) { $arItems[$itemCount]->xLink = $data; } elseif ($curTag == $itemDescKey) { $arItems[$itemCount]->xDescription = $data; } elseif ($curTag == $itemUrlKey) { $arItems[$itemCount]->xUrl = $data; } elseif ($curTag == $itemLngKey) { $arItems[$itemCount]->xLng = $data; } elseif ($curTag == $itemLatKey) { $arItems[$itemCount]->xLat = $data; } // increment item counter $itemCount++; } // 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 file for input"); } while ($data = fread($fp, 4096)) { $data = str_replace("&","[amp];",$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 for ($i=0;$i<count($arItems);$i++) { $txItem = $arItems[$i]; if (!empty($txItem->xLink)) echo("<a href=".$txItem->xLink.">".$txItem->xTitle."</a><br/>"); else echo("<h3>".$txItem->xTitle."</h3>"); if (!empty($txItem->xDescription)) echo ("<p>".$txItem->xDescription."</p>"); if (!empty($txItem->xUrl)) echo ("<p>".$txItem->xUrl."</p>"); if (!empty($txItem->xLng)) echo ("<p>".$txItem->xLng."</p>"); if (!empty($txItem->xLat)) echo ("<p>".$txItem->xLat."</p>"); } ?> Thanks for any help/advice you can give. Any ideas will be tried, tested, tried again and tested a few more times.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.