gpt4 book ai didi

xml - 使用 VBA 选择单个节点 XML 对象

转载 作者:数据小太阳 更新时间:2023-10-29 02:25:34 28 4
gpt4 key购买 nike

您好,我正在尝试访问下面的 XML 响应对象

<?xml version='1.0' encoding='UTF-8'?>
<gfi_message version="1.0">
<header>
<transactionId>123</transactionId>
<timestamp>2018-02-08T15:59:41+08:00</timestamp>
<processingTime>0.15</processingTime>
</header>
<body>
<response name="action1" function="PRICING" version="1.0">
<option name="data" ref="price169" />
</response>
<data format="NAME_VALUE" name="price169">
<node name="European Call">
<field name="Scenario" value="Trading" />
<field name="Currency" value="USD" status="input" />
<field name="CtrCcy" value="HKD" status="input" />
<field name="Strategy" value="Call" status="input" />
<field name="Model" value="Analytic" />
<field name="Class" value="European" status="input" />
<field name="Direction" value="Buy" status="input" />
<field name="Spot" value="7.81241/7.82871" />
<field name="Cutoff" value="TOK" />
<field name="Market" value="OTC" />
<field name="HorDate" value="15:59 Thu 8 Feb 18" status="input" />
<field name="ValDate" value="12 Feb 18" />
<field name="SpotDate" value="12 Feb 18" />
<field name="Maturity" value="Odd Date" />
<field name="ExDate" value="8 Feb 18" status="input" />
<field name="ExDays" value="0" />
<field name="ExTime" value="14:00 SGT" />
<field name="DelDate" value="12 Feb 18" />
<field name="DelDays" value="0" />
<field name="PremDate" value="Mon 12 Feb 18" />
<field name="PremType" value="Spot" />
<field name="Strike" value="7.81241" />
<field name="CtrStk" value="0.128001474576987" />
<field name="FwdWealth" value="0\-0.002082079933987" status="input" />
</node>
</data>
</body>
</gfi_message>

具体来说我只需要<field name="Spot" value="7.81241/7.82871" />

目前我正在尝试解析并尝试在调试器中打印语句,但我也没有得到任何响应

Dim objXML As MSXML2.DOMDocument30
Dim nodesThatMatter As MSXML2.IXMLDOMElement
Set nodesThatMatter = objXML.SelectNodes("//gfi_message/body/data/node")

For Each Node In nodesThatMatter
Debug.Print nodesThatMatter.SelectSingleNode("field").Text
Next

最佳答案

您可以通过简单地使用 /gfi_message/body/data/node/field[@name='Spot'] xpath

找到该特定节点

注意 - 避免在 xpaths 中使用 //,因为它很慢。

示例代码

Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
With objXML
.SetProperty "SelectionLanguage", "XPath"
.ResolveExternals = True
.ValidateOnParse = True
.Async = False
.Load "C:\Users\pankaj.jaju\Desktop\test2.xml"
End With

Set objNode= objXML.SelectSingleNode("/gfi_message/body/data/node/field[@name='Spot']")
Msgbox objNode.XML

输出

enter image description here

关于xml - 使用 VBA 选择单个节点 XML 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48682377/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com