Click to See Complete Forum and Search --> : XML text read


Petrolhead
10-13-2003, 02:06 AM
Hi,

Does anyone know how to extract CDATA sections from an XML file using javascript and W3C dom? I cannot read the text that it stored between tags it keeps giving me an undefined object error.
For example:
my XML fie looks like

<vehicle_db>
<vehicle ID="760KRT">
<manufacturer>Honda</manufacturer>
<model>Civic</model>
<category>Sedan</category>
<trim>EX-S</trim>
<colour>Mettalic Blue</colour>
<year>2003</year>
<price>16600</price>
<transmission>Automatic</transmission>
<condition>New</condition>
<odometer>0</odometer>
<options>
<option>
<o_name>6 CD Changer</o_name>
<o_price>350</o_price>
</option>
<option>
<o_name>Leather Seats</o_name>
<o_price>700</o_price>
</option>
<option>
<o_name>Fog Lights</o_name>
<o_price>200</o_price>
</option>
</options>
<picture>
<thumb>pictures/t_civic.jpg</thumb>
<full>pictures/f_civic.jpg</full>
</picture>
</vehicle>

<vehicle ID="320FTN">
<manufacturer>Honda</manufacturer>
<model>Accord</model>
<category>Sedan</category>
<trim>LX</trim>
<colour>Silver</colour>
<year>1999</year>
<price>17000</price>
<transmission>Automatic</transmission>
<condition>Used</condition>
<odometer>17800</odometer>
<picture>
<thumb>pictures/t_old_silver_accord.jpg</thumb>
<full>pictures/f_old_silver_accord.jpg</full>
</picture>
</vehicle>

Im trying to write a search function to search these fields. So lets say I wanted the see the model for the first vehcile (want it to return Civic)? How would I do that?
My code right now is

<script type="text/javascript">

function search()
{
topHandle = document.getElementsByTagName("vehicle_db");

veh = topHandle.item(0);

vehElements = veh.getElementsByTagName("vehicle");

firstVe = vehElements.item(0);

model = firstVe.getElementsByTagName("model");

//How to read the text (CDATA) within the model tags? (i.e. civic)
document.write(model1);

return;

}


I tried model.getValue but that didnt work.
model.text failed too, I cannot use the microsoft DOM, it has to be done using W3C with mozilla 1.4 as the reference browser.

Thanks