从 AMAZON XML API in Excel VBA 访问元素
Acessing elements from AMAZON XML API in Excel VBA
我正在尝试解析来自亚马逊产品广告 API XML 响应的数据。我在 excel vba 中使用 DOMDocument 加载 xml 文档并解析响应。这是 XML 响应的样子:
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<OperationRequest>
<RequestId>fd9f1314-29932-45a0-8fc4-c999276425f4</RequestId>
<RequestProcessingTime>0.0265880000000000</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemLookupRequest>
<IdType>ASIN</IdType>
<ItemId>B00CSDILZI</ItemId>
<ResponseGroup>Offers</ResponseGroup>
<VariationPage>All</VariationPage>
</ItemLookupRequest>
</Request>
<Item>
<ASIN>B00CSDILZI</ASIN>
<OfferSummary>
<LowestNewPrice>
<Amount>26761</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>7.61</FormattedPrice>
</LowestNewPrice>
<TotalNew>22</TotalNew>
<TotalUsed>0</TotalUsed>
<TotalCollectible>0</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<TotalOffers>1</TotalOffers>
<TotalOfferPages>1</TotalOfferPages>
MoreOffersUrl>...</MoreOffersUrl>
<Offer>
<OfferAttributes>...</OfferAttributes>
<OfferListing>
<OfferListingId>...</OfferListingId>
<Price>
<Amount>26761</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>7.61</FormattedPrice>
</Price>
<AmountSaved>
<Amount>23841</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>8.41</FormattedPrice>
</AmountSaved>
<PercentageSaved>47</PercentageSaved>
<Availability>Usually ships in 1-2 business days</Availability>
<AvailabilityAttributes>
<AvailabilityType>now</AvailabilityType>
<MinimumHours>24</MinimumHours>
<MaximumHours>48</MaximumHours>
</AvailabilityAttributes>
<IsEligibleForSuperSaverShipping>0</IsEligibleForSuperSaverShipping>
</OfferListing>
</Offer>
</Offers>
</Item>
</Items>
</ItemLookupResponse>
我正在尝试遍历整个 xml 并将商品详细信息(如 ASIN、格式化价格、最低价格等)提取到 excel sheet。我就是这样尝试的:
Sub subReadXMLStream()
Dim xmlDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xParent As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim Col, Row As Integer
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
' use XML string to create a DOM, on error show error message
If Not xmlDoc.Load("E:\web\offersumm.xml") Then
Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set xEmpDetails = xmlDoc.DocumentElement
Set xParent = xEmpDetails.FirstChild
Row = 1
Col = 1
Dim xmlNodeList As IXMLDOMNodeList
Set xmlNodeList = xmlDoc.SelectNodes("//Item")
For Each xParent In xmlNodeList
For Each xChild In xParent.ChildNodes
Worksheets("Sheet2").Cells(Row, Col).Value = xChild.Text
Debug.Print Row & " - "; Col & " - " & xChild.Text
Col = Col + 1
Next xChild
Row = Row + 1
Col = 1
Next xParent
结束子
然而,这是将标签中的整个文本打印成类似的
1 - 1 - B00CSDILZI
1 - 2 - 26761 USD 7.61 22 0 0 0
1 - 3 - 1 1 ... ... ... 26761 USD 7.61 23841 USD 8.41 47 Usually ships in 1-2 business days now 24 48 0
但我想要实现的是通过标签名称获取元素,然后在单元格中打印。任何人都可以告诉我我在这里做错了什么吗?
以下代码为 XML 中的每个 Item
元素获取 ASIN
、Offers/Offer[0]/OfferListing/Price/FormattedPrice
和 Offers/Offer[0]/OfferListing/Availability
。它假定 Offers
中可能有多个 Offer
元素,但每个 Offer
中只有一个 OfferListing
。如果有多个Offer
个元素,取第一个
SelectNodes
和SelectSingleNode
中使用的语法是XPath,参见http://en.wikipedia.org/wiki/XPath. For MSXML2.DOMDocument see https://msdn.microsoft.com/en-us/library/aa923288.aspx
Sub subReadXMLStream()
Dim xmlDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xParent As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim lCol, lRow As Long
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
If Not xmlDoc.Load("P:\offersumm.xml") Then
Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set xEmpDetails = xmlDoc.DocumentElement
Set xParent = xEmpDetails.FirstChild
lRow = 1
lCol = 1
Dim xmlNodeList As IXMLDOMNodeList
Dim sFormattedPrice As String, sAvailability As String
Set xmlNodeList = xmlDoc.SelectNodes("//Item")
For Each xParent In xmlNodeList
For Each xChild In xParent.ChildNodes
If xChild.nodeName = "ASIN" Then
Worksheets("Sheet2").Cells(lRow, lCol).Value = xChild.Text
lCol = lCol + 1
ElseIf xChild.nodeName = "Offers" Then
sFormattedPrice = xChild.SelectSingleNode("Offer[0]/OfferListing/Price/FormattedPrice").Text
Worksheets("Sheet2").Cells(lRow, lCol).Value = sFormattedPrice
lCol = lCol + 1
sAvailability = xChild.SelectSingleNode("Offer[0]/OfferListing/Availability").Text
Worksheets("Sheet2").Cells(lRow, lCol).Value = sAvailability
lCol = lCol + 1
End If
Next xChild
lRow = lRow + 1
lCol = 1
Next xParent
End Sub
我正在尝试解析来自亚马逊产品广告 API XML 响应的数据。我在 excel vba 中使用 DOMDocument 加载 xml 文档并解析响应。这是 XML 响应的样子:
<ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<OperationRequest>
<RequestId>fd9f1314-29932-45a0-8fc4-c999276425f4</RequestId>
<RequestProcessingTime>0.0265880000000000</RequestProcessingTime>
</OperationRequest>
<Items>
<Request>
<IsValid>True</IsValid>
<ItemLookupRequest>
<IdType>ASIN</IdType>
<ItemId>B00CSDILZI</ItemId>
<ResponseGroup>Offers</ResponseGroup>
<VariationPage>All</VariationPage>
</ItemLookupRequest>
</Request>
<Item>
<ASIN>B00CSDILZI</ASIN>
<OfferSummary>
<LowestNewPrice>
<Amount>26761</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>7.61</FormattedPrice>
</LowestNewPrice>
<TotalNew>22</TotalNew>
<TotalUsed>0</TotalUsed>
<TotalCollectible>0</TotalCollectible>
<TotalRefurbished>0</TotalRefurbished>
</OfferSummary>
<Offers>
<TotalOffers>1</TotalOffers>
<TotalOfferPages>1</TotalOfferPages>
MoreOffersUrl>...</MoreOffersUrl>
<Offer>
<OfferAttributes>...</OfferAttributes>
<OfferListing>
<OfferListingId>...</OfferListingId>
<Price>
<Amount>26761</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>7.61</FormattedPrice>
</Price>
<AmountSaved>
<Amount>23841</Amount>
<CurrencyCode>USD</CurrencyCode>
<FormattedPrice>8.41</FormattedPrice>
</AmountSaved>
<PercentageSaved>47</PercentageSaved>
<Availability>Usually ships in 1-2 business days</Availability>
<AvailabilityAttributes>
<AvailabilityType>now</AvailabilityType>
<MinimumHours>24</MinimumHours>
<MaximumHours>48</MaximumHours>
</AvailabilityAttributes>
<IsEligibleForSuperSaverShipping>0</IsEligibleForSuperSaverShipping>
</OfferListing>
</Offer>
</Offers>
</Item>
</Items>
</ItemLookupResponse>
我正在尝试遍历整个 xml 并将商品详细信息(如 ASIN、格式化价格、最低价格等)提取到 excel sheet。我就是这样尝试的:
Sub subReadXMLStream()
Dim xmlDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xParent As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim Col, Row As Integer
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
' use XML string to create a DOM, on error show error message
If Not xmlDoc.Load("E:\web\offersumm.xml") Then
Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set xEmpDetails = xmlDoc.DocumentElement
Set xParent = xEmpDetails.FirstChild
Row = 1
Col = 1
Dim xmlNodeList As IXMLDOMNodeList
Set xmlNodeList = xmlDoc.SelectNodes("//Item")
For Each xParent In xmlNodeList
For Each xChild In xParent.ChildNodes
Worksheets("Sheet2").Cells(Row, Col).Value = xChild.Text
Debug.Print Row & " - "; Col & " - " & xChild.Text
Col = Col + 1
Next xChild
Row = Row + 1
Col = 1
Next xParent
结束子
然而,这是将标签中的整个文本打印成类似的
1 - 1 - B00CSDILZI
1 - 2 - 26761 USD 7.61 22 0 0 0
1 - 3 - 1 1 ... ... ... 26761 USD 7.61 23841 USD 8.41 47 Usually ships in 1-2 business days now 24 48 0
但我想要实现的是通过标签名称获取元素,然后在单元格中打印。任何人都可以告诉我我在这里做错了什么吗?
以下代码为 XML 中的每个 Item
元素获取 ASIN
、Offers/Offer[0]/OfferListing/Price/FormattedPrice
和 Offers/Offer[0]/OfferListing/Availability
。它假定 Offers
中可能有多个 Offer
元素,但每个 Offer
中只有一个 OfferListing
。如果有多个Offer
个元素,取第一个
SelectNodes
和SelectSingleNode
中使用的语法是XPath,参见http://en.wikipedia.org/wiki/XPath. For MSXML2.DOMDocument see https://msdn.microsoft.com/en-us/library/aa923288.aspx
Sub subReadXMLStream()
Dim xmlDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xParent As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim lCol, lRow As Long
Set xmlDoc = New MSXML2.DOMDocument
xmlDoc.async = False
xmlDoc.validateOnParse = False
If Not xmlDoc.Load("P:\offersumm.xml") Then
Err.Raise xmlDoc.parseError.ErrorCode, , xmlDoc.parseError.reason
End If
Set xEmpDetails = xmlDoc.DocumentElement
Set xParent = xEmpDetails.FirstChild
lRow = 1
lCol = 1
Dim xmlNodeList As IXMLDOMNodeList
Dim sFormattedPrice As String, sAvailability As String
Set xmlNodeList = xmlDoc.SelectNodes("//Item")
For Each xParent In xmlNodeList
For Each xChild In xParent.ChildNodes
If xChild.nodeName = "ASIN" Then
Worksheets("Sheet2").Cells(lRow, lCol).Value = xChild.Text
lCol = lCol + 1
ElseIf xChild.nodeName = "Offers" Then
sFormattedPrice = xChild.SelectSingleNode("Offer[0]/OfferListing/Price/FormattedPrice").Text
Worksheets("Sheet2").Cells(lRow, lCol).Value = sFormattedPrice
lCol = lCol + 1
sAvailability = xChild.SelectSingleNode("Offer[0]/OfferListing/Availability").Text
Worksheets("Sheet2").Cells(lRow, lCol).Value = sAvailability
lCol = lCol + 1
End If
Next xChild
lRow = lRow + 1
lCol = 1
Next xParent
End Sub