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</manuf
acturer>
<model>Civic</model>
<category>Sedan</category>
<trim>EX-S</trim>
<colour>Mettalic Blue</colour>
<year>2003</year>
<price>16600</price>
<transmission>Automatic</t
ransmissio
n>
<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.jp
g</thumb>
<full>pictures/f_civic.jpg
</full>
</picture>
</vehicle>
<vehicle ID="320FTN">
<manufacturer>Honda</manuf
acturer>
<model>Accord</model>
<category>Sedan</category>
<trim>LX</trim>
<colour>Silver</colour>
<year>1999</year>
<price>17000</price>
<transmission>Automatic</t
ransmissio
n>
<condition>Used</condition
>
<odometer>17800</odometer>
<picture>
<thumb>pictures/t_old_silv
er_accord.
jpg</thumb
>
<full>pictures/f_old_silve
r_accord.j
pg</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.getElementsByTagN
ame("vehic
le_db");
veh = topHandle.item(0);
vehElements = veh.getElementsByTagName("
vehicle");
firstVe = vehElements.item(0);
model = firstVe.getElementsByTagNa
me("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
Start Free Trial