PDA

View Full Version : خواندن فایل xml و پیدا کردن اطلاعات خاص



hmbarnamenevis
چهارشنبه 04 دی 1392, 20:36 عصر
با استفاده از توابع API و تابع sax می خواهیم یک فایل xml را خوانده و بعد با استفاده از یکسری اطلاعات مثل کد و نام در xml جستجو کرده و در صورت وجود اطلاعات دیگر آن را در فایل xml دیگری ذخیره کنیم.

m.4.r.m
جمعه 06 دی 1392, 00:01 صبح
ببین این کد ها به دردت می خوره ؟

Private Sub Form_Load()
Dim xmlDoc As New MSXML2.DOMDocument30
Dim nodeBook As IXMLDOMElement
Dim nodeId As IXMLDOMAttribute
Dim sIdValue As String
xmlDoc.async = False
xmlDoc.Load App.Path & "\books.xml"
If (xmlDoc.parseError.errorCode <> 0) Then
Dim myErr
Set myErr = xmlDoc.parseError
MsgBox ("You have error " & myErr.reason)
Else
Set nodeBook = xmlDoc.selectSingleNode("//book")
Set nodeId = nodeBook.getAttributeNode("id")
sIdValue = nodeId.xml
MsgBox sIdValue
End If

End Sub

hmbarnamenevis
جمعه 06 دی 1392, 08:20 صبح
ممنون دوست گرامی ولی من با توابع API باید بنویسم و آن هم با تابع sax

m.4.r.m
جمعه 06 دی 1392, 16:03 عصر
Option Explicit

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest
As Any, pSource As Any, ByVal ByteLen As Long)

Implements XMLSAX.ISAXContentHandler

Private Sub Command1_Click()

Call Parse

End Sub

Private Function Parse() As Boolean

Dim i() As Integer
Dim str As String

Dim sax As SAXXMLReader30

Set sax = New SAXXMLReader30

Call sax.PutContentHandler(Me)

str = App.Path & "\" & "test.xml"

' size the array to the number of characters
ReDim i(Len(str))

' copy the memory, note that the size is twice the number of chars as it is
'a unicode (double byte) string
CopyMemory i(0), ByVal StrPtr(str), Len(str) * 2

' pass the first array entry in
sax.ParseURL i(0), Len(str)

End Function

Private Sub ISAXContentHandler_Characters(pwchChars As Integer, ByVal
cchChars As Long)

'Do Nothing

End Sub

Private Sub ISAXContentHandler_EndDocument()

'Do Nothing


End Sub

Private Sub ISAXContentHandler_EndElement(pwchNamespaceUri As Integer,
ByVal cchNamespaceUri As Long, pwchLocalName As Integer, ByVal
cchLocalName As Long, pwchQName As Integer, ByVal cchQName As Long)

'Do Nothing

End Sub

Private Sub ISAXContentHandler_EndPrefixMapping(pwchPrefix As Integer,
ByVal cchPrefix As Long)

'Do Nothing


End Sub

Private Sub ISAXContentHandler_IgnorableWhitespace(pwchChars As Integer,
ByVal cchChars As Long)

'Do Nothing


End Sub

Private Sub ISAXContentHandler_ProcessingInstruction(pwchTarge t As
Integer, ByVal cchTarget As Long, pwchData As Integer, ByVal cchData
As Long)

'Do Nothing


End Sub

Private Sub ISAXContentHandler_PutDocumentLocator(ByVal pLocator As
XMLSAX.ISAXLocator)

'Do Nothing

End Sub

Private Sub ISAXContentHandler_SkippedEntity(pwchName As Integer, ByVal
cchName As Long)

'Do Nothing


End Sub

Private Sub ISAXContentHandler_StartDocument()

End Sub

Private Sub ISAXContentHandler_StartElement(pwchNamespaceUri As Integer,
ByVal cchNamespaceUri As Long, pwchLocalName As Integer, ByVal
cchLocalName As Long, pwchQName As Integer, ByVal cchQName As Long,
ByVal pAttributes As XMLSAX.ISAXAttributes)

Dim str As String

' size the string to the correct no of chars
str = String$(cchLocalName, 0)

' copy over the values. note the size is twice no of chars because the
' string is unicode (double byte)
CopyMemory ByVal StrPtr(str), pwchLocalName, cchLocalName * 2

Debug.Print str

End Sub

Private Sub ISAXContentHandler_StartPrefixMapping(pwchPrefix As Integer,
ByVal cchPrefix As Long, pwchUri As Integer, ByVal cchUri As Long)

'Do Nothing


End Sub

hmbarnamenevis
سه شنبه 10 دی 1392, 10:32 صبح
از این کد ها جوابی نگرفتم. آیا رفرنسی باید به پروژه اضافه کنم؟
لطفا کارهایی که باید انجام بدم رو برام توضیح دهید...