我有一个简单的 XML,如下所示,我需要显示每个节点的名称及其值。任何元素都不会有任何属性。
<?xml version="1.0" encoding="UTF-8"?>
<ResponseEnvelope xmlns="http://www.nwabcdfdfd.com/messagin" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ResponseHeader>
<RequestId>directv_99e0857d-abf3-461c-913e-3ab59c6b5ef6</RequestId>
<ResponseId>1162969</ResponseId>
<MessageVersion>1.10</MessageVersion>
<RequestTimestamp>2013-02-12T17:26:28.172Z</RequestTimestamp>
<ResponseTimestamp>2013-02-12T17:26:50.409Z</ResponseTimestamp>
<SenderId>CarePortal2</SenderId>
<ProgramName />
<TestProdFlag>P</TestProdFlag>
<ResultCode>9</ResultCode>
<Locale>en_US</Locale>
<Errors>
<Error>
<ErrorCode>9</ErrorCode>
<ErrorNumber>90001</ErrorNumber>
<ErrorMessage>System error occurred</ErrorMessage>
<ErrorFieldId />
</Error>
</Errors>
</ResponseHeader>
<ResponseBody xsi:type="CPSingleSignOnResponse">
<PortalUserID>45497</PortalUserID>
<PartyID>1858186</PartyID>
<WarrantyItemName>DTV ABC WOLE HE P</WarrantyItemName>
<WarrantyInventoryItemId>138677</WarrantyInventoryItemId>
<ClientWarrantySku>202</ClientWarrantySku>
<ClientWarrantyDescription>DV Plan</ClientWarrantyDescription>
<ContractNumber>4003564</ContractNumber>
<IsPortalUserCreated>N</IsPortalUserCreated>
<IsPartyCreated>N</IsPartyCreated>
<IsContractUpdated>N</IsContractUpdated>
<IsFootPrintUpdated>N</IsFootPrintUpdated>
<Customer>
<PartyId>185812386</PartyId>
<Salutation />
<FirstName>Tejas</FirstName>
<LastName>Tanna</LastName>
<AddressList>
<Address>
<PartySiteId>3617490</PartySiteId>
<Type>BILTO</Type>
<Address1>CASCADES</Address1>
<Address2>202</Address2>
<Address3>RIDGE HEAVEN</Address3>
<Address4 />
<City>STERLING</City>
<State>VA</State>
<PostalCode>20165</PostalCode>
<County>LOUDOUN</County>
<Province />
<Country>US</Country>
<Urbanization />
<AddressStyle>US</AddressStyle>
</Address>
<Address>
<PartySiteId>3613791</PartySiteId>
<Type>SHIP_T</Type>
<Address1>CASADS</Address1>
<Address2>22</Address2>
<Address3>RIE HEEN</Address3>
<Address4 />
<City>STELI</City>
<State>VA</State>
<PostalCode>2065</PostalCode>
<County>LOUUN</County>
<Province />
<Country>US</Country>
<Urbanization />
<AddressStyle>US</AddressStyle>
</Address>
</AddressList>
<PhoneList>
<Phone>
<ContactPointId>2371717</ContactPointId>
<Type>HOME PNE</Type>
<PhoneNumber>51-62-7464</PhoneNumber>
<Country>1</Country>
<PrimaryFlag>Y</PrimaryFlag>
</Phone>
</PhoneList>
<EmailList>
<Email>
<ContactPointId>237516</ContactPointId>
<EmailAddress>a.abc@abc.com</EmailAddress>
<PrimaryFlag>Y</PrimaryFlag>
</Email>
</EmailList>
</Customer>
</ResponseBody>
</ResponseEnvelope>
这里唯一的挑战是可能有一些元素可能在它自己的子元素中,例如地址所以代码需要有一个递归函数。
此外,不应显示没有像Address4 这样的文本的元素(它只有子元素)。此外,不应显示省等元素。
我尝试了以下代码但没有工作..
Sub Driver()
Range("4:" & Rows.Count).ClearContents
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
i = 4
xmlDoc.LoadXML (Range("A2"))
Set oParentNode = xmlDoc.DocumentElement.SelectNodes("ResponseBody")(0)
Call List_ChildNodes(oParentNode, i, "A", "B")
End Sub
Sub List_ChildNodes(oParentNode, i, NameColumn, ValueColumn)
For Each oChildNode In oParentNode.ChildNodes
If oChildNode.ChildNodes.Length > 1 Then
Call List_ChildNodes(oChildNode, i, NameColumn, ValueColumn)
Else
Cells(i, NameColumn) = oChildNode.tagname
Cells(i, ValueColumn) = oChildNode.Text
i = i + 1
End If
Next
End Sub