XML - 如何访问元素的属性?
我正在使用 ColdFusion。我正在尝试访问元素的属性,但不太清楚该怎么做。该元素的名称是 Package。该属性的名称是 ID。我需要获取包裹 ID。
<cfset Packages = xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package')>
我已经上传了我想要获取的 id 的图像。
http://evikjames.com/SteinAir/xml.jpg
我发誓我已经尝试了所有可能的组合并到处寻找答案。但是,运气好。你能帮忙吗?
我已经尝试过下面提供的解决方案。我无法让它为我工作。看来我成功地将Package ID的值放入变量中,但它只是一个数组。我似乎无法在数组中访问它。这是我的错误:
Complex object types cannot be converted to simple values.
这是我正在使用的代码:
<cfset PackageID = xmlSearch(MyXMLDoc, 'Package/Package/@ID[i]')>
<cfset Observations = xmlSearch(MyXMLDoc, " Package/Observations[i]")>
我可以输出观察结果,但无法输出 PackageID。
这是怎么回事?
I am using ColdFusion. I am trying to access an attribute of an element and can't quite figure out how to do it. The name of the element is Package. The name of the attribute is ID. I need to get the Package ID.
<cfset Packages = xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package')>
I have uploaded an image of the id that I am trying to get at.
http://evikjames.com/SteinAir/xml.jpg
I swear I have tried every possible combination and looked everywhere for the answer. But, luck. Can you help?
I have tried the solutions offered below. I can't get it to work for me. It seems that I successfully put the value of the Package ID into a variable, but it's just an array. I can't seem to access it in the array. Here's my error:
Complex object types cannot be converted to simple values.
Here's the code I am using:
<cfset PackageID = xmlSearch(MyXMLDoc, 'Package/Package/@ID[i]')>
<cfset Observations = xmlSearch(MyXMLDoc, " Package/Observations[i]")>
I can output Observations, but I can't output PackageID.
What's up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您遵循本指南,以下内容应该适合您(本质上,这是 XPath 语法):
If you follow this guide, the following should work for you (essentially, this is XPath syntax):
更新:修复了每个评论的属性名称大小写
如果只需要“id”,Abel 的方法会更灵活。但如果您还需要处理其他信息,那么其中任何一个都应该有效。只需注意大小写敏感问题。
Update: Fixed case of attribute name per comments
If only the "id" is needed, Abel's approach is slicker. But if you need to process other information as well, either of these should work. Just watch for case sensitivity issues.