使用VBA从XML提取数据

时间:2012-12-10 15:51:18

标签: xml vba

作为一个背景,我是一个相当新的编码人。自从我上次'编码'以来已经差不多10年了,所以请放轻松;)。

我目前正在尝试从以下XML文件中提取数据:

- <IPNumber IPNumber="5">
- <Band Band="1">
- <Channel Channel="1">
  <CMPWRE>0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
- <Channel Channel="2">
  <CMPWRE>-0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
  </Band>
- <Band Band="2">
- <Channel Channel="1">
  <CMPWRE>0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
  </Band>
</IPNumber5>
- <IPNumber IPNumber="6">
- <Band Band="1">
- <Channel Channel="1">
  <CMPWRE>0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
- <Channel Channel="2">
  <CMPWRE>-0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
  </Band>
- <Band Band="2">
- <Channel Channel="1">
  <CMPWRE>0.123456</CMPWRE> 
  <CMPWIM>0.234567</CMPWIM> 
  </Channel>
  </Band>
</IPNumber6>

目前我希望用户选择他们想要的IP编号,频道和频段..然后我想提取CMPWRE&amp;下面列表中每个的CMPWIM数据:

IPNumber 'X'
Band     'X'
Channel  'X'
CMPWRE   'XXX'
CMPWIM   'XXX' 

这是一段简短的代码片段,我试图用来提取数据,但我只是没有运气让它工作。

代码:

Set oXML = CreateObject("MSXML.DOMDocument")
oXML.async = False
oXML.Load ("H:\14-13-21.xml")


Dim LgChan As String
Dim LgChan1 As String
Dim LgChan2 As String
**Dim LgChan3 As String
Dim LgChan4 As String**

Dim Test As Integer 
Dim Test2 As Integer
Dim Test3 As Integer

Test = mobelenum0.Value ' User Box on GUI where a value is inputted E.G. the number 5
Test2 = FeedSubBand.Value ' User Box on GUI where a value is inputted E.G. the number 1 
Test3 = LogicChannel.Value ' User Box on GUI where a value is inputted E.G. the number 1


LgChan = "TableContents/IPModuleNumber[@IPModuleNumber='1']"
LgChan1 = "//TableContents/IPModuleNumber/FeederSubBand[@FeederSubBand=" & Test2 & "]"
LgChan2 = "//TableContents/IPModuleNumber/FeederSubBand/LogicalChannel[@LogicalChannel=" & Test3 & "]" 
**LgChan3 = "//TableContents/IPModuleNumber/FeederSubBand/LogicalChannel[@LogicalChannel=" & Test3 & "]/CMPWRE"
LgChan4 = "//TableContents/IPModuleNumber/FeederSubBand/LogicalChannel[@LogicalChannel=" & Test3 & "]/CMPWIM"**

 Worksheets("sheet1").Range("B101").Value = oXML.DocumentElement.SelectSingleNode(LgChan).NodeTypedValue
 Worksheets("sheet1").Range("B102").Value = oXML.DocumentElement.SelectSingleNode(LgChan1).nodeTypedValue
 Worksheets("sheet1").Range("B103").Value = oXML.DocumentElement.SelectSingleNode(LgChan2).nodeTypedValue
**Worksheets("sheet1").Range("B104").Value = oXML.DocumentElement.SelectSingleNode(LgChan3).nodeTypedValue
Worksheets("sheet1").Range("B105").Value = oXML.DocumentElement.SelectSingleNode(LgChan4).nodeTypedValue**

我正在努力获取代码,以便如果选择了差异频段,那么显然信道值将会改变...再次与IPNumber更改过滤相同。

任何帮助都会很棒!

Smerf

1 个答案:

答案 0 :(得分:0)

您需要添加捕获事件的代码“选择不同的频段时”。 该代码取决于用户如何选择波段,但如果从Userform中的Combobox中选择波段,您将捕获Combobox_Change事件,如下所示:

Private Sub FeedSubBand_Change()
    DisplayBands
End Sub

修改 如果用户按执行按钮选择了乐队,则应使用按钮的Click事件,如下所示:

Private Sub ExecuteButton_Click()
        DisplayBands
End Sub

正如您所说,重要的是要注意这里的性能问题。如果XML文件很大,那么阅读它会花费很长时间。这样做是个好主意,只有当用户通过按下按钮专门请求时才能读取文档。另一种选择是每次文本框中的一个更改时自动读取,但如果XML文档很大则不会很好。