473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Populating a combobox from an XML file

How do you go about populating a select list from an XML file?

I can open the XML file fine and get at all of the data, but I'm stuck on
how to use that data in my <option> tags. Is it even possible?

Thanks.
Jul 23 '05 #1
2 3834


Billy Smith wrote:
How do you go about populating a select list from an XML file?

I can open the XML file fine and get at all of the data, but I'm stuck on
how to use that data in my <option> tags. Is it even possible?


It depends on the browser how you do it, or how you finally do it
depends on what browsers you target.

If you look at Mozilla or at Opera 8 which have their own combined XML
and HTML DOM implementation then you simply need to make sure you put
XHTML <option> elements in the XML by using the proper namespace and
then you can use the importNode method to import the nodes from the XML
DOM document into your HTML document and use appendChild to insert the
elements into the <select>, e.g.

// parsing X(HT)ML from string here for the example
// but could of course load/parse from a URL
var xmlDocument = new DOMParser().parseFromString(
[
'<dt:data xmlns:dt="http://example.com/2005/dt"',
' xmlns="http://www.w3.org/1999/xhtml">',
'<option>Kibo</option>',
'<option>Xibo</option>',
'<option>Jaffo</option>',
'</dt:data>'
].join('\r\n'),
'application/xml'
);
var select = document.createElement('select');
var options = xmlDocument.getElementsByTagNameNS(
'http://www.w3.org/1999/xhtml',
'option'
);
for (var i = 0; i < options.length; i++) {
select.appendChild(
select.ownerDocument.importNode(options[i], true));
}
document.body.appendChild(select);
With IE the problem is that IE itself implements the HTML DOM and the
XML DOM is implemented by MSXML and script simply uses MSXML as an
ActiveX component, there is no importNode to move nodes from one DOM
implementation to the other, and even if you use the proper XHTML
namespace then in MSXML's XML DOM you simply have some Element nodes but
not HTMLElement or HTMLOptionElement nodes. So with IE you would need to
write some importNode yourself which traverses the XML DOM and creates
the necessary HTML DOM nodes or you could consider to treat the input as
a markup string which you assign the innerHTML of the select. But the
latter does strange things here for me with IE 6 on Windows XP SP 2, it
does not look like setting innerHTML on the select is a working approach
to get any options to show up.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
VK


Martin Honnen wrote:
With IE the problem is that IE itself implements the HTML DOM and the
XML DOM is implemented by MSXML and script simply uses MSXML as an
ActiveX component, there is no importNode to move nodes from one DOM
implementation to the other, and even if you use the proper XHTML
namespace then in MSXML's XML DOM you simply have some Element nodes but
not HTMLElement or HTMLOptionElement nodes. So with IE you would need to
write some importNode yourself which traverses the XML DOM and creates
the necessary HTML DOM nodes or you could consider to treat the input as
a markup string which you assign the innerHTML of the select. But the
latter does strange things here for me with IE 6 on Windows XP SP 2, it
does not look like setting innerHTML on the select is a working approach
to get any options to show up.

The problem is that IE implements rather strict databinding scheme.
With a normal XMLData/XMLTemplate scheme there is no problem:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<HTML>
<BODY>
....
<xsl:for-each select="portfolio/stock">
<SELECT NAME="Portfolio">
<OPTION><xsl:value-of select="name"/></OPTION>
</SELECT>
</xsl:for-each>
....
</xsl:template>
</xsl:stylesheet>
(Actually it already works in FF too)

And for all kind of tabular data (anyhow delimited fields' set) they
have full set databound elements populating automatically upon new data
arrival.

As many browsers do not stay even close to that, users need so far to
make an equivalend of a stomach surgery through the nostrils (to not
mention other place :-) Thus they load XML to a static HTML page and
then manually place each peace of info to the right spot.

If Co & Co hopes to win corporate customers this way, they are sorry
mistaken. :-(

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert | last post by:
I'm sure this is a fairly basic question, but I've been looking all over the web for days for suggestions on how to do this. I've got a datagrid that's bound to a dataset on my form. It includes...
2
by: John Ninan | last post by:
I am creating Dynamic Usercontrol in Asp.net application. In this application I have a combobox(aspx Page). Which contains various items. Based on item selected I am dynamically populating...
4
by: Keith | last post by:
Hello - this started out as a minor annoyance - and now is starting to bother me more and more - I'm hoping someone can help me. I would like to have a combobox display - NOT initially be blank...
2
by: Bill Brinkworth | last post by:
in vb.net 2005, i would like to pull up a name from a database (customers) (i can do this already), then after a name is selected from a combox box, to populate the form's text boxes with with data...
4
by: c_shah | last post by:
I am a beginner and learning VB.net There are two wasy to bind a combobox or a listbox First approach is to assign a datasource and displaymember Second approach is to iterate through datarow...
5
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted...
0
by: CCLeasing | last post by:
Hello, I have searched google but can not find a straight forward answer to my problem. Hopefuly someone will be kind enough to offer their expertise. Please forgive if this seems a bit convoluted...
0
by: Paul Hadfield | last post by:
I'm looking for thoughts on the "correct" design for this problem (DotNet 2.0 - in winforms, but that's not so important). I've got two combo boxes (combo1 and combo2), both are populating via...
4
by: yaaara | last post by:
Hi, Here's another problem that I came up with: I have a combobox in a form with 2 columns. What I need is the following: 1. The combobox to be cleared on form_load (not only the text, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.