2017-09-21 4 views
1
にキー内のXMLキーを取得する方法

XMLVBA - のDebug.Print

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<EmpDetails> 
    <Employee> 
     <Name>ABC</Name> 
     <Dept> 
     <Software1> VBA </Software1> 
     <Software2> Windows </Software2> 
     </Dept> 
     <Location>New Delhi</Location> 
    </Employee> 
    <Employee> 
     <Name>XYZ</Name> 
     <Dept> 
     <Software1> VBA </Software1> 
     <Software2> Windows </Software2> 
     </Dept> 
     <Location>Chennai</Location> 
    </Employee> 
    <Employee> 
     <Name>IJK</Name> 
     <Dept> 
      <Software1> VBA </Software1> 
      <Software2> Windows </Software2> 
     </Dept> 
     <Location>Bangalore</Location> 
    </Employee> 
</EmpDetails> 

VBA

Sub Test() 

Dim userBeanList As MSXML2.IXMLDOMNodeList 
Dim userbean As MSXML2.IXMLDOMNode 
Dim beanChild As MSXML2.IXMLDOMNode 

Set xDoc = New MSXML2.DOMDocument 
xDoc.Load ("C:\data\xml.xml") 

Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee") 

For Each userbean In userBeanList 
    For Each beanChild In userbean.ChildNodes 
     Debug.Print beanChild.nodeName & ":" & beanChild.Text 


    Next beanChild 
Next userbean 

End Sub 

私のコードは、現在、XMLから次のように出力されます

Name:ABC 
Dept:VBA Windows 
Location:New Delhi 
Name:XYZ 
Dept:VBA Windows 
Location:Chennai 
Name:IJK 
Dept:VBA Windows 
Location:Bangalore 

実際に印刷したいです。

Name:ABC 
Dept:VBA Windows 
Location:New Delhi 
Name:XYZ 
Software1:VBA 
Software2:Windows 
Location:Chennai 
Name:IJK 
Software1: VBA 
Software2: Windows 
Location:Bangalore 
+0

私は適切な字下げであなたのXMLコードを再フォーマットします...それが受け入れられると、あなたはそれを見なければなりません....あなたのXMLファイルはあなたが望むようにフォーマットされていません。 'XYZ'は' ABC'のサブタグではありません...どのタグをどの順序で印刷するかを選択する必要があります – jsotola

答えて

2

私の最初の例では、単純に別のレベル(grandChild)をループに追加しました。ここで

Sub Example() 

    Dim userBeanList As MSXML2.IXMLDOMNodeList 
    Dim userbean As MSXML2.IXMLDOMNode 
    Dim beanChild As MSXML2.IXMLDOMNode 
    Dim grandChild As MSXML2.IXMLDOMNode 

    Set xDoc = New MSXML2.DOMDocument 
    xDoc.Load ("C:\Users\best buy\Desktop\xml.xml") 

    Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee") 

    For Each userbean In userBeanList 
     For Each beanChild In userbean.ChildNodes 
      Debug.Print beanChild.nodeName & ":" & beanChild.Text 
      For Each grandChild In beanChild.ChildNodes 
       If Not Left(grandChild.nodeName, 1) = "#" Then Debug.Print grandChild.nodeName & ":" & grandChild.Text 
      Next 
     Next beanChild 
    Next userbean 

End Sub 

実際の構造を知らなくても、XMLのすべての枝と葉を印刷します再帰を使用しての例です。

Sub RecursiveExample() 
    Dim userBeanList As MSXML2.IXMLDOMNodeList 
    Set xDoc = New MSXML2.DOMDocument 
    xDoc.Load ("C:\Users\best buy\Desktop\xml.xml") 

    Set userBeanList = xDoc.SelectNodes("//EmpDetails/Employee") 
    RecursivePrintNodes userBeanList 
End Sub 

Sub RecursivePrintNodes(NodeList As MSXML2.IXMLDOMNodeList) 
    Dim child As MSXML2.IXMLDOMNode 
    For Each child In NodeList 
     If Not Left(child.nodeName, 1) = "#" Then Debug.Print child.nodeName & ":" & child.Text 
     If child.ChildNodes.Length > 0 Then RecursivePrintNodes child.ChildNodes 
    Next 
End Sub 
+0

これはすばらしく見えます - これはどのxmlにも適用できることを願っています! – user8608110

+0

私は、再帰的パターンはどのXMLでも動作するはずだと楽観的です。基本的には、ディレクトリ内のすべてのフォルダとサブフォルダを一覧表示するのと同じパターンです。 –