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.
解决方案 »
- jqgrid表头合并和行合并,基于jquery的脚本插件
- 数学不等式 谁会哦?
- 如何删除字符串中的空格
- div的折行问题(如何能让div中的内容不折行呢)急!!!
- 一个<a>元素,如果点击时只是为了驱动一个js函数,该怎么写?
- 客户端技术(javascript vbscript等)能实现网站总流量和日流量统计吗?
- 如何实现当一个网页加载时!把该网页的浏览器的菜单,导航条!,按钮,地址栏关掉!
- 紧急求助!!如何屏蔽提示框?
- 我在一个table里做了一个树形菜单,但是当菜单项全部打开时table的height就会自动加长,如何使height保持不变?
- onclick,this的值应该怎样定义
- 为什么我加了src=**.js会报有错误
- 大家好, 如果用js判断一个对象的存在?
我的做法是:
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的值和属性,不行.
<!--
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>
还有我之前是加载一个XML文件,用var abc=myDoc.documentElement后.可以用Alert(abc)把XML的值显示出来.而现在则不行. 谢谢了.马上给分.
<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>
<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>