nihilnovi 0 Newbie Poster

So been sitting with this problem all week, can't figure it out.

Need to make a automatic parser that converts xml in this format:

<?xml version="1.0" encoding="UTF-8" ?>
<open_marine version="1.7" language="english" origin="Companyname" date="ISO 8601 1250016600">
<broker code="MIB">
<broker_details>
<company_name>Company name</company_name>
</broker_details>
<offices>
<office id="1">
<office_name>Moore International</office_name>
<email>mail@mail.com</email>
<name >
<title />
<forename />
<surname />
</name>
<address>random address </address>
<town>Poole</town>
<county>Dorset</county>
<country>UK</country>
<postcode>BH13 7HS</postcode>
<daytime_phone>+00 0 0000 000</daytime_phone>
<evening_phone />
<fax>+00 000 00 000 00</fax>
<mobile />
<website>www.whatever.com</website>
</office>
</offices>
<adverts>
<advert ref="392" office_id="1" status="Available">
<advert_media>
<media type="image/jpeg" caption="main" primary="True">img url</media>
<media type="image/jpeg" caption="plan" primary="False">img url</media>
<media type="image/jpeg" caption="1" primary="False">img url</media>
<media type="image/jpeg" caption="2" primary="False">img url</media>
<media type="image/jpeg" caption="3" primary="False">img url</media>
<media type="image/jpeg" caption="4" primary="False">img url</media>
<media type="image/jpeg" caption="5" primary="False">img url</media>
<media type="image/jpeg" caption="6" primary="False">img url</media>
<media type="image/jpeg" caption="7" primary="False">img url</media>
<media type="image/jpeg" caption="8" primary="False">img url</media>
<media type="image/jpeg" caption="9" primary="False">img url</media>
<media type="image/jpeg" caption="10" primary="False">img url</media>
<media type="image/jpeg" caption="11" primary="False">img url</media>
<media type="image/jpeg" caption="12" primary="False">img url</media>
<media type="image/jpeg" caption="13" primary="False">img url</media>
<media type="image/jpeg" caption="14" primary="False">img url</media>
<media type="image/jpeg" caption="15" primary="False">img url</media>
</advert_media>
<advert_features>
<boat_type>power</boat_type>
<boat_category />
<new_or_used>used</new_or_used>
<vessel_lying country="Spain">Ampuriabrava</vessel_lying>
<asking_price poa="False" currency="EUR" vat_included="True" >175000</asking_price>
<marketing_descs>
<marketing_desc language="ISO-8859-1">Description with <br /> tags. </marketing_desc>
</marketing_descs>
<manufacturer>SEA RAY</manufacturer>
<model>550 Sedan Bridge </model>
<other>
<item name="external_url" label="" />
</other>
</advert_features>
<boat_features>
<dimensions>
<item name="beam" unit="feet">15.0</item>
<item name="draft" unit="feet">4.2</item>
<item name="loa" unit="feet" >57.10</item>
</dimensions>
<build>
<item name="year">1992</item>
<item name="hull_colour"></item>
<item name="flybridge">True</item>
<item name="displacement" units="kg">24000</item>
</build>
<engine>
<item name="fuel">diesel</item>
<item name="hours">1060</item>
<item name="max_speed">30</item>
<item name="horse_power">665</item>
<item name="engine_manufacturer">MTU 8V-183 TE92</item>
<item name="engine_quantity">2</item>
<item name="tankage" unit="litres">2271</item>
<item name="drive_type">Conventional Shaft</item>
</engine>
<accommodation>
<item name="cabins">3</item>
<item name="berths">6</item>
<item name="toilet">2</item>
</accommodation>
</boat_features>
</advert>
<advert ref="370" office_id="1" status="Available">
<advert_media>
etc... lots of entries =)

The problem is to convert it to our specific xml format, automatically:

<?xml version="1.0" encoding="UTF-8"?>
<feed version="0.1">
<feed_information>
<reference>reference</reference>
<title>Title</title>
<description>Desc</description>
<created>Y-m-d H:i:s</created>
<updated>Y-m-d H:i:s</updated>
</feed_information>
<dealer>
<id>780</id>
<name>The dealer</name>
</dealer>
<adverts>
<advert reference="392" category="powerboat">
<preowned>yes</preowned>
<type>sale</type>
<brand>sea ray</brand>
<model>550 sedan bridge</model>
<year>1992</year>
<price_on_request>no</price_on_request>
<price currency="EUR">175000</price>
<location>
<country>spain</country>
<region />
<city />
</location>
<headline>sea ray 550 sedan bridge</headline>
<description> Description </description>
<beam unit="ft">15.0</beam>
<loa unit="ft">57.10</loa>
<draft unit="ft">4.2</draft>
<displacement unit="t">24000</displacement>
<powerboat_type />
<engine total="2" hp="665" hours="1060">mtu</engine>
<speed>
<cruising />
<max unit="kt">30</max>
</speed>
<fuel_tankage unit="l">2271</fuel_tankage>
<water_tankage />
<naval_architect />
<interior_designer />
<hull_material />
<berths />
<crew />
<media>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
<image>
<image_url>img url</image_url>
</image>
</media>
</advert>

I've tried converting to an array and then to xml, but the problem arises when there is not a set number of <images> within 1 advert entry.
Any ideas?