Hi,
I am new to this (just got it yesterday!) and I have what is probably a simple question.
I am trying to parse the following xml code. I can retrieve the data in the ad tags ok (but not the id= part, don't need that anyway), but I wonder how to get the data from the section tags.
Any help anyone can offer me would be greatly appreciated!
<classifieds>
<section name="FURNISHED 1 BEDROOM" code="021">
<ad id="102676">0 Utilities. 1 bedroom, flexible lease periods, super convenient location, 38 E. 17th Ave, laundry, off-street parking. $350-$450 per month. 296-6304 or 263-1193.</ad>
<ad id="102470">1 bedroom. North Campus, 3 blocks N. of Lane & Neil. Mainly grad students in building. Clean, very secure, quiet, off-street parking, car ports, carpeted, A/C, laundry room, microwave. Available now. 562-1415.</ad>
<ad id="103353">Available now and Fall. Recently remodeled studio suites in prime locations. Utilities paid, A/C, free parking, security doors. Short term available. $405-475/mo. www.universitymanors.com 291-5001</ad>
<ad id="103677">Northwest OSU. Spring and Summer quarter, Riverview Dr. - 1 Bdr, living room, kitchen, bath, hardwood floors, new gas furnace, new windows, A/C, new furniture, water paid, laundry, free off-street parking. Call 571-5109. </ad>
<ad id="102694">OSU Northwest Campus, Luxury Units, completely remodeled, hardwood floors, new baths, new windows new gas furnace, A/C, large unit with walk-in closets, laundry, free Off-street parking, H20 paid, Call 571-5109</ad>
</section>
<section name="FURNISHED 2 BEDROOM" code="022">
<ad id="103323">$1100 NEW 2BR 2BA W/D POOL FITNESS at Emerald Pkwy & Sawmill in Dublin. 668-0275</ad>
<ad id="103431">2BR, 1 bath in Riverwatch Tower, 364 W. Lane Ave. Parking in secured lot, trash pickup, water, sewer, wireless Internet, & basic cable all included. $765/mo, 12 month lease. 614-570-2425
</ad>
<ad id="103618">Beautiful fully furnished 2 bedroom apartment with leather couches, cherry-wood furniture and new computer desks, on-site laundry, private -off street parking and a great price. Only $675 per month including water! Call 614-294-3502 for a tour today.</ad>
<ad id="102163">Some of the Campus Best, 2 bdrm T.H. and flats, furnished and unfurnished. Oak staircases, oak cabinets, D/W, very large kitchen w/eating area, CA, parking. Range $590-$660. Call 718-0790.</ad>
</section>
</classifieds>
David,
Thank you so much for your help, the code works like a charm!
This gives me a great starting point for learning with a practical example.
I appreciate your timely response,
Rick
Hello Rick,
You have probably been able to extract all Ads using the auto-detected format string. In order to access each SECTION as the repeating record; you need to use the following format string:
xml|CLASSIFIEDS/SECTION/
However, this means that each AD becomes a duplicate item within each SECTION; and so Magic Parser uses the @n postfix in order to differentiate them (this is because ultimately, Magic Parser is designed to access multiple records in a flat XML file). Therefore, in order to handle this, the method I normally suggest is to create a loop that goes through @1..@2.. etc., checking for duplicate fields of the expected names; and processing each one individually until there are no more.
It's probably best explained with an example, so I've just written a short script that displays each section and each ad within that section (followed by the ad ID). The script uses the XML from your post, saved as "classifieds.xml" in the same directory as the script:
View Output
Source:
<?php
require("MagicParser.php");
function myRecordHandler($record)
{
print "<h2>".$record["SECTION-NAME"]."</h2>";
$i = 0;
while(1) {
if ($i) $postfix = "@".$i;
if (!$record["AD".$postfix."-ID"]) break;
$ad_id = $record["AD".$postfix."-ID"];
$ad = $record["AD".$postfix];
print "<p>".$ad." (ID: ".$ad_id.")</p>";
$i++;
}
}
MagicParser_parse("classifieds.xml","myRecordHandler","xml|CLASSIFIEDS/SECTION/");
?>
Hope this helps!
Cheers,
David.