f



help with extracting nodes and their children as text

I'm loading an xml data file and then trying to take a particular node
and add it, as html, to an element on my page using inner HTML.  The
xml is like what is below, with the ... representing eliminated
detail:

<?xml version="1.0" encoding="iso-8859-1"?>
<table>
  <descriptions>
    <fielddesc id="1">
    ...
    </fielddesc>
    ...
  </descriptions>
  <recordset>
    <record id="1">
	<field id="1">1</field>
	<field id="2">Hydrogen</field>
	<field id="3">H</field>
	<field id="4">[not applicable]</field>
	<field id="5">[not applicable]</field>
	<field id="6">[not applicable]</field>
	<field id="7">1s<sup>1</sup></field>
    </record>
    ...
  </recordset>
</table>

The problem comes with id 7 above. I want to get everything that is
contained in that field and use it in my page so that the <sup> html
tag is used.  Here are some things I've tried:

recordset = table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
valueIwant = recordset[1].getElementsByTagName('field')[7].firstChild.data;
(This only gets everything up to the <sip> tag)

recordset = table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
valueIwant = recordset[1].getElementsByTagName('field')[7].text; 
(This only works on IE and it doesn't format the contents in the <sup>
tag)

Anybody have some suggestions on how to do what I want?

Thanks,
Rob
0
rfentres
2/6/2004 2:00:25 PM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

4 Replies
443 Views

Similar Articles

[PageSpeed] 14

Am I missing something? You write: valueIwant = recordset[1] ... 
('field')[7] ...

Shouldn't that be recordset[0] and ...('field')[6] ?



recordset = 
table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
 > valueIwant = 
recordset[1].getElementsByTagName('field')[7].firstChild.data;
 > (This only gets everything up to the <sip> tag)
 >
 > recordset = 
table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
 > valueIwant = recordset[1].getElementsByTagName('field')[7].text;
 > (This only works on IE and it doesn't format the contents in the <sup>
 > tag)

0
Antenna
2/6/2004 2:33:03 PM

Robert Fentress wrote:

> I'm loading an xml data file and then trying to take a particular node
> and add it, as html, to an element on my page using inner HTML.  The
> xml is like what is below, with the ... representing eliminated
> detail:
> 
> <?xml version="1.0" encoding="iso-8859-1"?>
> <table>
>   <descriptions>
>     <fielddesc id="1">
>     ...
>     </fielddesc>
>     ...
>   </descriptions>
>   <recordset>
>     <record id="1">
> 	<field id="1">1</field>
> 	<field id="2">Hydrogen</field>
> 	<field id="3">H</field>
> 	<field id="4">[not applicable]</field>
> 	<field id="5">[not applicable]</field>
> 	<field id="6">[not applicable]</field>
> 	<field id="7">1s<sup>1</sup></field>
>     </record>
>     ...
>   </recordset>
> </table>
> 
> The problem comes with id 7 above. I want to get everything that is
> contained in that field and use it in my page so that the <sup> html
> tag is used.  Here are some things I've tried:
> 
> recordset = table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
> valueIwant = recordset[1].getElementsByTagName('field')[7].firstChild.data;
> (This only gets everything up to the <sip> tag)
> 
> recordset = table.getElementsByTagName('recordset')[0].getElementsByTagName('record');
> valueIwant = recordset[1].getElementsByTagName('field')[7].text; 
> (This only works on IE and it doesn't format the contents in the <sup>
> tag)
> 
> Anybody have some suggestions on how to do what I want?

Well, an XML element with tagname sup is not an HTML element, if you 
target XHTML you would need
   <sup xmlns="http://www.w3.org/1999/xhtml">...</sup>
that way a browser like Mozilla would allow you to import that node from 
the XML document into an XHTML document:
   http://home.arcor.de/martin.honnen/javascript/200402/test20040206.xhtml

IE however doesn't understand XHTML and doesn't support importNode so 
there you are better off trying to insert some snippet of markup using 
insertAdjacentHTML or innerHTML. XSLT transformations might be a way 
with IE to create that snippet of HTML markup from your XML.
-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/

0
Martin
2/6/2004 3:11:58 PM
Antenna <q123456789-NO_SPAM@hotmail.com> wrote in message news:<c008jp$kj7$1@news.worldonline.be>...
> Am I missing something? You write: valueIwant = recordset[1] ... 
> ('field')[7] ...
> 
> Shouldn't that be recordset[0] and ...('field')[6] ?
> 
> 

Yes.  I realized that after I sent it.  Doesn't affect the issue though.

Rob
0
rfentres
2/6/2004 6:56:48 PM
Martin Honnen <mahotrash@yahoo.de> wrote in message news:<4023aec3$1@olaf.komtel.net>...
> Well, an XML element with tagname sup is not an HTML element, if you 
> target XHTML you would need
>    <sup xmlns="http://www.w3.org/1999/xhtml">...</sup>
> that way a browser like Mozilla would allow you to import that node from 
> the XML document into an XHTML document:
>    http://home.arcor.de/martin.honnen/javascript/200402/test20040206.xhtml
> 
> IE however doesn't understand XHTML and doesn't support importNode so 
> there you are better off trying to insert some snippet of markup using 
> insertAdjacentHTML or innerHTML. XSLT transformations might be a way 
> with IE to create that snippet of HTML markup from your XML.

Thanks for your help Martin.  I'll try to do some research on XSLT so
I can get it to work on IE too.

Rob
0
rfentres
2/7/2004 1:36:10 PM
Reply: