Forum Moderators: open
The old format was:
<specials>
<special>
<id>1101</id>
etc...
etc...
</special>
</specials>
function getXmlData($xml) {$n=0; // Counter used for arraying the XML data
$ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()
// Parse the XML document
$parser = xml_parser_create();
xml_parse_into_struct($parser,$xml,$vals,$index); //or die(xml_error_string(xml_get_error_code($parser)));
xml_parser_free($parser);
$ttags=array(); // Temporary arry for storing tag names
for($n=0;$n<=count($vals)-1;$n++) {
if(trim($vals[$n][value])) {
$ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
$ttags[$vals[$n][tag]]=$vals[$n][tag];
}
}
$tags=array();
// Extract and save the tag names to the array
foreach($ttags as $tagi) { array_push($tags,$tagi); }
return $ar;
}
// Get the file contents
$myar=getXmlData($contents);
for($i=0;$i<=count($myar[TITLE]);$i++) {
// Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
// You can read the values of any tag like this.
// $myar[TITLE][$i], $myar[SOMETHING][$i], $myar[SOMETAG][$i].... and so on..
// Here it goes
$id = $myar[ID][$i];
$link = $myar[LINK][$i];
$localeid = $myar[LOCALEID][$i];
$localename = $myar[LOCALENAME][$i];
$resortid = $myar[RESORTID][$i];
$resortlink = $myar[RESORTLINK][$i];
$title = $myar[TITLE][$i];
$summary = $myar[SUMMARY][$i];
$thumbnaillink = $myar[THUMBNAILLINK][$i];
$rentalbasequantity = $myar[RENTALBASEQUANTITY][$i];
$pricedoubleroom = $myar[PRICEDOUBLEROOM][$i];
$salesend = $myar[SALESEND][$i];
$timestamp = $myar[TIMESTAMP][$i];
$pricead = $myar[PRICEAD][$i];
//To make sure that summary does not contain Â
$summary = ereg_replace("[Â]","",$summary);
$summary = ereg_replace("<p>","",$summary);
$summary = ereg_replace("</p>","",$summary);
$summary = ereg_replace("<br>","",$summary);
}
Current Format:
<specials>
<special id="1859">
<title>
ARUBA - Amsterdam Manor Beach Resort - (AI) ~May 21 - August 30, 2007
</title>
<summary>
Travel Dates: May 21 - August 30, 2007 All Inclusive ~ Small, Quiet Resort at Eagle Beach!
</summary>
<thumbnail>
http://www.example.com/upload/file_25396.jpg
</thumbnail>
<salesEnd>Apr 02, 2007</salesEnd>
<advNights>5</advNights>
<localeId>275</localeId>
<localeName>Aruba</localeName>
<advertisedPrice>1529.00</advertisedPrice>
<travelDatesInfo>May 21 - August 30, 2007</travelDatesInfo>
<additionalInfo>All Inclusive</additionalInfo>
</special>
</specials>
[edited by: jatar_k at 3:12 pm (utc) on April 4, 2007]
[edit reason] please use example.com [/edit]
I'm also not exactly sure what the issue is. I assume that you need to extract the "id" attribute value from an instance of "special." Since there was a dedicated "id" element before, it just showed up in the array, and the attributes are not.
I'm sure there's a different class function for that. Attributes are a critical part of XML, and there should definitely be a way to get at them.
They don't seem to cover attributes well in the docs [us.php.net], but I did come upon this example [us.php.net]. where they actually handle attributes.
Also, I do recommend <oXygen/>. It's a pretty low-cost, cross-platform XML tool that I have found absolutely invaluable for learning up on XML. It won't be that much help for this one particular issue, but it will help if you want to delve more deeply into XML.
I have a parser put together that ouputs the array below. What I am having problems with is getting the values into php friendy variables so I can display the content. I am also including a "work in progress" piece of code where I am trying to loop through the array to get at the values...but I am stuck on the right format to out put the content of the array!
Array output of XML feed:
Array
(
[SPECIALS] => Array
(
[SPECIAL] => Array
(
[0] => Array
(
[ID] => 2114
[TITLE] => COZUMEL - Allegro Cozumel ~ July 1 - August 31, 2007
[THUMBNAIL] => http://www.example.com/upload/file_16183.jpg
[SALESEND] => Apr 03, 2007
[ADVNIGHTS] => 5
[LOCALEID] => 322
[LOCALENAME] => Cozumel
[ADVERTISEDPRICE] => 949.00
[TRAVELDATESINFO] => July 1 - August 31, 2007
[ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
)
[1] => Array
(
[ID] => 2113
[TITLE] => COZUMEL - Allegro Cozumel ~ May 1 - June 30 & September 1 - October 31, 2007
[THUMBNAIL] => http://www.example.com/upload/file_16183.jpg
[SALESEND] => Apr 03, 2007
[ADVNIGHTS] => 5
[LOCALEID] => 322
[LOCALENAME] => Cozumel
[ADVERTISEDPRICE] => 879.00
[TRAVELDATESINFO] => May 1 - Jun 30 & Sep 1 - Oct 31, 2007
[ADDITIONALINFO] => All Inclusive ~ Kids Stay & Eat Free
)
)
)
)
// Get the file contents
$myar=getXmlData2($contents);
for($i=0;$i<=count($myar[TITLE]);$i++) {
// Here, we want to read the TITLE, SUMMARY, and THUMBNAIL etc. of the XML feed.
$id = $myar[ID][$i];
$title = $myar[TITLE][$i];
$thumbnaillink = $myar[THUMBNAIL][$i];
$saleends = $myar[SALESEND][$i];
$advnights = $myar[ADVNIGHTS][$i];
$localeid = $myar[LOCALEID][$i];
$localename = $myar[LOCALENAME][$i];
$adverpprice = $myar[ADVERTISEDPRICE][$i];
$pricedoubleroom = $myar[TRAVELDATESINFO][$i];
$salesend = $myar[ADDITIONALINFO][$i];
$summary = $myar[SUMMARY][$i];
//echo " <b>$id</b> $title $summary <br><br>";
}
?>
Any suggestions would be appreciated!
Thanks,
Neal
[edited by: jatar_k at 3:11 pm (utc) on April 4, 2007]
[edit reason] please use example.com [/edit]