我想从XML文件中获取值。如何在m1
,m2
和m3
?中创建数组?怎么办呢?
这是XML
<?xml version="1.0"?>
<language>
<menus>
<m1>HomePage</m1>
<m2>Contact</m2>
<m3>About Us</m3>
</menus>
</language>
这是我的ASP代码:
Set Menus = xmlDoc.selectNodes("//language/menus/*" )
MenuCount = Menus.length
For Each entry in Menus
If entry.tagName = "m1" Then
m1 = entry.text
elseif entry.tagName="m2" then
m2 = entry.text
elseif entry.tagName="m3" then
m3 = entry.text
End If
Next
答案 0 :(得分:1)
首先,我有必要指出所呈现的xml结构很差。如果<menus>
中的每个元素代表一个菜单,那么所有元素都应该具有相同的标记名称,例如<menu>
。使用1,2和3后缀表示xml设计者对标识符和值之间的区别感到困惑。如果这些值很重要(即,不能依赖文档中的序数位置)那么这些值应作为属性包含在内: -
<menus>
<menu position="1">HomePage</menu>
<menu position="2">Contact</menu>
<menu position="3">About Us</menu>
</menus>
至于把它们放在一个数组中,你的代码看起来非常接近,但是现在就去了。
Dim menuNodes: Set menuNodes= xmlDoc.selectNodes("/language/menus/menu" )
Redim menus(menuNodes.length - 1)
Dim i : i = 0
For Each menuNode in menu
menus(i) = menu.Text
i = i + 1
Next
答案 1 :(得分:0)
您是说您当前的ASP代码不起作用?从XML文件获取值并在ASP中使用它们的一种方法如下所示;
<%
Dim menuItems 'Array of menu items
Set objHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
objHTTP.open "GET","http://yourxmlfeed.xml",false
objHTTP.send
XMLData = objHTTP.responseText
' this code takes the raw RSSFeed and loads it into an XML Object
Set xmlFeed = Server.CreateObject("MSXML2.DomDocument.4.0")
xmlFeed.async = false
xmlFeed.LoadXml(XMLData)
Set objHTTP = Nothing
Set objItems = xmlFeed.getElementsByTagName("menus")
Set xmlFeed = Nothing
' loop over all the items in the XML Feed
For x = 0 to objItems.length - 1
Set objItem = objItems.item(x)
For Each objChild in objItem.childNodes
menuItems = menuItems & objChild.text & ","
Next
Next
response.write DeleteLastComma(menuItems) 'menuItems is your array of menu items
Function DeleteLastComma
...function to remove last comma off array
End Function
%>