页面代码:
<html> <head> <script language="JavaScript" for="window" event="onload"> var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); var i=0; var j=0; loadXML(); function loadXML(){ xmlDoc.async="false"; xmlDoc.load("account.xml"); xmlObj=xmlDoc.documentElement; nodes = xmlDoc.documentElement.childNodes; document.frm.mainclass.options.length = 0; for (i=0;i<xmlObj.childNodes.length;i++){ labels=xmlObj.childNodes(i).getAttribute("display_name"); values=xmlObj.childNodes(i).text; document.frm.mainclass.add(document.createElement("OPTION")); document.frm.mainclass.options[i].text=labels; document.frm.mainclass.options[i].value=values; } } </script> <title>在HTML中调用XML数据</title> </head> <body bgcolor="#FFFFFF"> <FORM name="frm"> 类型<SELECT name="mainclass" ></SELECT> <option selected value="" ></option> </form> </body> </html>
xml:
<?xml version="1.0" encoding="GB2312"?> <item> <class display_name="用户ID"> USER_ID </class> <class display_name="密码">PWD </class> <class display_name="用户名称"> USERNAME </class> <class display_name="性别"> XB </class> <class display_name="部门ID">BMID </class> </item>