Xml文件的读取

来源:互联网 发布:一号店和淘宝的区别 编辑:程序博客网 时间:2024/05/18 21:42

 本文介绍了对Xml文件的操做使用:

一、如何简单的遍历Xml文件的信息

首先准备一个定义好的xml文件命名为book.xml,定义结构如下:

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  
<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    
<title>The Autobiography of Benjamin Franklin</title>
    
<author>
      
<first-name>Benjamin</first-name>
      
<last-name>Franklin</last-name>
    
</author>
    
<price>8.99</price>
  
</book>
  
<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    
<title>The Confidence Man</title>
    
<author>
      
<first-name>Herman</first-name>
      
<last-name>Melville</last-name>
    
</author>
    
<price>11.99</price>
  
</book>
  
<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    
<title>The Gorgias</title>
    
<author>
      
<name>Plato</name>
    
</author>
    
<price>9.99</price>
  
</book>
</bookstore>

接着我们使用xmlDocument来读取遍历一个xml文件将book.xml文件的信息输出,实现Xml文件遍历的.Net后台代码如下:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Xml;
public partial class _Default : System.Web.UI.Page 
{
    
private ArrayList NodesArrayList = new ArrayList();
    
protected void Page_Load(object sender, EventArgs e)
    
{
        XmlDocument xmlDoc 
= new XmlDocument();
        xmlDoc.Load(Server.MapPath(
"book.xml"));
        TraverseTree(xmlDoc.DocumentElement);
    }

    
//递归遍历xml文件。
    private void TraverseTree(XmlNode node)
    
{
        
if (node.HasChildNodes) //判断当前节点是否有子节点
        {
            node 
= node.FirstChild; //将当前节点移到第一个字节点
            DisplayNode(node); //显示当前节点

            TraverseTree(node); 
//调用函数本身,进行递归
        }

        
while (node.NextSibling != null//判断当前节点是否有兄弟节点
        {
            node 
= node.NextSibling; //将当前节点移到下一个兄弟节点
            DisplayNode(node);
            TraverseTree(node); 
//调用函数本身,进行递归
        }


    }

    
//显示输出xml文件信息。
    private void DisplayNode(XmlNode node)
    
{
        
string outStr = "";
        
if (node.NodeType == XmlNodeType.Element) //若当前节点为元素类型
        {
            outStr 
= "&lt;" + node.Name; //输出<name
            for (int i = 0; i < node.Attributes.Count; i++//输出当前节点的所有属性
            {
                outStr 
+= " " + node.Attributes[i].Name + "=" + """ + node.Attributes[i].Value + """;
            }

            
if (node.InnerText == ""//若当前元素节点为空
            {
                outStr 
+= "/&gt;<br>"//输出以/>结束
            }

            
else
            
{
                outStr 
+= "&gt;<br>"//否则以>结束
            }

        }

        
else if (node.NodeType == XmlNodeType.Text) //若当前节点为文本类型
        {
            outStr 
= node.Value + "<br>"//输出当前节点的值
        }

        
else
        

        
        }

        Response.Write(outStr);

    }

}

使用前台的JS脚本遍历Xml文件,具体实现代码如下:

// 访问Xml文件实例

function loadXML()
{
    
var xmlDoc;
    
//使用IE
    if (window.ActiveXObject)  
    
{  
        xmlDoc
=new ActiveXObject("Microsoft.XMLDOM"); 
        xmlDoc.async
=false
        xmlDoc.load(
"book.xml"); 
    }

   
// 使用 Mozilla, Firefox, Opera, etc.
   else if (document.implementation &&document.implementation.createDocument)
    

        xmlDoc
=document.implementation.createDocument("","",null); 
        xmlDoc.load(
"book.xml");  
        xmlDoc.onload
=getmessage;  
    }

    
else
    
{  alert('你的浏览器不支持此脚本');
    }

   
return xmlDoc.documentElement;
}

function TraverserTree(node)
{
    
if(node.childNodes.length>0)
    
{
        node
=node.firstChild;
        DisplayNode(node);
        TraverserTree(node);
    }

    
while (node.nextSibling != null//判断当前节点是否有兄弟节点
    {
        node 
= node.nextSibling; //将当前节点移到下一个兄弟节点
        DisplayNode(node);
        TraverserTree(node); 
//调用函数本身,进行递归
   }

}

  
var OutStr="";
 
function DisplayNode(node)
 
{
     
    OutStr 
+= "&lt;" + node.nodeName; //输出<name
    if(node.attributes!=null)
    
{
        
for (var i = 0; i < node.attributes.length; i++//输出当前节点的所有属性
        {
            OutStr 
+= " " + node.attributes[i].name + "=" + """ + node.attributes[i].value + """;
        }

    }

    
if (node.nodeValue == null//若当前元素节点为空
    {
        OutStr 
+= "/&gt;<br/>"//输出以/>结束
    }

    
else
    
{
        OutStr 
+= "&gt;<br/>"+node.nodeValue + "<br/>"//否则以>结束
    }

    document.getElementById(
"showxml").innerHTML=OutStr;
}