• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

How to Populate A Combo Box(in HTML) with all the Values From an XML file..

hi all
I am really strugling with XML..that makes me try to learn it even better..
I have this xml file(companies.xml).
****************************************
<?xml version="1.0" ?>
- <companies>
- <company>
  <co_code>20tcen</co_code>
  <co_name>20th Century Finance Corporation Ltd (Merged)</co_name>
  </company>
- <company>
  <co_code>akhtex</co_code>
  <co_name>Akhileshwar Texports Ltd</co_name>
  </company>
- <company>
  <co_code>arcgre</co_code>
  <co_name>Archies Greetings and Gifts Ltd</co_name>
  </company>
- <company>
  <co_code>elgequ</co_code>
  <co_name>Elgi Equipments Ltd</co_name>
  </company>
- <company>
  <co_code>gujvit</co_code>
  <co_name>Gujarat Vita Pharma Ltd</co_name>
  </company>
- <company>
  <co_code>jattex</co_code>
  <co_name>Jatayu Textiles and Industries Ltd</co_name>
  </company>
- <company>
  <co_code>leafin</co_code>
  <co_name>Leasing Finance India Ltd</co_name>
  </company>
- <company>
  <co_code>mefcap</co_code>
  <co_name>Mefcom Capital Markets Ltd</co_name>
  </company>
- <company>
  <co_code>suqdam</co_code>
  <co_name>Sugal and Damani Finlease Ltd</co_name>
  </company>
- <company>
  <co_code>tstanc</co_code>
  <co_name>T Stanes and Company Ltd</co_name>
  </company>
- <company>
  <co_code>viclam</co_code>
  <co_name>Victory Laminations Ltd</co_name>
  </company>
- <company>
  <co_code>yuvint</co_code>
  <co_name>Yuvraj International Ltd</co_name>
  </company>
  </companies>
****************************************
I want to render all the values in this XML file in a Combobox in a simple html page..That is I want to populate all the values in the XML FILE in the combobox.
i am really strugling with the code..Can anyone help me..with it..
Regards
Nitin K
0
khacharn
Asked:
khacharn
  • 3
  • 2
  • 2
  • +1
1 Solution
 
BigRatCommented:
Post the code you have so far, or at least say with what tool you are trying to process it.
0
 
khacharnAuthor Commented:
hi BigRat..
Here is the code for the .html page in which i am calling the xml file
****************************************
<applet code="com.ms.xml.dso.XMLDSO.class" id="xmldso" mayscript="true" height="1"
width="1">
  <param name="url" value="companies.xml">
</applet>
<html>
<body>

<table DATASRC="#xmldso">
*****problem Area***********************
******This coide does not work*********
<select name="list" rows="3">
      <option value="me" ><span DATAFLD="co_name"></span></option></select>
****************************************
****************************************
  <tr>
    <td><a href=<span DATAFLD="co_code">&nbsp;<span DATAFLD="co_code"></span></a></td>
    <td><span DATAFLD="co_name"></span></td>
  </tr>
</table>

</body>
</html>
***************************************
Please Help ASAP
Regards
Nitin
0
 
BigRatCommented:
Why isn't the <select> a row in the table? And what exactly do you get on the screen?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
khacharnAuthor Commented:
If i put the <select> in the <tr> then it is printed as many times as there are records in the Xml file..

in the xml file there are 10 companies so the select box gets printed in the html file with no values in it..
basically the:
<select name="list" rows="3">
<option value="me" ><span DATAFLD="co_name"></span></option></select>
DOES NOT WORK..
Regards
Nitin
0
 
gavindewCommented:
Try the following. assuming xml is loaded as follows :

<XML ID=xmldso>YOUR XML</XML>

function loadcombo()
{
var c;

for(c in xmldso.documentElement.childNodes)
{
    opt = document.createElement("OPTION");
    opt.value = c.selectSingleNode("co_code").text;
    opt.text = c.selectSingleNode("co_name").text;
    combo.options.add(opt);
}

WHERE combo is the name of your combo box.

I haven't tested the code for errors, but the principle definitely works. I have used it a number of times.

Regards
Gavin
0
 
AlanB082898Commented:
You can do this by applying XSL to your XML.

1. Add <?xml-stylesheet type="text/xsl" href="companies.xsl" ?> below the <?xml version="1.0" ?> line.

2. Use this XSL file (save it as companies.xsl to match above)

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  <xsl:template match="/">
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="companies">
    <html>
      <head>
        <title>Companies</title>
      </head>
      <body>
        <center>
          <h4> Companies</h4>
        </center>
        <select>
          <xsl:apply-templates />
        </select>
      </body>
    </html>
  </xsl:template>
  <xsl:template match="company">
    <option value="me"/>
    <xsl:value-of select="co_name" />
  </xsl:template>
</xsl:stylesheet>

3. Double click on your the companies.xml file.

You should see a ComboBox in a simple HTML page. I did when I tested it under IE 5.

note: The code you were trying to use (data binding) works great for filling a table but it won't fill a select. It will just allow you to set which item in the list is selected. see http://msdn.microsoft.com/workshop/c-frame.htm#/workshop/author/default.asp and search for select on the page.
0
 
AlanB082898Commented:
The microsoft url I listed in the answer doesn't work. It should be http://msdn.microsoft.com/workshop/c-frame.htm?950744713098#/workshop/author/databind/dataconsumer.asp and search for "select element"
note: I know its probably obvious but step 1 in the answer refers to the XML file.
0
 
khacharnAuthor Commented:
HI all..
Thanx for all the help
Last Question
I have a
1) try.xml file
2) try.xsl file
I want to ccreate a new virtual document using both these
I tried calling both of these in an xml file but my syntax does not work..
CAN YOU HELP ME..
PLEASE REPLY ASAP
regards
Nitin
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now