Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Importing XML records individually

Hi,
Is there a way to import data one record at a time from an XML file? I need
to read in the data for one record, perform a calculation, then read in the
next record and perform a calculation and repeat this for each record.
Regards and thanks,
Mark
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Importing XML records individually

It may not be terribly efficient to process the records one by one, but you
could try something like the following. It requires a reference to MSXML2.

Dim DOM As MSXML2.DOMDocument60
Dim Nodes As MSXML2.IXMLDOMNodeList
Dim Node As MSXML2.IXMLDOMNode
Dim XPath As String

Set DOM = New MSXML2.DOMDocument60
With DOM
.setProperty "SelectionLanguage", "XPath"
.validateOnParse = True
.Load "C:\Test.xml"
With .parseError
If .errorCode < 0 Then
MsgBox "Invalid XML: " & vbCrLf & _
"Error: " & .reason & " on line: " & .Line
Exit Sub
End If
End With
XPath = "/Records/Record"
Set Nodes = .selectNodes(XPath)
For Each Node In Nodes
' calculate with node
Debug.Print Node.childNodes(0).nodeTypedValue
Next Node
End With

This assumes a basic XML structure like

<Records
<Record
<Element123</Element
</Record
<Record
<Element234</Element
</Record
</Records


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting LLC
www.cpearson.com
(email on the web site)



"Zarch" wrote in message
...
Hi,
Is there a way to import data one record at a time from an XML file? I
need
to read in the data for one record, perform a calculation, then read in
the
next record and perform a calculation and repeat this for each record.
Regards and thanks,
Mark



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22
Default Importing XML records individually

Gosh Chip, that was a fast response! Many thanks.

Unfortunately the schema is a bit mote complex than that. It contains
historical sales data for several days for each of a number of products.

Something like:

Product
- Description
Sales
- SomeData
- MoreData
- History
- Day
- Date
- QuantitySold

Each product has a single deccription and a single instance of each of
SomeData and MoreData, but the 'Day' data contains several dates and
respective sales quantites for each product (it is on these that I have to
perform the calculations for each product to calculate the total sales for
two time periods before bringing in the next product record)

For me a simple solution would be to map each historical day's sales
quantity into an individual list and then sum the sales quantities row by row
for each product for the required dates, but I am being coerced in to doing
it using the current schema structure.

Regards and thanks,

Mark

"Chip Pearson" wrote:

It may not be terribly efficient to process the records one by one, but you
could try something like the following. It requires a reference to MSXML2.

Dim DOM As MSXML2.DOMDocument60
Dim Nodes As MSXML2.IXMLDOMNodeList
Dim Node As MSXML2.IXMLDOMNode
Dim XPath As String

Set DOM = New MSXML2.DOMDocument60
With DOM
.setProperty "SelectionLanguage", "XPath"
.validateOnParse = True
.Load "C:\Test.xml"
With .parseError
If .errorCode < 0 Then
MsgBox "Invalid XML: " & vbCrLf & _
"Error: " & .reason & " on line: " & .Line
Exit Sub
End If
End With
XPath = "/Records/Record"
Set Nodes = .selectNodes(XPath)
For Each Node In Nodes
' calculate with node
Debug.Print Node.childNodes(0).nodeTypedValue
Next Node
End With

This assumes a basic XML structure like

<Records
<Record
<Element123</Element
</Record
<Record
<Element234</Element
</Record
</Records


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting LLC
www.cpearson.com
(email on the web site)



"Zarch" wrote in message
...
Hi,
Is there a way to import data one record at a time from an XML file? I
need
to read in the data for one record, perform a calculation, then read in
the
next record and perform a calculation and repeat this for each record.
Regards and thanks,
Mark




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing a text file of individual records toby131 Excel Discussion (Misc queries) 3 September 28th 09 06:42 PM
Importing multiline records files into Excel cells michaelp Excel Discussion (Misc queries) 6 November 6th 06 07:07 PM
Importing Text File Data into Excel where records span several row Razorback76 Excel Discussion (Misc queries) 2 June 25th 06 06:36 AM
Importing file with fixed width, multi-line records danmcgov Excel Discussion (Misc queries) 7 March 20th 06 08:05 PM
Analyzing Text file records in VBA without importing into Excel spreadsheet ExcelMonkey[_190_] Excel Programming 1 February 4th 05 04:43 PM


All times are GMT +1. The time now is 10:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"