I have a large XML file that I am trying to use the DOM. What I'm trying to do is pull information from only one node if its parent has a specific "id" attribute
For example, if I only use books for "id = adventure" Want to pull TITLE or AUTHOR - How do I use DOM?
& lt ;? XML version = "1.0"? & Gt; & Lt; List & gt; & Lt; Book ID = "Adventure" & gt; & Lt; Author & gt; Gagardella, Matthew & lt; / Author & gt; & Lt; Title & gt; XML Developer's Guide & lt; / Title & gt; & Lt; Price & gt; 44.95 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book ID = "Adventure" & gt; & Lt; Author & gt; Raals, Kim & lt; / Author & gt; & Lt; Title & gt; Midnight rain & lt; / Title & gt; & Lt; Price & gt; 5.95 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book ID = "Adventure" & gt; & Lt; Author & gt; Boyle, John & lt; / Author & gt; & Lt; Title & gt; Mist & lt; / Title & gt; & Lt; Price & gt; 15.95 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; Book ID = "Mystery" & gt; & Lt; Author & gt; Raals, Kim & lt; / Author & gt; & Lt; Title & gt; Some mystery book & lt; / Title & gt; & Lt; Price & gt; 9.95 & lt; / Pricing & gt; & Lt; / Book & gt; & Lt; / List & gt;
Use this code now:
Sub MySub () Dim mainWorkBook Workbook Dim XCMFILE version dim booktype string dim BOOKTITLE as version dim title string As dim as dim IXMLDOMNode set as integral as I set mainWorkBook = ActiveWorkbook XCMFILE = CreateObject ("Microsoft.XMLDOM") XCMFILE.Load (XCMFileName) 'load XCM file I = 0 in each XCMFILE.SelectNodes (for "/ list / book") booktype = n.Attributes.getNamedItem ("ID"). Search for ID properties within text 'node then bookID = "bold" then set BOOKTITLE = = XCMFILE.SelectNodes ("/ list / book / title / text ()") Title = BOOKTITLE (i) .NodeValue mainWorkBook.Sheets ("Sheet1") range ("B" in & amp; I + 3) .Value = BookTitle'prints in B column values i = i + 1 endif Next End Sub
I know how to increase the nodes that fit my specification / range properly No
Thanks for any suggestions and hints - I appreciate it
You do not need to increase anything simply selecting an XPath query that is only interested in nodes Type 'n' id 'attribute value "adventure" for each n ("/ list / book [@id =" "adventure" "]" in XCMFILE.SelectNodes:
Is a book node next to
No comments:
Post a Comment