我正在尝试将以下 Xml 抓取到 Excel 工作表中。但是,我只想遍历特定的子节点以显示 Name、PriceEffectiveStart、PriceEffectiveEnd、Price 和每个指数摘要的货币。
XML 代码
<indexPrices>
<indexPriceSummary>
<id>1</id>
<uri>www.example.com</uri>
<index>
<id>3</id>
<name>Same Day Index</name>
<uri>www.example.com.xml</uri>
</index>
<priceEffectiveStart>2015-06-26</priceEffectiveStart>
<priceEffectiveEnd>2015-06-26</priceEffectiveEnd>
<price>
<amount>2.4806</amount>
<currency>CAD</currency>
</price>
<duration>1</duration>
<quantityTraded>
<amount>474</amount>
<unit>GJ</unit>
<contractUnit>Day</contractUnit>
</quantityTraded>
<numberOfTrades>7</numberOfTrades>
<settlementState>Settled</settlementState>
<lastUpdateDate>2015-06-27T02:15:01-06:00</lastUpdateDate>
</indexPriceSummary>
<indexPriceSummary>
<id>1</id>
<uri>www.example.com.xml</uri>
<index>
<id>1</id>
<name>Same Day Index </name>
<uri>www.example.com.xml</uri>
</index>
<priceEffectiveStart>2015-06-27</priceEffectiveStart>
<priceEffectiveEnd>2015-06-27</priceEffectiveEnd>
<price>
<amount>2.516</amount>
<currency>CAD</currency>
</price>
<duration>1</duration>
<quantityTraded>
<amount>251</amount>
<unit>GJ</unit>
<contractUnit>Day</contractUnit>
</quantityTraded>
<numberOfTrades>50</numberOfTrades>
<settlementState>Settled</settlementState>
<lastUpdateDate>2015-06-28T02:15:00-06:00</lastUpdateDate>
</indexPriceSummary>
</IndexPrices>
VBA 代码
Dim xDoc As DOMDocument
Set xDoc = New DOMDocument
xDoc.LoadXML objHTTP.responseText
Dim i As Integer
Dim list As IXMLDOMNodeList
Set list = xDoc.SelectNodes("//indexPrices/indexPriceSummary")
Dim node As IXMLDOMNode
Dim childNode As IXMLDOMNode
Dim price As IXMLDOMNode
For Each node In list
i = i + 1
If (node.HasChildNodes) Then
For Each childNode In node.ChildNodes
i = i + 1
Debug.Print childNode.BaseName & " " & childNode.Text
Worksheets("Sheet1").Cells(i, 1) = childNode.BaseName
Worksheets("Sheet1").Cells(i, 2) = childNode.Text
Next childNode
End If
Next node
当前 VBA 显示输出中的所有节点。我希望它只显示 Name、PriceEffectiveStart、PriceEffectiveEnd、Price 和 Currency 用于每个索引摘要。
最佳答案
您可以在每个 indexPriceSummary 节点上使用 xpath 直接获取子元素:
Sub Tester()
Dim xDoc As DOMDocument
Set xDoc = New DOMDocument
''more code here
xDoc.LoadXML objHTTP.responseText
Dim i As Integer
Dim list As IXMLDOMNodeList
Set list = xDoc.SelectNodes("//indexPrices/indexPriceSummary")
Dim node As IXMLDOMNode, nd As IXMLDOMNode
Dim childNode As IXMLDOMNode
Dim price As IXMLDOMNode
i = 4
For Each node In list
i = i + 1
With Sheet1.Rows(i)
.Cells(1).Value = GetNodeValue(node, "index/name")
.Cells(2).Value = GetNodeValue(node, "priceEffectiveStart")
.Cells(3).Value = GetNodeValue(node, "priceEffectiveEnd")
.Cells(4).Value = GetNodeValue(node, "price/amount")
.Cells(5).Value = GetNodeValue(node, "price/currency")
End With
Next node
End Sub
Function GetNodeValue(node As IXMLDOMNode, xp As String)
Dim n As IXMLDOMNode, nv
Set n = node.SelectSingleNode(xp)
If Not n Is Nothing Then nv = n.nodeTypedValue
GetNodeValue = nv
End Function
关于excel - VBA - 从 XML 代码循环特定的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31411708/
我脑子里浮现出一些关于一种新编程语言的想法,所以我想我会尝试实现它。一位friend建议我尝试使用Treetop(Rubygem)来创建一个解析器。Treetop的文档很少,我以前从未做过这种事情。我的解析器表现得好像有一个无限循环,但没有堆栈跟踪;事实证明很难追踪到。有人可以指出入门级解析/AST指南的方向吗?我真的需要一些列出规则、常见用法等的东西来使用像Treetop这样的工具。我的语法分析器在GitHub上,以防有人希望帮助我改进它。class{initialize=lambda(name){receiver.name=name}greet=lambda{IO.puts("He
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru
我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur