2016-11-12 20 views
0

以下の形式のデータがあり、Excelシートに類似のデータがあります。VBAでXMLファイルを解析する方法

<LegalEntityDataVO> 
    <LegalEntityDataVORow> 
     <Name>Siemens Corporation</Name> 
     <LegalEntityIdentifier>010</LegalEntityIdentifier> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>Siemens Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Income Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Federal Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>US Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Service Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Oil Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
    </LegalEntityDataVORow> 
<LegalEntityDataVO> 

私の要件は、ExcelデータとXMLデータを比較することです。具体的には、私の仕事は、以下の通りである:

  1. すべてLegalEntityDataVORow

    はすべてEstablishmentDataVORowが多くRegistrationDataEtbVORow含まれている多くのEstablishmentDataVORow
  2. 含まれています。ここ
    If **LegalEntityIdentifier** value in Excel = **LegalEntityIdentifier** value in xml then 
    
    ( 
    If(**MainEstablishmentFlag** value in Excel = **MainEstablishmentFlag** value in Xml then 
    
        (
         Compare **Name** in Excel with **Name** in XML 
        ) 
    ) 
    
    
    **LegalEntityIdentifier** childnode of LegalEntityDataVORow 
    
    **MainEstablishmentFlag** childnode of EstablishmentDataVORow 
    
    **Name** childnode of RegistrationDataEtbVORow 
    

    は、私が直面している問題です。

私のXMLファイルには、私は100 <LegalEntityDataVORow>があります。 VBAで上記のタスクを実行するにはどうすればよいですか?

+0

http://stackoverflow.com/questions/11305/how-to-parse-xml-using-vba – cullan

+0

XMLは、ツリー構造の文書であり、Excelのデータはフラット、2です次元形式。だから、2つのコンテンツは類似している可能性はありません。表形式のExcelデータを表示してください。 – Parfait

答えて

1

次のコードを出力し、以下のファイルのコード以下の出力を生成します。

コード:

Sub parse_data() 

    Dim strXmlFileName As String: strXmlFileName = "Drive:\Path\Filename.xml" 
    Dim docXmlDocument As New MSXML2.DOMDocument60 

    Dim wsDataToCompare As Worksheet: Set wsDataToCompare = ActiveWorkbook.Sheets("DataToCompare") 
    Dim strLegalEntityIdentifierToCompare As String: strLegalEntityIdentifierToCompare = wsDataToCompare.Cells(1, 1).Value 
    Dim strMainEstablishmentFlagToCompare As String: strMainEstablishmentFlagToCompare = wsDataToCompare.Cells(2, 1).Value 

    Dim ndeEntityData As IXMLDOMNode 
    Dim ndeEntityDataChild As IXMLDOMNode 
    Dim ndeEstablishmentData As IXMLDOMNode 

    Dim strNameToExtract As String 

    docXmlDocument.Load strXmlFileName 

    For Each ndeEntityData In docXmlDocument.DocumentElement.ChildNodes 

     If ndeEntityData.SelectSingleNode("LegalEntityIdentifier").Text = strLegalEntityIdentifierToCompare Then 

      For Each ndeEntityDataChild In ndeEntityData.ChildNodes 

       If ndeEntityDataChild.BaseName = "EstablishmentData" Then 

        If ndeEntityDataChild.SelectSingleNode("EstablishmentDataVORow/MainEstablishmentFlag").Text = strMainEstablishmentFlagToCompare Then 

         strNameToExtract = ndeEntityDataChild.SelectSingleNode("EstablishmentDataVORow/Name").Text 
         Debug.Print strNameToExtract 

        End If 

       End If 

      Next ndeEntityDataChild 

     End If 

    Next ndeEntityData 

End Sub 

出力:私はあなたを拡大しなければならなかった

Siemens Corporation 
US Corporation 

注意XML-Fileを再度有効にして有効にします。私が使用するファイルは次のとおりです。

<?xml version="1.0" encoding="UTF-8"?> 
<LegalEntityDataVO> 
    <LegalEntityDataVORow> 
     <Name>Siemens Corporation</Name> 
     <LegalEntityIdentifier>010</LegalEntityIdentifier> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>Siemens Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Income Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Federal Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>US Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Service Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Oil Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
    </LegalEntityDataVORow> 
</LegalEntityDataVO> 
関連する問題