// 遍历xml对象
function tree(Element) {
  var node = "";
  if(Element.nodeType != 3) {
    node = Element;
    onElement(Element);
  }
  if(Element.nodeType == 3)
    onData(Element);
  if(Element.hasChildNodes) {
    for(var i=0;i<Element.childNodes.length;i++) {
      tree(Element.childNodes(i));
    }
  }
  if(node)
    endElement(node);
}/***** 以下三个函数请根据需要自行修改 *****/
// 节点开始
function onElement(Element) {
  s += "<" + Element.nodeName;
  n = Element.attributes.length
  if(n>0) {  // 若该节点有参数
    for(var i=0;i<n;i++)
      s += ' ' + Element.attributes(i).name + '=\"' + Element.attributes(i).value + '"';
  }
  s += ">";
}// 节点结束
function endElement(Element) {
  s += "</" + Element.nodeName + "><br>";
}// 节点数据
function onData(Element) {
  s += Element.nodeValue
}

解决方案 »

  1.   

    忘了说明,我不是写javascript,而是用c#写的。
      

  2.   

    Chaining an XmlReader to an XmlWriter 
    In certain cases one may want to perform more sophisticated manipulation of an XML file besides merely appending elements to the root element. For example, one may want to filter out every entry in a log file that doesn't meet some particular criteria before archiving the log file. One approach to doing this would be loading the XML file into an XmlDocument and then selecting the events that one is interested in via XPath. However, doing so involves loading the entire document into memory, which may be prohibitive if the document is large. Another option would involve using XSLT for such tasks, but this suffers from the same problem as the XmlDocument approach since the entire XML document has to be in memory. Also, for developers unfamiliar with XSLT, there is a steep learning curve when figuring out how to use template matches properly. One approach to solving the problem of how to process a very large XML document is to read in the XML using an XmlReader and write it out as it is being read with an XmlWriter. With this approach the entire document is never in memory at once and more granular changes can be made to the XML than simply appending elements. The following code sample reads in the XML document from the previous section and saves it as an archive file after filtering out all the events whose ip element has the value "127.0.0.1". using System;
    using System.Xml; 
    using System.IO;
    using System.Text;
    public class Test2{
      static string ipKey;
      static string httpMethodKey;
      static string fileKey; 
      static string dateKey;
      static string referrerKey;   public static void WriteAttributes(XmlReader reader, XmlWriter writer){
        
        if(reader.MoveToFirstAttribute()){
          do{
       writer.WriteAttributeString(reader.Prefix, 
                    reader.LocalName, 
                    reader.NamespaceURI,
                    reader.Value); 
          }while(reader.MoveToNextAttribute());
          reader.MoveToElement(); 
        }
      }  public static void WriteEvent(XmlWriter writer, string ip,
                                     string httpMethod, string file,
                                     string date, string referrer){
        
        writer.WriteStartElement("event"); 
        writer.WriteElementString("ip", ip);
        writer.WriteElementString("http_method", httpMethod);
        writer.WriteElementString("file", file);
        writer.WriteElementString("date", date);    
        if(referrer != null) writer.WriteElementString("referrer", referrer);
        writer.WriteEndElement();   }   public static void ReadEvent(XmlReader reader, out string ip,
                                  out string httpMethod, out string file,
                                  out string date, out string referrer){    ip = httpMethod = file = date = referrer = null;     while( reader.Read() && reader.NodeType != XmlNodeType.EndElement){                
          
     if (reader.NodeType == XmlNodeType.Element) {
              
         if(reader.Name == ipKey){   
           ip = reader.ReadString(); 
         }else if(reader.Name == httpMethodKey){ 
           httpMethod = reader.ReadString();
         }else if(reader.Name == fileKey){ 
           file = reader.ReadString();
         }else if(reader.Name == dateKey){ 
           date = reader.ReadString();
           // reader.Read(); // consume end tag
         }else if(reader.Name == referrerKey){ 
           referrer = reader.ReadString();
          }
            }//if 
        }//while   
      }  public static void Main(string[] args){
        string ip, httpMethod, file, date, referrer; 
        //setup XmlNameTable with strings we'll be using for comparisons
        XmlNameTable xnt = new NameTable(); 
        ipKey            = xnt.Add("ip"); 
        httpMethodKey    = xnt.Add("http_method"); 
        fileKey          = xnt.Add("file");
        dateKey          = xnt.Add("date");
        referrerKey      = xnt.Add("referrer");
        
        //load XmlTextReader using XmlNameTable above 
        XmlTextReader xr = new XmlTextReader("logfile.xml", xnt);
        xr.WhitespaceHandling = WhitespaceHandling.Significant;    XmlValidatingReader vr = new XmlValidatingReader(xr);
        vr.ValidationType = ValidationType.None;
        vr.EntityHandling = EntityHandling.ExpandEntities; 
        StreamWriter sw =  
          new StreamWriter ("logfile-archive.xml", false, Encoding.UTF8 ); 
        XmlWriter xw    = new XmlTextWriter (sw);                 
        
        vr.MoveToContent(); // Move to document element   
        xw.WriteStartElement(vr.Prefix, vr.LocalName, vr.NamespaceURI);
        WriteAttributes(vr, xw);    
         
        vr.Read(); // Move to first <event> child of document element
        // Write out each event that isn't from 127.0.0.1 (localhost)
        do
        {
          ReadEvent(vr, out ip, out httpMethod, 
                   out file, out date, out referrer);
          if(!ip.Equals("127.0.0.1")){
            WriteEvent(xw,ip, httpMethod, file, date, referrer); 
          }
          vr.Read(); //move to next <event> element or end tag of <logfile>
        } while(vr.NodeType == XmlNodeType.Element);
         
        Console.WriteLine("Done");
        
        vr.Close();
        xw.Close();
      }
    }The code sample above results in the following output being written to the logfile-archive.xml file:<logfile>
     <event>
       <ip>192.168.0.1</ip>
       <http_method>POST</http_method>
        <file>comments.aspx</file>
        <date>1999-05-05T19:25:13.238220-08:00</date>
      </event>
    </logfile>The one interesting point in the above code, besides the fact that it uses a chaining of an XmlReader to an XmlWriter, is that it uses the NameTable to improve the performance of text comparisons when checking the tag names of elements in the ReadEvent() method. The benefits of using this approach to checking the tag names of elements in the XmlReader are outlined in the MSDN documentation topic Object Comparison Using XmlNameTable with XmlReader.
      

  3.   

    谢谢,先看看。
    我的xml是很不确定的,由html生成的xml串,没有固定的格式。
      

  4.   

    public void Delegate ElementNodeHandler(XmlNode);
     public event Delegate NodeFindedEvent;
    public void WalkTree(xmlNode currNode)
    {
      if(currNode.HasChildNodes)
      {
        int iNumChildNodes = currNode.ChildNodes.Count;
        int i;
        for(int i=0;i<=iNumChildNodes-1;i++)
        {
          XmlNode nextNode = currNode.ChildNodes[i];
          switch(nextNode.NodeType)
          {
           case XmlNodeType.Document;
                 break;
           case XmlNodeType.XmlDeclaration;
                 break;
           case XmlNodeType.Element;
                   //具体节点的处理,最好是一个事件或接口,可达到重用 
                   NodeFindedEvent(nextNode);
                   break;
          }
          WalkTree(nextNode);        
                   
          }
        }  
        }
      

  5.   

    从开始就可以遍历,因为XMLDOCUMENT就是XMLNODE