Страница: 1 |
|
Вопрос: XML в Access
|
Добавлено: 18.10.05 21:20
|
|
Автор вопроса: AndreyMp | ICQ: 237822510
|
Может только у меня такая трабла,но прошу помочь.
Есть примерно такой XML.
<?xml version="1.0" encoding="UTF-8" ?>
<Cadastre_Dat>
- <Parcels>
- <Parcel>
<CadastralNumber>01:08:01 01 001:0001</CadastralNumber>
<Category>003002000000</Category>
<GroundPayment>5120</GroundPayment>
<Utilization>ИЖС</Utilization>
- <LOCATION>
<RegionRFKind>01</RegionRFKind>
<inBounds>1</inBounds>
- <Elaboration_Location>
<referenceMark />
<distance />
<direction />
<placed>Ж5</placed>
</Elaboration_Location>
- <Address>
<District>Майкоп</District>
<InhabitedLocalityKind>х</InhabitedLocalityKind>
<InhabitedLocality>Веселый</InhabitedLocality>
<StreetKind>ул</StreetKind>
<Street>Молодежная</Street>
<House>дом 11 квартира 1</House>
</Address>
</LOCATION>
<Date_Registration>24.08.2004</Date_Registration>
<PrevCadastralNumber />
<Area>1000</Area>
<Unit>055</Unit>
- <Right>
<Name_Right>Государственная собственность</Name_Right>
<Kind_Right>001001009000</Kind_Right>
<N_Right>.</N_Right>
<Data_Right>02.09.2005</Data_Right>
<Data_Stopped />
- <LegalOwner>
- <Person>
- <Name>
<lastName>Самойлов</lastName>
<firstName>Андрей</firstName>
<middleName>Викторович</middleName>
</Name>
<INN_Person />
- <Certificate>
<Kind>05</Kind>
<Number />
<Organization />
<Date />
</Certificate>
<Share>1/2</Share>
</Person>
</LegalOwner>
</Right>
- <Right>
<Name_Right>Государственная собственность</Name_Right>
<Kind_Right>001001009000</Kind_Right>
<N_Right>.</N_Right>
<Data_Right>02.09.2005</Data_Right>
<Data_Stopped />
- <LegalOwner>
- <Person>
- <Name>
<lastName>Титуленко</lastName>
<firstName>Валерий</firstName>
<middleName>Владимирович</middleName>
</Name>
<INN_Person>00000000</INN_Person>
- <Certificate>
<Kind>05</Kind>
<Number />
<Organization />
<Date />
</Certificate>
<Share>1/2</Share>
</Person>
</LegalOwner>
</Right>
</Parcel>
- <Parcel>
Мне надо считать все Parcel в базу Access.
Я делаю следующее:
Dim fileIN As String
Dim fileXLS As String
Dim fileMDB As String
Dim fldXML() As String
Dim txtXML() As String
Dim tblName As String
Dim Parsel As Long
Dim fldCount As Long
Dim dbfConnStr As String
Dim sqlCreate As String
Dim NewBase As New ADOX.Catalog
Private Sub OpenConnect(sConnStr As String, sStr1 As String, sConnection As adodb.Connection, sRecordset As adodb.Recordset)
Set sConnection = New adodb.Connection
sConnection.CursorLocation = adUseClient
sConnection.Mode = adModeReadWrite
sConnection.Open sConnStr
Set sRecordset = New adodb.Recordset
sRecordset.Open sStr1, sConnection, adOpenDynamic, _
adLockOptimistic, adCmdUnknown
End Sub
Private Sub CloseConnect(sConnection As adodb.Connection, sRecordset As adodb.Recordset)
' sRecordset.Close
Set sRecordset.ActiveConnection = Nothing
sConnection.Close
Set sConnection = Nothing
End Sub
'Процедура создания базы данных
Private Sub CreateRezDBF(OutTable As String)
Dim Conn1 As adodb.Connection
Dim Rec1 As adodb.Recordset
sqlCreate = "create table " & OutTable & " ([" & fldXML(0) & "] String)"
NewBase.Create dbfConnStr
Call OpenConnect(dbfConnStr, sqlCreate, Conn1, Rec1)
Call CloseConnect(Conn1, Rec1)
End Sub
Private Sub AddfldXML(OutTable As String, newfldXML As String)
Dim Conn1 As adodb.Connection
Dim Rec1 As adodb.Recordset
sqlCreate = "Alter Table " & OutTable & " ADD [" & newfldXML & "] String)"
Call OpenConnect(dbfConnStr, sqlCreate, Conn1, Rec1)
Call CloseConnect(Conn1, Rec1)
End Sub
Private Sub FindChildNoteXML(inNode As IXMLDOMNode)
Dim oNode1 As IXMLDOMNodeList
Set oNode1 = inNode.childNodes
For k = 0 To oNode1.length - 1
List1.AddItem oNode1.Item(k).xml
If oNode1.Item(k).nodeType = NODE_TEXT Then
List2.AddItem oNode1.Item(k).Text
List3.AddItem oNode1.Item(k).parentNode.nodeName
ReDim Preserve fldXML(fldCount)
ReDim Preserve txtXML(fldCount)
fldXML(fldCount) = oNode1.Item(k).parentNode.parentNode.nodeName & _
"/" & oNode1.Item(k).parentNode.nodeName
txtXML(fldCount) = oNode1.Item(k).Text
fldCount = fldCount + 1
End If
Call FindChildNoteXML(oNode1.Item(k))
Next k
End Sub
Private Sub ReadXML(inFile As String)
Dim oDoc As New DOMDocument
Dim oNode As IXMLDOMNode
oDoc.async = False
oDoc.validateOnParse = False
oDoc.resolveExternals = False
oDoc.preserveWhiteSpace = True
If oDoc.Load(inFile) Then
Set oNodes = oDoc.selectNodes("//Parcel") 'Right
For Parsel = 0 To oNodes.length - 1
Set oNode = oNodes.nextNode
If Not (oNode Is Nothing) Then
fldCount = 0
ReDim fldXML(fldCount)
ReDim txtXML(fldCount)
Call FindChildNoteXML(oNode)
'Call WriteMDB
List1.AddItem "---------------"
List2.AddItem "---------------"
List3.AddItem "---------------"
End If
Next Parsel
Else
MsgBox "Файл не найден!", vbOKOnly, "Ошибка!"
End If
End Sub
Private Sub ReplaceOUTname(INstring As String)
Dim ExtFile As String
ExtFile = Mid(INstring, Len(INstring) - 2)
fileXLS = Replace(INstring, ExtFile, "xls")
fileMDB = Replace(INstring, ExtFile, "mdb")
End Sub
Private Sub OpenXMLfile()
Dim fsoFile As New FileSystemObject
CommonDialog1.DialogTitle = "Выбрать файл для обработки"
CommonDialog1.Filter = "XML файлы|*.xml|Все файлы|*.*|"
CommonDialog1.ShowOpen
If CommonDialog1.FileName <> "" Then
Text1.Text = CommonDialog1.FileName
fileIN = CommonDialog1.FileName
If fsoFile.FileExists(fileIN) Then
Call ReplaceOUTname(fileIN)
tblName = fsoFile.GetFileName(fileIN)
tblName = Mid(tblName, 1, Len(tblName) - 4)
Else
MsgBox "Файл не найден. Проверьте правильность пути и имени файла!", vbOKOnly, "Ошибка !"
End If
End If
End Sub
Private Sub WriteMDB()
Dim strSQL As String
Dim fldLoop As adodb.Field
Dim Conn1 As adodb.Connection
Dim Rec1 As adodb.Recordset
Dim fldNo As Boolean
If Parsel = 0 Then Call CreateRezDBF(tblName)
strSQL = "select * from " & tblName
For i = 0 To UBound(fldXML)
Call OpenConnect(dbfConnStr, strSQL, Conn1, Rec1)
Call CloseConnect(Conn1, Rec1)
fldNo = False
For Each fldLoop In Rec1.fields
If fldLoop.Name = fldXML(i) Then
fldNo = True
Exit For
End If
DoEvents
Next fldLoop
If Not fldNo Then Call AddfldXML(tblName, fldXML(i))
Next i
Call OpenConnect(dbfConnStr, strSQL, Conn1, Rec1)
Rec1.AddNew
For Each fldLoop In Rec1.fields
For i = 0 To UBound(fldXML)
If fldXML(i) = fldLoop.Name Then fldLoop.Value = txtXML(i)
Next i
Next fldLoop
Rec1.Update
Call CloseConnect(Conn1, Rec1)
End Sub
Private Sub Command1_Click()
Call OpenXMLfile
Command3.Enabled = True
Command4.Enabled = True
End Sub
Private Sub Command3_Click()
Dim fsoFile As New FileSystemObject
If fsoFile.FileExists(fileMDB) Then fsoFile.DeleteFile (fileMDB)
dbfConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & fileMDB
Call ReadXML(fileIN)
End Sub
Private Sub Command4_Click()
Call ReadXML(fileIN)
End Sub
Private Sub Command5_Click()
Unload Me
End Sub
Private Sub List2_Click()
List3.ListIndex = List2.ListIndex
End Sub
Private Sub List3_Click()
List2.ListIndex = List3.ListIndex
End Sub
Траблы:
1. В базу идут толко последний собственник из XML;
2. При сканировании полей приходится заново коннектиться к базе, а это задержка.. Но это не критично, поскольку XML на моем компе.
Ответить
|
Номер ответа: 4 Автор ответа: AndreyMp
ICQ: 237822510
Вопросов: 28 Ответов: 1182
|
Профиль | | #4
|
Добавлено: 21.10.05 15:57
|
Нет.Не проходит.
oDoc.selectNodes("//Parcel" ).selectNodes("//Person" ) -нет такого метода.Ошибка.
oDoc.selectNodes("//Parcel//Person" )
-дает по всему документу, а не в текущем ключе Parcel.
Ответить
|
Номер ответа: 6 Автор ответа: AndreyMp
ICQ: 237822510
Вопросов: 28 Ответов: 1182
|
Профиль | | #6
|
Добавлено: 21.10.05 16:19
|
oDoc.selectNodes("//Parcel" ) ну так это много нод возвращает. выбери одну и запроси у нее. Так я вроде это и спрашиваю,как? п.1
Ответить
|
Страница: 1 |
Поиск по форуму