Link to home
Start Free TrialLog in
Avatar of Fordraiders
FordraidersFlag for United States of America

asked on

xml data to excel spreadsheet in vertical format

I have an xml file i need to import and place in vertical format not horizontal
code:
xmlDoc.LoadXML (reader.responseText)
   
 ''''   not working
    Set xEmpDetails = xmlDoc.DocumentElement
    Set xParent = xEmpDetails.FirstChild
    
    Row = 1
    Col = 1
    
    Dim xmlNodeList As IXMLDOMNodeList
    
 
        Set xmlNodeList = xmlDoc.SelectNodes("/*/*/*") ' i think this is working ? 8/26/2020

    For Each xParent In xmlNodeList
        For Each xChild In xParent.ChildNodes
            Worksheets("xmldata").Cells(Row, Col).value = xChild.Text
        Next xChild
        Row = Row + 1
        Col = Col
        Next xParent

Open in new window

current output:
User generated image
correct format:
User generated imagexml: data
User generated image
Thanks
fordraiders
Avatar of Boris Petrovic
Boris Petrovic
Flag of Croatia image

It looks to me that you are not increasing Col at all.
In addition to that, I changed the SelectNodes to target only "items" as parent nodes. This way you will be able to fetch their children (it would work with "/*/*" as well).
Set xmlNodeList = xmldoc.SelectNodes("/EsrpPriceRequest/Items")
For Each xParent In xmlNodeList
    Col = 1
    For Each xChild In xParent.ChildNodes
        Worksheets("xmldata").Cells(Row, Col).value = xChild.Text
        Col = Col + 1
    Next xChild
    Row = Row + 1
Next xParent

Open in new window

Where is the third value coming from?

I would use the XML schema like this:

Public Sub ParseXmlDoument(AXmlDocument As Object, AStartCell As Excel.Range) ' Use correct type instead of object.

  Dim Items As IXMLDOMNodeList
  Dim Item As Object ' Use correct type instead of object.

  Dim ItemName As String
  Dim Row As Long
  Dim TargetPrice As Long

  Set Items = AXmlDocument.SelectNodes("/EsrpPriceRequest/Items")
  Row = 0
  For Each Item In Items 
    ItemName = Item.SelectSingleNode("Item").Text
    TargetPrice = CLng(Item.SelectSingleNode("TargetPrice").Text)
    AStartCell.Offset(Row, 0).Value = ItemName
    AStartCell.Offset(Row, 1).Value = TargetPrice
    Row = Row + 1
  Next Item

End Sub

Open in new window

Avatar of Fordraiders

ASKER

Boris, Sorry another adjustment.. Please.
User generated image
the other code worked fine..
ASKER CERTIFIED SOLUTION
Avatar of Boris Petrovic
Boris Petrovic
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you both very very much !!!!