XML - 如何访问元素的属性?

发布于 2024-11-28 15:30:55 字数 749 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小姐丶请自重 2024-12-05 15:30:55

如果您遵循本指南,以下内容应该适合您(本质上,这是 XPath 语法):

<cfset IdAttributes= xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package/@ID')>

If you follow this guide, the following should work for you (essentially, this is XPath syntax):

<cfset IdAttributes= xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package/@ID')>
他是夢罘是命 2024-12-05 15:30:55

更新:修复了每个评论的属性名称大小写

如果只需要“id”,Abel 的方法会更灵活。但如果您还需要处理其他信息,那么其中任何一个都应该有效。只需注意大小写敏感问题。

<cfxml variable="str">
    <IntlRateV2Response>
        <Package ID="123">a</Package>
        <Package ID="456">b</Package>
        <Package ID="789">c</Package>
    </IntlRateV2Response>
</cfxml>

<cfset MyXMLDoc = xmlParse(str)>
<cfset packages = xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package')>

<cfloop array="#packages#" index="pack">
    <cfoutput>
        #pack.xmlAttributes["ID"]# OR
        #pack.xmlAttributes.ID#  
    </cfoutput>
</cfloop>

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.

<cfxml variable="str">
    <IntlRateV2Response>
        <Package ID="123">a</Package>
        <Package ID="456">b</Package>
        <Package ID="789">c</Package>
    </IntlRateV2Response>
</cfxml>

<cfset MyXMLDoc = xmlParse(str)>
<cfset packages = xmlSearch(MyXMLDoc, '/IntlRateV2Response/Package')>

<cfloop array="#packages#" index="pack">
    <cfoutput>
        #pack.xmlAttributes["ID"]# OR
        #pack.xmlAttributes.ID#  
    </cfoutput>
</cfloop>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文