Jump to content

xml_parser and various issues :S


tcwadams

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/124659-xml_parser-and-various-issues-s/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.