日期:2014-05-17  浏览次数:21025 次

如何读取XML?
<?xml   version= '1.0 '   encoding= 'gb2312 '?>
<TX>
<Item   ID= '1 '   Scale= '2 '   Notes= ' '/>
<Item   ID= '2 '   Scale= '1 '   Notes= ' '/>
<Item   ID= '3 '   Scale= '3 '   Notes= ' '/>
</TX>
如何循环读取Item中的。ID Scale Notes的值?
thanks

------解决方案--------------------
Set xml = Server.CreateObject( "Xml.DomDocument ")
xml.async = false
xml.load(变量) 变量如果直接是DOM结构的话
xml.loadXML(变量) 变量如果是XML结构的字符串的话

' ' ' '操作!

response.write xml.documentElement.xml
------解决方案--------------------
<%
Dim sXml
sXml = " <?xml version= '1.0 ' encoding= 'gb2312 '?> <TX> <Item ID= '1 ' Scale= '2 ' Notes= ' '/> <Item ID= '2 ' Scale= '1 ' Notes= ' '/> <Item ID= '3 ' Scale= '3 ' Notes= ' '/> </TX> "

' Create Xml Document
Dim objXml
Set objXml = Server.CreateObject( "MSXML2.DOMDocument ")

objXML.Async = False
objXml.LoadXML(sXml)

Dim objRoot
Set objRoot = objXml.selectSingleNode( "TX ")

Dim i
Dim node

For Each node In objRoot.ChildNodes
response.write " NodeName: " & node.NodeName
response.write " ID: " & node.GetAttribute( "ID ")
response.write " Scale: " & node.GetAttribute( "Scale ")
response.write " Notes: " & node.GetAttribute( "Notes ")
response.write " <BR> "
Next

%>
------解决方案--------------------
dim xmlStr
xmlStr = " <?oxml version= '1.0 ' encoding= 'gb2312 '?> <TX> <Item ID= '1 ' Scale= '2 ' Notes= ' '/> <Item ID= '2 ' Scale= '1 ' Notes= ' '/> <Item ID= '3 ' Scale= '3 ' Notes= ' '/> </TX> "


dim oxml
set oxml = createobject( "msxml.domdocument ")
oxml.async = false
oxml.loadXML(xmlStr)

if oxml.parseError.errorCode <> 0 then
response.write oxml.parseError.errorCode
response.end
else

dim items
set items = oxml.selectNodes( "//Tx/item ")
dim i
for each i in items
dim c
set c = items(i)
response.write(c.getNamedItem( "id ").value & " --- " & c.getNamedItem( "scale ").value & " --- " & c.getNamedItem( "notes ").value & " <br> ")
next
set items = nothing
end if
set oxml = nothing
------解决方案--------------------
dim str,objXML,childs
str= " <?xml version= '1.0 ' encoding= 'gb2312 '?> <TX> <Item ID= '1 ' Scale= '2 ' Notes= ' '/> <Item ID= '2 ' Scale= '1 ' Notes= ' '/> <Item ID= '3 ' Scale= '3 ' Notes= ' '/> </TX> "
set objXML=Server.CreateObject( "Microsoft.XMLDOM ")
objXML.loadXML(str)
set childs=objXML.selectNodes( "//Item ")
for i=0 to childs.length-1
response.write "ID= "& childs(i).getAttribute( "ID ") & " <br> "