XML问题,读取一个网页的信息,把读取到的信息按照个数输出到浏览器上
本帖最后由 wei395107171 于 2012-09-13 13:50:43 编辑 读取后台信息,然后输出到浏览器上,后台读取到的信息是下面这样的一个xml格式的
<?xml version="1.0" encoding="UTF-8"?>
<term seqno="1">
<command name="help" app=" sunflyspa">
<commandName>dll</commandName>
<commandName>set</commandName>
<commandName>sip</commandName>
<commandName>logfilesize</commandName>
<commandName>monitor</commandName>
<commandName>about</commandName>
</command>
</term>
要把里面的值(help,sunflyspa,dll,set...)输出到浏览器上的文本框中,不会写呀,求代码
后台是可以通过它的url访问到
[解决办法]
var xmlParse = function(str)
{
if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined')
{
var doc = new ActiveXObject('Microsoft.XMLDOM');
doc.loadXML(str);
return doc;
}
if (typeof DOMParser != 'undefined')
{
return (new DOMParser()).parseFromString(str, 'text/xml');
}
return createElement('div', null);
}
var xml = '<?xml version="1.0" encoding="UTF-8"?>\
<term seqno="1">\
<command name="help" app=" sunflyspa">\
<commandName>dll</commandName>\
<commandName>set</commandName>\
<commandName>sip</commandName>\
<commandName>logfilesize</commandName>\
<commandName>monitor</commandName>\
<commandName>about</commandName>\
</command>\
</term>';
var doc = xmlParse( xml );
function getNodeValues( root ) {
var values = [];
var attrs = root.attributes;
for( var i = 0; attrs && i < attrs.length; i++ ) values.push( attrs[ i ].nodeName + ':' + attrs[ i ].nodeValue );
if( root.nodeType == 3 && root.nodeValue.replace( /^\s+
[解决办法]
\s+$/g, '' ) ) values.push( root.nodeValue );
var children = root.childNodes;
for( var i = 0; i < children.length; i++ ) {
values = values.concat( getNodeValues( children[ i ] ) )
}
return values;
}
var result = getNodeValues( doc.documentElement );
alert( result )