homepage Welcome to WebmasterWorld Guest from 54.205.189.156
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
XML & PHP
Parsing XML with PHP
nealw




msg:3298973
 10:48 am on Apr 1, 2007 (gmt 0)

I am having problems with trying to parse a feed with the following format. And it comes down to the way this "newly" formated feed uses the "id". I'm not overly familar with xml but it seems the id was moved to node?

The old format was:

<specials>
<special>
<id>1101</id>
etc...
etc...
</special>
</specials>

I was using this to parse the old feed, but this is no longer working on the new format (see below)


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]

 

cmarshall




msg:3299060
 12:28 pm on Apr 1, 2007 (gmt 0)

I haven't used these particular XML functions yet.

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.

nealw




msg:3299469
 9:59 pm on Apr 1, 2007 (gmt 0)

Update: Problem not quite solved but...

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 of the array: This is where I am having a problem?

// 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]

cmarshall




msg:3299582
 1:46 am on Apr 2, 2007 (gmt 0)

For each of the assignments:

$id = $myar[ID][$i];

should be:

$id = $myar[$i]['ID'];

You have the nested indexes in the wrong order. You should also quote the array key. It isn't required, but it's a good habit to get into.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved