使用经典 ASP 的 XML selectNodes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1432966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML selectNodes using Classic ASP
提问by JezB
XML problem that's got me stumped, but is probably very simple...
XML 问题让我很难过,但可能很简单......
The XML is like:
XML 是这样的:
<header>
<createdOn>16 Sep 2009</createdOn>
<createdBy>Jez</createdBy>
</header>
<agents>
<agent>
<agentDetails>
<agentName>text</agentName>
<agentTelephone>text</agentTelephone>
</agentDetails>
<properties>
<property>
<propertyid>number</propertyid>
<address>
<number>1</number>
<street>High St</street>
<postcode></postcode>
<country>UK</country>
</address>
<price>
<category>text</category>
<price>number</price>
<reference>text</reference>
</price>
<description>
<propertyType>House</propertyType>
<bedrooms>2</bedrooms>
<bathrooms>1</bathrooms>
<sleeps>
<briefDescription>text</briefDescription>
<addDescription>long-text</addDescription>
<floorSize>
<size>80</size>
<type>sq. mt</type>
</floorSize>
<bullets>
<bullet>No Of Bedrooms : 2</bullet>
<bullet>Condition : Habitable</bullet>
<bullet>Land Size (M2): 2,000</bullet>
</bullets>
</description>
<images>
<image>
<thumbnail>URL</thumbnail>
<image>URL</image>
<alttext></alttext>
</image>
<image>
<thumbnail>URL</thumbnail>
<image>URL</image>
<alttext></alttext>
</image>
</images>
<links>
<link>
<type>text</type>
<url>url</url>
</link>
<link>
<type>text</type>
<url>url</url>
</link>
</links>
</property>
</properties>
</agent>
</agents>
And the code I would like to use is:
我想使用的代码是:
Set NodeList = objXML.documentElement.selectNodes("agents/agent/properties/property")
For Each Node In NodeList
'I want to be able to extract distinct fields here...
response.write Node.selectSingleNode("address/street") & "<br/>"
response.write Node.selectSingleNode("description/briefDescription") & "<br/>"
Next
But, I don't know how.
但是,我不知道如何。
ALso, this could be a problem with, for example, the <images>and <links>tags.
此外,这可能是一个问题,例如<images>和<links>标签。
Suggestions please?
请提出建议?
采纳答案by Chris Nielsen
First, the XML example you've posted is invalid. It lacks a root element (or has multiple root elements, depending on your point of view). Also, the <sleeps>element is never closed. I think these might be typos in your example?
首先,您发布的 XML 示例无效。它缺少一个根元素(或者有多个根元素,这取决于您的观点)。此外,<sleeps>元素永远不会关闭。我认为这些在你的例子中可能是错别字?
I'm not sure what you mean by "I want to be able to extract distinct fields here." Can you give an example of the output that you are after?
我不确定“我希望能够在这里提取不同的字段”是什么意思。你能举一个你所追求的输出的例子吗?
Without more information, I can suggest trying some variation of this:
没有更多信息,我可以建议尝试一些变体:
Dim NodeList, Node, SubNode
'' # Note: Replace [root] with your actual root level element
Set NodeList = objXML.documentElement.selectNodes("/[root]/agents/agent/properties/property")
For Each Node In NodeList
'' # Do something useful... ?? Distinct fields??
Set Node = Node.selectSingleNode("address/street/text()")
If Not Node Is Nothing Then
Response.Write Server.HTMLEncode(Node.nodeValue) & "<br />"
End If
Next
Does this help?
这有帮助吗?
回答by JezB
The code I'm using is:
我正在使用的代码是:
Set NodeList = objXML.documentElement.selectNodes("agents/agent/properties/property")
For Each Node In NodeList
Set AddrNode = Node.selectSingleNode("address/street/text()")
if not AddrNode Is Nothing then response.write AddrNode.nodeValue & "<br/>"
set AddrNode = nothing
Set AddrNode = Node.selectSingleNode("address/region/text()")
if not AddrNode Is Nothing then response.write AddrNode.nodeValue & "<br/>"
set AddrNode = nothing
For Each ImgNode In Node.selectNodes("images/image")
Set ThNode = ImgNode.selectSingleNode("thumbnail/text()")
if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
set ThNode = nothing
Set ThNode = ImgNode.selectSingleNode("image/text()")
if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
set ThNode = nothing
Set ThNode = ImgNode.selectSingleNode("alttext/text()")
if not ThNode Is Nothing then response.write ThNode.nodeValue & "<br/>"
set ThNode = nothing
next
Next
I hope someone else finds its useful!
我希望其他人发现它有用!

