<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<script type="text/javascript">
//注意传入的xml字符串一定要有根节点
var xmlString = "<root><name>weiqi</name><age>23</age><sex>男</sex></root>";
var xmlDoc;
xmlDoc= new ActiveXObject("MSXml.DOMDocument");
xmlDoc.async="false"
/*注意这里载入字符串使用的是"loadXML()"方法)(而不是前面用过的"load()"方法),
"loadXML()"是用于载入字符串,"load()"是用于载入XML文档。
*/
xmlDoc.loadXML(xmlString);
function getValue(argue)
{
//alert(argue);调试用的^_^
var nameElement = xmlDoc.getElementsByTagName(argue);
alert(nameElement(0).text);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>XML文档载入/title>
</head>
<body>
<!---记住如果在双引号中还想加入带引号的变量记得要用单引号引起来 ^_^->
<button id="button1" name="button1" onclick="getValue('name');">
获取XML中name信息
</button>
<button id="button2" name="button2" onclick="getValue('age');">
获取XML中age信息
</button>
<button id="button3" name="button3" onclick="getValue('sex');">
获取XML中sex信息
</button>
</body>
</html>
<html>
<head>
<script type="text/javascript">
//注意传入的xml字符串一定要有根节点
var xmlString = "<root><name>weiqi</name><age>23</age><sex>男</sex></root>";
var xmlDoc;
xmlDoc= new ActiveXObject("MSXml.DOMDocument");
xmlDoc.async="false"
/*注意这里载入字符串使用的是"loadXML()"方法)(而不是前面用过的"load()"方法),
"loadXML()"是用于载入字符串,"load()"是用于载入XML文档。
*/
xmlDoc.loadXML(xmlString);
function getValue(argue)
{
//alert(argue);调试用的^_^
var nameElement = xmlDoc.getElementsByTagName(argue);
alert(nameElement(0).text);
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>XML文档载入/title>
</head>
<body>
<!---记住如果在双引号中还想加入带引号的变量记得要用单引号引起来 ^_^->
<button id="button1" name="button1" onclick="getValue('name');">
获取XML中name信息
</button>
<button id="button2" name="button2" onclick="getValue('age');">
获取XML中age信息
</button>
<button id="button3" name="button3" onclick="getValue('sex');">
获取XML中sex信息
</button>
</body>
</html>