一Requirement
ApplicationgetElementsByTagName Method to access the data in the XML document by name
Second analysis
Access the XML document by name
First apply ActiveX#. ##ObjectCreate a Microsoft parser instance, and then load the XML document into memory
Then use the getElementsByTagName() method to obtain thereference## of the number element, name element and object element. #, the return result is an array, each element in the array corresponds to an element in the XML document, and the order is the same. Finally get the value of the text contained in the corresponding element, and
StringThree Notes##Start counting from 0 in the subscript of JavaScript
.AttributeDescribes the need to access the text contained in the name element, rather than accessing the name element itself; the data attribute obtains the value of the node. Four codes
<?xml version="1.0" encoding="GB2312"?> <employes> <employe id='1' attendence='经理'> <number>1001</number> <name>程**</name> <object>PHP</object> <tel>84971547</tel> <address>长春市</address> <e_mail>cak**@sina.com</e_mail> </employe> <employe id='2' attendence='员工'> <number>1002</number> <name>龙**</name> <object>SQL</object> <tel>1234556</tel> <address>武汉市</address> <e_mail>ak**@sina.com</e_mail> </employe> </employes>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>应用名称访问XML文档</title> </head> <script> function get_xml() { var xmldoc,employesNode,employeNode,peopleNode; //定义变量 var nameNode,titleNode,numberNode,displayText; //定义变量 xmldoc = new ActiveXObject("Microsoft.XMLDOM"); //创建Microsoft解析器实例 xmldoc.async = false; xmldoc.load("index.xml"); //载入指定的XML文档 numberNode=xmldoc.getElementsByTagName("number")[1]; //获取number元素的引用,访问员工的第2条信息 nameNode=xmldoc.getElementsByTagName("name")[1]; //获取name元素的引用 objectNode=xmldoc.getElementsByTagName("object")[1]; telNode=xmldoc.getElementsByTagName("tel")[1]; //实现字符串的拼接,输出XML文档中的数据 displayText="员工信息:"+numberNode.firstChild.data+','+nameNode.firstChild.data+', '+objectNode.firstChild.data+','+telNode.firstChild.data; div.innerHTML=displayText; //指定在ID标识为div的<div>标签中输出字符串displayText的信息 } </script> <body> <h1>应用名称访问XML文档</h1> <!--应用onClick事件调用函数get_xml()--> <input type="button" value="获取XML中的指定数据" onClick="get_xml()"> <div id="div"></div> </body> </html>
Five running results
The above is the detailed content of Detailed explanation of code cases for accessing XML documents by application name. For more information, please follow other related articles on the PHP Chinese website!