Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

Struggling to pull information from XML file into excel using VBA

I have been able to modify some code found on here to pull some data from an XML file but I am struggling to do this for one particular number, I am very new to XML files and cant figure out if what I am trying to pull is an Attribute/Node/Element. Below is the XML and the VBA I have so far.

<?xml version="1.0" encoding="UTF-8"?>
<reel id="r1" protversion="17">
    <order number="81234">
        <job name="apples">
            <lengthunit>mm</lengthunit>
            <formatlength>570.0</formatlength>
            <formatwidth>1300.0</formatwidth>
        </job>
    </order>
</reel>
Function ReadFromXML2()

Set oXMLFile = CreateObject("Microsoft.XMLDOM")
XMLFileName = "E:xml files
eport1.xml"
oXMLFile.Load (XMLFileName)

Set Nodes_Attribute = oXMLFile.SelectNodes("reel")
For i = 0 To (Nodes_Attribute.Length - 1)
    Attributes = Nodes_Attribute(i).getAttribute("id")
    ThisWorkbook.Worksheets(1).Range("A" & i + 2).Value = Attributes
Next

Set TitleNodes = oXMLFile.SelectNodes("/reel/order/job/formatlength/text()")
For i = 0 To (TitleNodes.Length - 1)
    Title = TitleNodes(i).NodeValue
    ThisWorkbook.Worksheets(1).Range("B" & i + 2).Value = Title
Next

End Function

The part I am struggling with is the order number, should I be using getAttribute or NodeValue? Whay I have so far works for getting the reel ID and formatlength.

question from:https://stackoverflow.com/questions/65848620/struggling-to-pull-information-from-xml-file-into-excel-using-vba

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Figured it out, I used getAttribute but I was trying "order number" rather than just "number"


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...