2015-09-10 32 views
0

我有一個XML文件需要解析。該文件包含有關潛水的信息。這是很簡單的結構:Excel VBA中的XML解析沒有找到我的節點

<Dive xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Suunto.Diving.Dal"> 
    <Algorithm>1</Algorithm> 
    <AltitudeMode>0</AltitudeMode> 
    <AscentTime i:nil="true"/> 
    <AvgDepth>9.36</AvgDepth> 
    <Boat i:nil="true"/> 
    <BottomTemperature>23</BottomTemperature> 
    <BottomTime i:nil="true"/> 
    <CnsEnd>1</CnsEnd> 
    <CnsStart>0</CnsStart> 
    <CylinderVolume>12</CylinderVolume> 
    <CylinderWorkPressure>232000</CylinderWorkPressure> 
    <Deleted i:nil="true"/> 
    <DeltaPressure i:nil="true"/> 
    <DesaturationTime i:nil="true"/> 
    <DiveMixtures> 
     <DiveMixture> 
     <DiveGasChanges/> 
     <EndPressure>47300</EndPressure> 
     <Helium>0</Helium> 
     <Name i:nil="true"/> 
     <Oxygen>21</Oxygen> 
     <PO2>0</PO2> 
     <Size>0</Size> 
     <StartPressure>208100</StartPressure> 
     <TransmitterId i:nil="true"/> 
     <Type>4</Type> 
    </DiveMixture> 
</DiveMixtures> 
<DiveNumberInSerie>2</DiveNumberInSerie> 
<DiveSamples> 
    <Dive.Sample> 
     <AveragedTemperature>26</AveragedTemperature> 
     <Ceiling i:nil="true"/> 
     <Depth>1.23</Depth> 
     <GasTime i:nil="true"/> 
     <Heading i:nil="true"/> 
     <Pressure>208100</Pressure> 
     <SacRate>27.0936737</SacRate> 
     <Temperature>26</Temperature> 
     <Time>0</Time> 
    </Dive.Sample> 
    <Dive.Sample> 
     <AveragedTemperature>26</AveragedTemperature> 
     <Ceiling i:nil="true"/> 
     <Depth>3.29</Depth> 
     <GasTime i:nil="true"/> 
     <Heading i:nil="true"/> 
     <Pressure>206800</Pressure> 
     <SacRate>28.29174</SacRate> 
     <Temperature>26</Temperature> 
     <Time>20</Time> 
    </Dive.Sample> 
</DiveSamples> 
<DiveTags/> 
<DiveTime i:nil="true"/> 
<DivingDaysInRow i:nil="true"/> 
<Duration>3595</Duration> 
<EndPressure>47300</EndPressure> 
<EndTemperature>25</EndTemperature> 
</Dive> 

現在我要像提取時間信息,AvgDepth等。此外,我需要每個「Dive.Sample」容器如在Excel工作表中的一行。假設我想在Sheet1的A1和B1以及Sheet2中的所有「Dive.Sample」中放置duration和AvgDepth,而每行代表一個「Dive.Sample」容器。我的VBA代碼現在看起來像這樣(我只是試圖讓AvgDepth):

Dim xmlDoc As New MSXML2.DOMDocument 
Dim xmlKnoten As IXMLDOMNode 

Dim xpathKnoten As String 
Dim xpathAttrib As String 

xmlDoc.async = False 
xmlDoc.validateOnParse = True     ' Auf Fehler prüfen 

If xmlDoc.Load(XmlDateiMitPfad) = False Then 
    MsgBox "XML-Datei: '" & XmlDateiMitPfad & "' wurde nicht gefunden" 
    Exit Sub 
ElseIf xmlDoc.parseError = True Then 
    MsgBox "XML-Datei: '" & XmlDateiMitPfad & "' hat fehlerhaften Aufbau (ist nicht 'wohlgeformt')" 
    Exit Sub 
End If 

xmlDoc.setProperty "SelectionLanguage", "XPath" 

xpathKnoten = "/Dive/AvgDepth"       
Set xmlKnoten = xmlDoc.SelectSingleNode(xpathKnoten) 

If xmlKnoten Is Nothing Then 
    MsgBox "Knoten nicht gefunden. Vermutlich falsche XML-Struktur" 
    Exit Sub 
End If 

With Tabelle1 
    .Range("A3") = xmlKnoten.SelectSingleNode("AvgDepth").Text 
End With 

的問題是,解析器沒有找到節點/dive/AvgDepthxmlKnoten總是沒有。我究竟做錯了什麼?

如何在xml結構中獲得更深的「Dive.Sample」?

+0

可能重複(http://stackoverflow.com/questions/26034373/reading-multiple-xml-files-via -vb6) – GSerg

回答

1

問題是多個名稱空間和未明確添加前綴的默認名稱空間。所以XPath不知道哪個名稱空間應該用於「/ Dive」。

無論您使用

... 
xpathKnoten = "*[local-name(.) = 'Dive']" 
Set xmlKnoten = xmlDoc.SelectSingleNode(xpathKnoten) 

If xmlKnoten Is Nothing Then 
    MsgBox "Knoten nicht gefunden. Vermutlich falsche XML-Struktur" 
    Exit Sub 
End If 

With Tabelle1 
    .Range("A3") = xmlKnoten.SelectSingleNode("*[local-name(.) = 'AvgDepth']").Text 
End With 
... 

,或者您與XPath的前綴映射的默認命名空間:

... 
xmlDoc.setProperty "SelectionLanguage", "XPath" 

xmlDoc.setProperty "SelectionNamespaces", "xmlns:sdd=""http://schemas.datacontract.org/2004/07/Suunto.Diving.Dal""" 

xpathKnoten = "/sdd:Dive" 
Set xmlKnoten = xmlDoc.SelectSingleNode(xpathKnoten) 

If xmlKnoten Is Nothing Then 
    MsgBox "Knoten nicht gefunden. Vermutlich falsche XML-Struktur" 
    Exit Sub 
End If 

With Tabelle1 
    .Range("A3") = xmlKnoten.SelectSingleNode("sdd:AvgDepth").Text 
End With 
... 

對於你的第二個問題:「我怎樣才能在XML結構更深得到我的「Dive.Sample」?「

我會做這樣的事情:通過VB6讀取多個XML文件]的

... 
Dim xmlNodeList As IXMLDOMNodeList 

Set xmlNodeList = xmlDoc.SelectNodes("/sdd:Dive/sdd:DiveSamples/sdd:Dive.Sample") 

For Each xmlKnoten In xmlNodeList 
MsgBox xmlKnoten.XML 
Next 
...