2017-05-13 93 views
-1

:更新時間:導入複雜的XML到Excel 2016

我試圖導入從在線源XML文件到Excel 2016,其工作正常,但響應被限制爲200條記錄,我想創建一個表,並用多個請求填充數據。我工作的響應數據示例如下: -

<?xml version="1.0" encoding="UTF-8"?> 
<response uri="/crm/private/xml/Products/getRecords"> 
<result> 
<Products> 
<row no="1"> 
<FL val="PRODUCTID">1250447000004184129</FL> 
<FL val="Product Code"> 
<![CDATA[ 
1321321321321321 
]]> 
</FL> 
</row> 

我現在有以下VB代碼(與其他線程的幫助不大),但我在一個小區獲得所有的值一起,我似乎不能將產品代碼記錄分成單獨的行?

Sub XMLfromPPTExample2() 

Dim XDoc As MSXML2.DOMDocument60 
Dim xProducts As MSXML2.IXMLDOMNode 
Dim xrow As MSXML2.IXMLDOMNode 
Dim xChild As MSXML2.IXMLDOMNode 

Dim wb As Workbook 
Dim Col As Integer 
Dim Row As Integer 

Set XDoc = New MSXML2.DOMDocument60 
XDoc.async = False 
XDoc.validateOnParse = False 
XDoc.Load ("xml") 
LoadOption = xlXmlLoadImportToList 

Set xProducts = XDoc.DocumentElement 
Set xrow = 
xProducts.SelectSingleNode("/response/result/Products/row/FL[val='Product 
Code']") 

Col = 1 
Row = 1 

For Each xrow In xProducts.ChildNodes 
Row = 1 

For Each xChild In xrow.ChildNodes 
    Worksheets("Sheet2").Cells(Col, Row).Value = xChild.Text 
    Row = Row + 1 
Next xChild 

Next xrow 

End Sub 
+0

它看起來像你有一個傑森你xml。傑森的反序列化可能會更好。 – jdweng

+0

@jdweng - 那不是json,它是一個CDATA XML部分。 –

+0

是的,它是我試圖加載在一個列中的xml,但此時我將FL PRODUCTID和單價與導入時創建的默認模式列在同一列中似乎無法識別差異閱讀一些其他職位,這聽起來像一個循環的VBA代碼會做的伎倆。 –

回答

0

嗯,我已經得到了VBA代碼和運行在第一個200點的記錄帶上,現在我只需要在建立一個循環,讓他們的休息!

Sub Tester() 
Dim xDoc As MSXML2.DOMDocument60 
Set xDoc = New MSXML2.DOMDocument60 

xDoc.async = False 
xDoc.validateOnParse = False 

If xDoc.Load("xml") Then 
' The document loaded successfully. 
Else 
' The document failed to load. 
End If 

Dim i As Integer 
Dim list As IXMLDOMNodeList 
Set list = xDoc.SelectNodes("//response/result/Products/row") 

Dim node As IXMLDOMNode 

i = 1 
For Each node In list 
    i = i + 1 

    With Sheet1.Rows(i) 
     .Cells(1).Value = GetNodeValue(node, "FL[@val=""Product Code""]") 
     .Cells(2).Value = GetNodeValue(node, "FL[@val=""PRODUCTID""]") 
    End With 

Next node 

End Sub