473,383 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problems importing xml to access2k

First go with trying to import xml to a database and whilst i have
managed to do what i want i find that the xml files we have here at
work are gonna cause me problems. I have a function that imports any
aml file, at the moment its importing to a hardcoded (name only) table
but i can manage to work out how to take the xml file name and use that
for the table name. My problem is some of the xml files do not follow
the structural conventions that are in place at this office they should
be as below...

.....
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="ENG_ID" fieldtype="r8" />
<FIELD attrname="JOB_DATE" fieldtype="dateTime" />
....
Followed by all the rest of the field names and types
....
</FIELDS>
<PARAMS LCID="1033" />
</METADATA>

Then tha actual Data
<ROWDATA>
<ROW ENG_ID="45" JOB_DATE="20060329" ACTIVITY_ID="44" QTY="1"
DURATION="75" SENT="Y" />
....
....
</ROWDATA>
</DATAPACKET>

providing that the fields are listed in the top section my function
will create a new table and populate it with the firlds and data from
the xml.

However some xml files have missing field data in the top or bottom
section ie a fiels is listed in the <fields> section but there is
nothing in the <rowdata> and vice versa. Guys this is so my first go
at this and ive struggled to get this far can anyone point me in the
right direction of how to get round this. Plus if theres anything in
the function that could be improved tell me. I could use the lessons. (
hopes there are no KIng Black Dragons lurking to flame a dumb nub)
antway heres the function and ty in anticipation

Private Function ProcessSingleProblemXML(ByVal sFile As String) As
Boolean
On Error Resume Next
Dim objDomDoc As New DOMDocument
Dim objRoot As IXMLDOMNode
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim child As IXMLDOMNode
Dim c As Integer
Dim r As Integer
Dim Maxc As Integer
Dim Maxr As Integer
Dim FieldNames() As String
Dim FieldType() As String
Dim Records() As String
Dim tdfNew As TableDef
Dim strField As String
Dim strFieldType As String
Dim fldNew As Field

Set dbs = CurrentDb

sFile = cXML_File_Folder & sFile
'If Dir(sFile) = "" Then Exit Function

objDomDoc.async = False
objDomDoc.Load sFile
Set objRoot = objDomDoc.documentElement
'Get No of Records (sure that there is a better way)
Maxr = 0
Maxc = 0
For Each child In objRoot.childNodes(1).childNodes
Maxr = Maxr + 1
Next
For Each child In objRoot.childNodes(0).childNodes(0).childNodes
Maxc = Maxc + 1
Next
Maxc = Maxc - 1
Maxr = Maxr - 1

ReDim FieldNames(Maxc)
ReDim FieldType(Maxc)
ReDim Records(Maxc, Maxr)
'Create fieldname array
c = 0
For Each child In objRoot.childNodes(0).childNodes(0).childNodes
FieldNames(c) = child.Attributes.getNamedItem("attrname").nodeValu e
FieldType(c) = child.Attributes.getNamedItem("fieldtype").nodeVal ue
c = c + 1
Next
'Initialise rowdata array

'Add rowdata to Records Array
c = 0
Do While c <= Maxc
r = 0
For Each child In objRoot.childNodes(1).childNodes
Records(c, r) =
child.Attributes.getNamedItem(FieldNames(c)).nodeV alue
r = r + 1
Next
c = c + 1
Loop

'Create table starts here
Set dbs = CurrentDb
On Error Resume Next
DoCmd.DeleteObject acTable, "MyNewTable1"
On Error GoTo 0

Set tdfNew = dbs.CreateTableDef("MyNewTable1")
c = 0
Do While c <= Maxc

strField = FieldNames(c)
strFieldType = FieldType(c)

Select Case strFieldType
Case "r8"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
Case "dateTime"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
Case "string"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
End Select

tdfNew.Fields.Append fldNew

c = c + 1
Loop
dbs.TableDefs.Append tdfNew

Set tdfNew = Nothing
Set fldNew = Nothing
'End Create table works up to here
Set rst = dbs.OpenRecordset("MyNewTable1")
r = 0
For r = 0 To Maxr
c = 0
rst.AddNew
For c = 0 To Maxc
If Records(c, r) <> "1" Then
rst.Fields(FieldNames(c)) = Records(c, r)
Else
rst.Fields(FieldNames(c)) = "N/A"
End If

Next

rst.Update
Next
rst.Close
'If unable to read xml store a detail of why
If Err.Number = 91 Then
Set rst = dbs.OpenRecordset("FIL_IMPRT_ERRRS", dbOpenDynaset)
rst.AddNew
rst!TYP = "PRBLM"
rst!BAD_FIL_NM = Replace(sFile, "Problem", "Reject")
rst!ERRR_DAT = Now()
rst.Update
rst.Close
Set rst = Nothing

ProcessSingleProblemXML = False
Exit Function
Else
ProcessSingleProblemXML = True
End If
'On Succcess exit function
If Err.Number = 0 Then ProcessSingleProblemXML = True
'Clean up
Set rst = Nothing
Set dbs = Nothing
Set objDomDoc = Nothing

End Function

Apr 26 '06 #1
5 2452

Be***********@gmail.com wrote:
First go with trying to import xml to a database and whilst i have
managed to do what i want i find that the xml files we have here at
work are gonna cause me problems. I have a function that imports any
aml file, at the moment its importing to a hardcoded (name only) table
but i can manage to work out how to take the xml file name and use that
for the table name. My problem is some of the xml files do not follow
the structural conventions that are in place at this office they should
be as below...

....
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD attrname="ENG_ID" fieldtype="r8" />
<FIELD attrname="JOB_DATE" fieldtype="dateTime" />
...
Followed by all the rest of the field names and types
...
</FIELDS>
<PARAMS LCID="1033" />
</METADATA>

Then tha actual Data
<ROWDATA>
<ROW ENG_ID="45" JOB_DATE="20060329" ACTIVITY_ID="44" QTY="1"
DURATION="75" SENT="Y" />
...
...
</ROWDATA>
</DATAPACKET>

providing that the fields are listed in the top section my function
will create a new table and populate it with the firlds and data from
the xml.

However some xml files have missing field data in the top or bottom
section ie a fiels is listed in the <fields> section but there is
nothing in the <rowdata> and vice versa. Guys this is so my first go
at this and ive struggled to get this far can anyone point me in the
right direction of how to get round this. Plus if theres anything in
the function that could be improved tell me. I could use the lessons. (
hopes there are no KIng Black Dragons lurking to flame a dumb nub)
antway heres the function and ty in anticipation

Private Function ProcessSingleProblemXML(ByVal sFile As String) As
Boolean
On Error Resume Next
Dim objDomDoc As New DOMDocument
Dim objRoot As IXMLDOMNode
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim child As IXMLDOMNode
Dim c As Integer
Dim r As Integer
Dim Maxc As Integer
Dim Maxr As Integer
Dim FieldNames() As String
Dim FieldType() As String
Dim Records() As String
Dim tdfNew As TableDef
Dim strField As String
Dim strFieldType As String
Dim fldNew As Field

Set dbs = CurrentDb

sFile = cXML_File_Folder & sFile
'If Dir(sFile) = "" Then Exit Function

objDomDoc.async = False
objDomDoc.Load sFile
Set objRoot = objDomDoc.documentElement
'Get No of Records (sure that there is a better way)
Maxr = 0
Maxc = 0
For Each child In objRoot.childNodes(1).childNodes
Maxr = Maxr + 1
Next
For Each child In objRoot.childNodes(0).childNodes(0).childNodes
Maxc = Maxc + 1
Next
Maxc = Maxc - 1
Maxr = Maxr - 1

ReDim FieldNames(Maxc)
ReDim FieldType(Maxc)
ReDim Records(Maxc, Maxr)
'Create fieldname array
c = 0
For Each child In objRoot.childNodes(0).childNodes(0).childNodes
FieldNames(c) = child.Attributes.getNamedItem("attrname").nodeValu e
FieldType(c) = child.Attributes.getNamedItem("fieldtype").nodeVal ue
c = c + 1
Next
'Initialise rowdata array

'Add rowdata to Records Array
c = 0
Do While c <= Maxc
r = 0
For Each child In objRoot.childNodes(1).childNodes
Records(c, r) =
child.Attributes.getNamedItem(FieldNames(c)).nodeV alue
r = r + 1
Next
c = c + 1
Loop

'Create table starts here
Set dbs = CurrentDb
On Error Resume Next
DoCmd.DeleteObject acTable, "MyNewTable1"
On Error GoTo 0

Set tdfNew = dbs.CreateTableDef("MyNewTable1")
c = 0
Do While c <= Maxc

strField = FieldNames(c)
strFieldType = FieldType(c)

Select Case strFieldType
Case "r8"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
Case "dateTime"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
Case "string"
Set fldNew = tdfNew.CreateField(strField, dbText, 50)
End Select

tdfNew.Fields.Append fldNew

c = c + 1
Loop
dbs.TableDefs.Append tdfNew

Set tdfNew = Nothing
Set fldNew = Nothing
'End Create table works up to here
Set rst = dbs.OpenRecordset("MyNewTable1")
r = 0
For r = 0 To Maxr
c = 0
rst.AddNew
For c = 0 To Maxc
If Records(c, r) <> "1" Then
rst.Fields(FieldNames(c)) = Records(c, r)
Else
rst.Fields(FieldNames(c)) = "N/A"
End If

Next

rst.Update
Next
rst.Close
'If unable to read xml store a detail of why
If Err.Number = 91 Then
Set rst = dbs.OpenRecordset("FIL_IMPRT_ERRRS", dbOpenDynaset)
rst.AddNew
rst!TYP = "PRBLM"
rst!BAD_FIL_NM = Replace(sFile, "Problem", "Reject")
rst!ERRR_DAT = Now()
rst.Update
rst.Close
Set rst = Nothing

ProcessSingleProblemXML = False
Exit Function
Else
ProcessSingleProblemXML = True
End If
'On Succcess exit function
If Err.Number = 0 Then ProcessSingleProblemXML = True
'Clean up
Set rst = Nothing
Set dbs = Nothing
Set objDomDoc = Nothing

End Function


First see the article by Danny Lesandrini:

http://www.databasejournal.com/featu...le.php/3310901

If that doesn't help I'll take a look at your code.

James A. Fortune
CD********@FortuneJames.com

The home team flop fest (a.k.a., NBA playoffs) saddens me. But there's
a certain fairness and sense in how it's being done. It rewards good
regular season play and increases the chances of a profitable seven
game series. Teams that overcome the home team flop factor more than
make up for having a worse regular season.

May 1 '06 #2
Thanks that actually was my starting point. I think there may be 10
lines of code left from that in my code. I solved my problem by
remembering that xml is still a text file so now i parse for the
fields and parse for the @@@=@@@@ pairs compare counts and
differentiate the treatment of the xml based on that. So yea it works
but if you feel like picking the code to pieces and enligtening me on
how it could be better i would be grateful.

Many thanks

May 2 '06 #3
Be***********@gmail.com wrote:
Thanks that actually was my starting point. I think there may be 10
lines of code left from that in my code. I solved my problem by
remembering that xml is still a text file so now i parse for the
fields and parse for the @@@=@@@@ pairs compare counts and
differentiate the treatment of the xml based on that. So yea it works
but if you feel like picking the code to pieces and enligtening me on
how it could be better i would be grateful.

Many thanks


O.K. I'll take a look at your code and Danny's code. I'm not
promising anything significant or anything soon but I am interested in
figuring out the best ways to parse XML. I'll try to be gentle if your
code is rough.

James A. Fortune
CD********@FortuneJames.com

May 3 '06 #4
rkc
Be***********@gmail.com wrote:
However some xml files have missing field data in the top or bottom
section ie a fiels is listed in the <fields> section but there is
nothing in the <rowdata> and vice versa.


See if this is any help to you. You can grab the ROW elements
attributes as a NamedNodeMap and iterate through them to get both
the baseName(field) and text(value) properties without having to
know which attributes have been included ahead of time.

<GetRowData>
Sub GetRowData(ByVal sFile As String)
Dim proc As New MSXML.DOMDocument
Dim rows As MSXML.IXMLDOMNodeList
Dim row As MSXML.IXMLDOMNode
Dim attribs As MSXML.IXMLDOMNamedNodeMap
Dim n As MSXML.IXMLDOMNode

proc.async = False
proc.Load (sFile)
Set rows = proc.getElementsByTagName("ROW")

For Each row In rows
Set attribs = row.Attributes
For Each n In attribs
Debug.Print n.baseName; "="; n.Text
Next
Debug.Print
Next

End Sub
</GetRowData>
May 4 '06 #5
Lol if u do do it when ur in a good mood or it may be hard to be
gentle. I also want to be able to parse xml. Go figure a successful
company sends data from 1 databse to another via xml then into a
spreadsheet by hand then back to a database god only knows how they are
still in business. Everytime someone creates a product or engineer or
any kind of list for an app or database the randomly pick new unique
id's so nothing talks to each other. But good news i cud be here for
years lol

:)

May 4 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ekyungchung | last post by:
I did install the python 2.4 and nltk. I am trying to follow the tutorial, but I kept getting error messages about importing corpus as follows ===================================================...
0
by: Lauren Quantrell | last post by:
I'm hoping someone can tell me if I'm doing the correct thing here or if there is a better way to do this with Access2K connecting to SQL Server 2K: Dim myConn As String myConn =...
10
by: Blake | last post by:
I have created an Access2K front end application that connects to a SQLServer2K backend. I use this vba code to create the connection from the Access app: Dim strConnect As String 'make sure...
0
by: Lauren Quantrell | last post by:
I'm trying to drop a file from Windows Explorer (or desktop, etc.) onto a field in Access2K and capture the full file path. I found an posting below that says this is possible but I cannot...
3
by: TWJohnson | last post by:
Can anyone tell me how to run the Access2k wizards from code in the Runtime enviroment? I need to do Address labels..perhaps there is another way to approach to this besides the wizards. Can anyone...
0
by: Lauren Quantrell | last post by:
I'm hoping someone can tell me if I'm doing the correct thing here or if there is a better way to do this with Access2K connecting to SQL Server 2K: Dim myConn As String myConn =...
26
by: jamesbeswick | last post by:
I've been using Access since version 97 and I've migrated to 2003. I've noticed a substantial number of strange ActiveX/OLE and code corruption problems when writing databases. The only solution...
13
by: Lauren Quantrell | last post by:
Is there a primer somewhere that can tell me code that works perfectly well in Access2K that won't work in 2003? I have been forced to migrate a large 2K app to 2003 and am finding a lot of...
3
by: scoots987 | last post by:
What do others do if you need to import excel files into SQL Server? My main problems are 1) zipcode formatting issues. If the column is a mix of zip and zip+4, I have problems retrieving all...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.