XML 行集和 MsXML2
我有一个使用 Microsoft 行集架构从我的库存控制系统返回的行集。
但是,当使用 msxml2 读取文档时,我似乎无法访问数据(用 vbscript 编写)
<xml>
<s:schema>
<!-- Schema here -->
</s:schema>
<rs:data>
<z:row field="value" field1="value" />
</rs:data>
</xml>
要将其拉回来,我正在使用:
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", address, False
objXmlHttp.setRequestHeader "Content-Type", "text/xml"
objXmlHttp.Send strXml
Set objLst = XML_response.getElementsByTagName("data")
myValue = objLst.item(0).getAttribute("field")
但是我收到以下消息:
Microsoft VBScript runtime error '800a01a8'
Object required: 'objLst.item(...)'
这可能是我做的事情完全错误,如果是这样,请有人向我指出,因为我已经盯着它看了两个小时,但我无法理解。
I have a rowset coming back using the Microsoft rowset schema from my stock control system.
However when using msxml2 to read the document I don't seem to be able to access the data (Written in vbscript)
<xml>
<s:schema>
<!-- Schema here -->
</s:schema>
<rs:data>
<z:row field="value" field1="value" />
</rs:data>
</xml>
To pull this back I am using:
Set objXmlHttp = Server.CreateObject("Msxml2.ServerXMLHTTP")
objXmlHttp.open "POST", address, False
objXmlHttp.setRequestHeader "Content-Type", "text/xml"
objXmlHttp.Send strXml
Set objLst = XML_response.getElementsByTagName("data")
myValue = objLst.item(0).getAttribute("field")
However I am receiving the following message:
Microsoft VBScript runtime error '800a01a8'
Object required: 'objLst.item(...)'
This is probably me doing something totally wrong, if so could someone point it out to me please because I have stared at this for 2 hours now and I can't get it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给定一个如下表:
在 ADO(经典,使用版本 2.8 进行测试)可访问数据库中,您
可以使用将结果集保存到 XML 为
您提供 XML,如下所示:
要读取该数据,请从(本地、控制台)代码开始,如:
重要步骤:
如果 cscript readxml.vbs 成功,则很容易“移植”工作代码
到.asp(并单独处理服务器特定问题)
SelectionNamespaces
Given a table like:
in an ADO (classic, tested with version 2.8) accessible database, you
can save the resultset to XML using
That gives you XML like:
To read that data, start with (local, console) code like:
The important steps:
if cscript readxml.vbs succeeds, it's easy to 'port' the working code
to .asp (and deal with server specific problems separately)
SelectionNamespaces
让我们假设您发布的 XML a) 包含正确的命名空间别名,b) 它已正确加载到
XML_response
中。现在“正确”的方法是使用 XPath,但是在这种情况下我们可以使用更简单的代码来实现我们的目标。 MSXML(3 或更低版本)中的默认选择语言是 XSL 模式,此选择不理解命名空间别名,因此
的标记名称为“rs:data”而不是“数据”的观点。所以这是你做错的第一件事。另一件事是 getElementsByTag 返回节点集合,其中仅包含一个节点“rs:data”。您的代码尝试从该节点读取“field”属性,实际上它位于“z:row”的子节点上。
您的代码应如下所示:
Lets assume the XML you posted a) includes correct namespace aliases and b) that it has loaded in
XML_response
correctly.Now the "correct" way to do this is use XPath however we can achieve our goals with simpler code in this case. The default selection language in MSXML (3 or below) is XSL Pattern, this selection doesn't understand namespace aliases, so the
<rs:data>
has the tag name "rs:data" not "data" from its point of view. So thats the first thing you are doing wrong.Another thing is that
getElementsByTag
is returning a collection of nodes, which contains just one node the "rs:data". Your code is trying to read the "field" attribute from that node where in fact it on a child node of "z:row".Here is what you code should look like: