<root>
<p_node province= "北京 ">
<c_item city= "北京 " post_code= "100000 " area_code= "010 "/>
</p_node>
</root>
js有方法,单独的取city值(北京)吗?
------解决方案--------------------
一个节点的属性值存储在attributes中。
如上例:
var avalue = c_item节点.attributes;
avalue[0]=北京,avalue[1]=100000,...
avalue.length为属性个数。
------解决方案--------------------
//a.html
<html>
<head>
<title> test.html </title>
<script>
function window_onload(){
var xmlDoc= new ActiveXObject( 'Microsoft.XMLDOM ');
xmlDoc.async=false;
xmlDoc.load( "test.xml ");
var city=xmlDoc.getElementsByTagName( 'p_node ')[0].childNodes[0].getAttribute( "city ");
alert(city);
}
</script>
</head>
<body onload= "window_onload() ">
</body>
</html>
//test.xml
<root>
<p_node province= "北京 ">
<c_item city= "北京 " post_code= "100000 " area_code= "010 "/>
</p_node>
</root>
------解决方案--------------------
//取得XML文件标签对应值
//xmlObj为XML对象,tag为需取得的标签名,第三个参数(可选)为标签的attribute的名称
function $X(xmlObj, tag){
if(arguments.length > 2) return xmlObj.getElementsByTagName(tag)[0].getAttribute(arguments[2]);
else return xmlObj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}
//调用
var city = $X(xmlObj, "root/p_code/c_item ", "city ");
------解决方案--------------------
selectNode(//@city= '北京 ')
XmlDoc.SelectNodes( "/root/c_item ")[0].GetAttribute( "city ");