First,you can use new ActiveXObject("Microsoft.XMLDOM").loadXML(strXML) to parse the string to a XmlDomDocument, then you can use DOM to get the attribute and nodeValue and so on.

解决方案 »

  1.   

    fason(forbes Pu)兄,    谢谢你的答复,能否给个相关例子,我试了行不通.不知为何.
        我的做法是:
        var strXML="<Row ID='RowHead' ReadOnly='false' Type='' Style='VISIBILITY: hidden'><cell ID='FKID' ReadOnly='true' Style='normal' Visible='ture' ControlType='Label' DataType='int' Colspan='1'  RowSpan='1'>FKID-POMID</cell></Row>"
        var strData = new activeXObject("Microsoft.XMLDOM").loadXML(strXML);
        再用For 语句,获取strData的值和属性,不行.
      

  2.   

    <SCRIPT LANGUAGE="JavaScript">
    <!--
    var strXML="<Row ID='RowHead' ReadOnly='false' Type='' Style='VISIBILITY: hidden'><cell ID='FKID' ReadOnly='true' Style='normal' Visible='ture' ControlType='Label' DataType='int' Colspan='1'  RowSpan='1'>FKID-POMID</cell><cell ID='FKID1' ReadOnly='true' Style='normal' Visible='ture' ControlType='Label' DataType='int' Colspan='1'  RowSpan='1'>CSDN</cell></Row>";
    var myDoc = new ActiveXObject("Msxml2.DOMDocument");
    myDoc.async = false;
    myDoc.resolveExternals = false;
    myDoc.validateOnParse = false;
    myDoc.loadXML(strXML);
    document.write("<table border=1>");
    var oTr = myDoc.selectSingleNode("//Row");
    document.write("<tr id=" + oTr.getAttribute("ID") + ">");
    var oTdList = oTr.selectNodes("//cell");
    for(var i=0; i<oTdList.length; i++)
    {
        var oTd = oTdList.item(i);
        document.write("<td id=" + oTd.getAttribute("ID") + ">" + oTd.text + "</td>");
    }
    document.write("</tr></table>")
    //-->
    </SCRIPT>
      

  3.   

    LxcJie(肖冲),   谢谢了.我现在已可以获取节点的属性了,只是我是用oRoot1.childNodes[0].getAttribute("ID")和你所说的方法有区别吗?
       还有我之前是加载一个XML文件,用var abc=myDoc.documentElement后.可以用Alert(abc)把XML的值显示出来.而现在则不行.  谢谢了.马上给分.
      

  4.   

    myDoc.async 设置的是什么?
      

  5.   

    <html>
    <head>
    <style>
    table
    {
    border-collapse: collapse;
    border-width: 4; 
    border-style: double; 
    border-color:#15336F;
    font-size:12px;
    }
    body
    {
    font-size:12px;
    }
    div
    {
    width:100%;
    height:9; 
    border-style:solid; 
    border-width:1; 
    border-color:#eeeeee;
    vertical-align:top;
    font-size:12;
    cursor:hand;
    }
    </style>
    <title>笨狼树状节点查看器</title>
    </head>
    <body>
     <INPUT type="file" id=file1 name=file1>请输入xml文件路径
     <INPUT type="button" value="确定" onclick = "vbs:analyse ">
     <SELECT id="select1" onchange="vbs:analyse">
    <OPTION value="nodeName" >显示标签</OPTION>
    <OPTION  value="text" >显示文字</OPTION>
    <OPTION  value="attribute" >显示属性</OPTION>
     
    <OPTION  value="XPath" >显示XPath</OPTION>
    </SELECT>
    <DIV id="oList" style="padding-left:0"></DIV>
     
    </body>
     <script language="vbScript" >
    '**************************************
    '****作者: 超级大笨狼 superdullwolf****
    '**************************************
     
    public dic,favour,anything ,doc   
     
    set doc = CreateObject("Microsoft.XMLDOM")
    doc.async=False
    sub analyse()
    dim myTR 
    favour = select1.value
    removeDIV  
    if not doc.load(file1.value) then 
    alert "文件加载失败,请检查文件是否存在!"
    else
    Set rootNode = doc.DocumentElement
    set rootDIV = document.createElement("DIV")

    oList.setAttribute "XPath",rootNode.nodeName   
    oList.setAttribute "parsed",false
    appendDIV  oList,rootNode
     
     
    end if
    end sub

    sub appendDIV(myDIV,myNode)
     
    dim myChild ,newDIV,ChildID,thisID
    ChildID = 0

    for each myChild in myNode.childNodes
    set newDIV = document.createElement("DIV")
    myDIV.appendChild newDIV

    addPx newDIV, myDIV,10

    if ChildID>0 then  
    if myChild.previousSibling.nodeName <> myChild.nodeName then
      ChildID =0 
     
     end if   
    end if

    newDIV.setAttribute "XPath",getXPath(myDIV,myChild.nodeName,ChildID)
    newDIV.setAttribute "parsed",false

    newDIV.title = newDIV.getAttribute("XPath") 
    newDIV.innerText = getText(myChild,newDIV) 

    if myChild.childNodes.length>0 then 
    if  myChild.nodeName <> "#text" then
    newDIV.attachEvent "onclick",GetRef("attachOnclick") 
    end if
    end if
     ChildID = ChildID + 1
    next
    myDIV.setAttribute "parsed",true
    end sub

    function getXPath(myDIV,name,index)  
    getXPath = myDIV.getAttribute("XPath") & "/" & name & "[" & index & "]"
     
    end function

    sub removeDIV()  
    dim oldDIV
    for each  oldDIV in   oList.childNodes   
        oldDIV.removeNode(true)  
     next  
    end sub

    sub attachOnclick()
    dim obj ,nodeXPath,cDIV
    set obj=window.event.srcElement 
    nodeXPath = obj.getAttribute("XPath")
    if instr(nodeXPath,"#text") >0 then 
    window.event.cancelBubble = true
    exit sub
    end if
    if not obj.getAttribute("parsed")= true then  
     appendDIV obj ,doc.selectSingleNode(nodeXPath)
    else
    for each cDIV in obj.children
    if cDIV.style.display = "none" then
    cDIV.style.display = ""
    else
    cDIV.style.display = "none"
    end if
    next
    end if
    window.event.cancelBubble = true
     
    end sub

    function getText(myNode,oDIV)
    dim myAttribute
    getText = ""
    select case favour
    case "text"
    if not isnull(myNode.text) then
    getText = myNode.text
     else
    getText = "空文字"
     end if
    case "nodeName"  
    getText = myNode.nodeName
    case "attribute"
    if myNode.nodeName <>"#text" then
    for each myAttribute in  myNode.attributes  
    getText =getText &  myAttribute.name
    getText = getText & "=" & chr(34) 
    getText = getText & myAttribute.value  & chr(34) & " "
    next
    getText = trim(getText)
    end if
        
     
    case "XPath"
    getText = oDIV.title
    end select
    if trim(getText) ="" then getText ="空"
    end function

    sub addPx(newDIV,oldDIV,num)
    dim re,myString
    set re = new RegExp
    re.Global = true
    re.Pattern = "[^\d]*"
    myString =  re.Replace(oldDIV.style.paddingLeft, "")
    if myString ="" then myString = "0"
    myString = (cint(myString) + num ) & "px"
    newDIV.style.paddingLeft = myString
    set re = nothing
    end sub
     </script>
    </html>
      

  6.   

    <html>
    <head>
    <style>
    table
    {
    border-collapse: collapse;
    border-width: 4; 
    border-style: double; 
    border-color:#15336F;
    font-size:12px;
    }
    body
    {
    font-size:12px;
    }
    </style>
    <body>
     <INPUT type="file" id=file1 name=file1>请输入xml文件路径
     <INPUT type="button" value="确定" onclick = "vbs:analyse ">
     <table border="1" id=xmlInfo>
     <tr id=myTR1>
      <td> 节点名称 </td><td> 属性名称 </td>
     </tr>
      </table>
      </body>
      <title>笨狼xml查看节点和属性工具</title>
    </head>
     <script language="vbScript" >
    '**************************************
    '****作者: 超级大笨狼 superdullwolf****
    '**************************************

    public dic
    set dic = CreateObject("Scripting.Dictionary")  
     
    sub analyse()
     
    dim doc
    set doc = CreateObject("Microsoft.XMLDOM")
    doc.async=False
    dic.RemoveAll
    if not doc.load(file1.value) then 
    alert "文件加载失败,请检查文件是否存在!"
    else
    Set rootNode = doc.DocumentElement
    getnode rootNode
    '清空行
    removeRows xmlInfo
    addRow xmlInfo
    end if
    end sub

    sub addRow(TB)
    '添加行
    dim myTR,myTD,kkk
    for each kkk in dic.Keys
    set myTR = TB.insertRow
    set myTD = myTR.insertCell
    myTD.innerText = kkk 
    set myTD = myTR.insertCell

    if len(trim(dic.Item(kkk))) > 0 then 
    myTD.innerText = dic.Item(kkk) 
    else
    myTD.innerText =  "" 
    end if
    next
    end sub

    sub removeRows(TB)
    dim TR
    for each  TR in   TB.rows    
      if TR.rowIndex>0 then  TR.removeNode(true)  
     next
    end sub

    sub getnode(myNode)
    on error resume next
    for each childNode in myNode.childNodes
    if childNode.nodeName<>"#text" then 
    if not(dic.Exists(childNode.nodeName)) then
    if childNode.Attributes.length>0 then
    dic.Add childNode.nodeName,  makeString(childNode.Attributes )
    else
    dic.Add childNode.nodeName,""
    end if
    else
    dic.Item(childNode.nodeName) = exclude(dic.Item(childNode.nodeName),childNode.Attributes )
    end if
    end if
    if childNode.childNodes.length > 0 then getnode childNode
    next
    end sub function exclude(myString,collection)
    dim sss,ccc,kkk,d
    set d = CreateObject("Scripting.Dictionary") for each sss in split(myString,",")
    if not d.Exists(sss) then d.Add sss,sss   
    next

    for each ccc in collection
    if not d.Exists(ccc.name) then d.Add ccc.name,ccc.name   
    next for each kkk in d.Keys
    exclude = exclude  & kkk  & " "
    next exclude = replace(trim(exclude)," ",",")
    end function

    function makeString(collection)
    dim ccc
    makeString = " "
     
    for each ccc in  collection 
    makeString = makeString & ccc.name & " "
    next
    makeString = replace(trim(makeString)," ",",")
    end function 

     </script>
    </html>