Home > Backend Development > XML/RSS Tutorial > Detailed explanation of code cases for accessing XML documents by application name

Detailed explanation of code cases for accessing XML documents by application name

黄舟
Release: 2017-03-30 14:04:30
Original
1898 people have browsed it

一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 the

reference## 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

String

Concatenate.

Three 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=&#39;1&#39; attendence=&#39;经理&#39;>
		<number>1001</number>
		<name>程**</name>
		<object>PHP</object>
		<tel>84971547</tel>
		<address>长春市</address>
		<e_mail>cak**@sina.com</e_mail>
	</employe>
	<employe id=&#39;2&#39; attendence=&#39;员工&#39;>
		<number>1002</number>
		<name>龙**</name>
		<object>SQL</object>
		<tel>1234556</tel>
		<address>武汉市</address>
		<e_mail>ak**@sina.com</e_mail>
	</employe>
</employes>
Copy after login
HTML code
<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+&#39;,&#39;+nameNode.firstChild.data+&#39;, &#39;+objectNode.firstChild.data+&#39;,&#39;+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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template