XmlReader
function XmlManager(){
/// <summary>
/// 管理 XmlReader 类. 使用方法参见示例代码 JsDemons/Html/XmlDemon.js.
/// 请尊重作者劳动, 引用需注明出处.
/// Howard.Queen@hotmail.com, 2008-04-24.
/// http://howard-queen.cnblogs.com/
/// 2008-07-10
/// 1, 实现了 Firefox 下的所有功能. 完美支持 Firefox.
/// 2, 加入 GetSingleNode 方法, 试图提高算法效率.
/// </summary>
if ((typeof document.implementation != 'undefined')
&& (typeof document.implementation.createDocument != 'undefined')){
XmlReader.prototype = {
_Init: function(){
this._xmlDoc = document.implementation.createDocument("", "", null);
this._xmlDoc.async = false;
this._domParser = new DOMParser();
this._nsResolver = null;
},
GetRoot: function(name){
return this.GetSingleNode(this._xmlDoc.documentElement, '/' + name);
},
GetSingleNode: function(parentNode, targetChildName){
return this._xmlDoc.evaluate(targetChildName, parentNode, this._nsResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
},
GetNodes: function(parentNode, targetChildName){
var items = this._xmlDoc.evaluate(targetChildName, parentNode, this._nsResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var nodes = new Array();
for( var i = 0; i < items.snapshotLength; i++){
nodes.push(items.snapshotItem(i));
}
return nodes;
},
GetNodeValue: function(targetNode){
if(targetNode.childNodes.length > 0)
return targetNode.childNodes[0].nodeValue;
else
return '';
},
GetAttribute: function(targetNode, attributeName){
return targetNode.getAttribute(attributeName)
},
LoadXml: function(xmlText){
var childNodes = this._xmlDoc.childNodes;
for (var i = childNodes.length - 1; i >= 0; i--)
this._xmlDoc.removeChild(childNodes[i]);
this._xmlDoc.appendChild(this._xmlDoc.importNode(this._domParser.parseFromString(xmlText, "text/xml").documentElement, true));
this._nsResolver = this._xmlDoc.createNSResolver(this._xmlDoc.documentElement);
},
LoadUrl: function(url){
this._xmlDoc.load(url);
this._nsResolver = this._xmlDoc.createNSResolver(this._xmlDoc.documentElement);
}
}
}
else if (typeof window.ActiveXObject != 'undefined'){//IE浏览器
XmlReader.prototype = {
_Init: function(){
this._xmlDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
this._xmlDoc.async = false;
while(this._xmlDoc.readyState != 4) {
};
},
GetRoot: function(name){
return this.GetSingleNode(this._xmlDoc.documentElement, '/' + name);
},
GetSingleNode: function(parentNode, targetChildName){
return parentNode.selectSingleNode(targetChildName);
},
GetNodes: function(parentNode, targetChildName){
return parentNode.selectNodes(targetChildName);
},
GetNodeValue: function(targetNode){
return targetNode.text;
},
GetAttribute: function(targetNode, attributeName){
return targetNode.getAttribute(attributeName)
},
LoadXml: function(xmlText){
this._xmlDoc.loadXML(xmlText);
},
LoadUrl: function(url){
this._xmlDoc.load(url);
}
}
}
else{
throw {name:"ExplorerException",message:"不可识别的浏览器, 创建 XmlManager 失败."};
}
}
function XmlReader(){
/// <summary>
/// 用于同步读取 Xml 的类. 使用方法参见示例代码 JsDemons/Html/XmlDemon.js.
/// 请尊重作者劳动, 引用需注明出处.
/// Howard.Queen@hotmail.com, 2008-04-24.
/// </summary>
this._xmlDoc = null;//[私有]xml 文档.
this._Init();
}
XmlReader.prototype = {
_Init: function(){
/// <summary>[私有]初始化.</summary>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
GetRoot: function(name){
/// <summary>获取根节点.</summary>
/// <param name="name" type="String">根名称.</param>
/// <returns>根节点对象.</returns>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
GetSingleNode: function(parentNode, targetChildName){
/// <summary>获取首个子节点.</summary>
/// <param name="parentNode" type="XmlNode">父节点.</param>
/// <param name="targetChildName type="String">子节点名称.</param>
/// <returns>首个子节点.</returns>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
GetNodes: function(parentNode, targetChildName){
/// <summary>获取子节点集合.</summary>
/// <param name="parentNode" type="XmlNode">父节点.</param>
/// <param name="targetChildName type="String">子节点名称.</param>
/// <returns>子节点集合.</returns>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
GetNodeValue: function(targetNode){
/// <summary>获取节点的内容.</summary>
/// <param name="targetNode" type="XmlNode">目标节点.</param>
/// <returns>节点内容.</returns>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
GetAttribute: function(targetNode, attributeName){
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
LoadXml: function(xmlText){
/// <summary>加载 xml 文本.</summary>
/// <param name="xmlText" type="String">xml 文本.</param>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
},
LoadUrl: function(url){
/// <summary>加载 xml 文件.</summary>
/// <param name="url" type="Uri">xml 文件 Uri.</param>
throw {name:"",message:"请在实例化 XmlReader 前创建一个 XmlManager 实例."};
}
}
Code
try{
//---------------------
//配置 XmlReader 类以适应当前浏览器环境.
var xm = new XmlManager();
//创建 XmlReader 实例.
var xr = new XmlReader();
//自定义处理函数
function Show(){
//开始输出.
var root = xr.GetRoot("Nodes");
var nodes = xr.GetNodes(root,"Node");
for(var i = 0;i<nodes.length;i++){
var name = xr.GetNodes(nodes[i], "Name")[0];
var sex = xr.GetNodes(nodes[i], "Sex")[0];
var age = xr.GetNodes(nodes[i], "Age")[0];
document.write("Name: " + xr.GetNodeValue(name) + " Sex: " + xr.GetNodeValue(sex) + " Age: " + xr.GetNodeValue(age) + "<br/>");
}
}
//---------------------
//应用1, 通常调用1.
document.write("LoadXml:<br/>");
var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Nodes><Node><Name>Q</Name><Sex>Boy</Sex><Age>10</Age></Node><Node><Name>I</Name><Sex>Girl</Sex><Age>11</Age></Node></Nodes>";
xr.LoadXml(xml);
Show();
document.write("<br/>");
document.write("ReLoadXml:<br/>");
xr.LoadXml(xml);
Show();
}
catch(e){//跟踪错误
alert(e.name + ' ' + e.message);
}
应该是你的小马虎造成的问题. 包装过类似应用, 但对如何运用