1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
Imports System.IO
Imports System.Xml
Public Class OfxParserToXML
Private classFileName As String
Private xmlFileName As String
Private xmlData As XmlDocument
Public Sub New()
Dim ofd As New System.Windows.Forms.OpenFileDialog()
xmlFileName = CurDir() & "\xmlData.xml"
'Recherche et ouverture du fichier OFX
With ofd
.Filter = "Open Financial exchange file (*.ofx)|*.ofx|Quicken file (*.qfx)|*.qfx|Makisoft Personnal file (*.afx)|*.afx"
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
.Multiselect = False
.ShowDialog()
If (.FileName <> "") Then
classFileName = .FileName
Else
Throw New Exception("No file specified.")
End If
End With
Try
'Convertir OFX en XML
fileToXML()
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub fileToXML()
If (classFileName <> "") Then
Dim objReader As New System.IO.StreamReader(classFileName)
Dim allFile As String
Dim newAllFile As String
Dim spacesToDelete As Boolean
Dim curPosition As Integer
Dim nodeName As String
Dim nodeValue As String
Dim aNode As XmlNode
Dim anAttribute As XmlAttribute
allFile = objReader.ReadToEnd()
'Remove all cariage return feed line.
allFile = allFile.Replace(vbCrLf, "")
'Delete all spaces between > and <
spacesToDelete = False
newAllFile = ""
For Each caractere As Char In allFile
Select Case caractere
Case ">"
spacesToDelete = True
newAllFile = newAllFile & caractere
Case "<"
spacesToDelete = False
newAllFile = newAllFile & caractere
Case Else
If (spacesToDelete) Then
If (caractere <> " ") Then
spacesToDelete = False
newAllFile = newAllFile & caractere
End If
Else
newAllFile = newAllFile & caractere
End If
End Select
Next
curPosition = newAllFile.IndexOf("<")
'Create new XML document
xmlData = New XmlDocument()
'Add XML declaration
xmlData.AppendChild(xmlData.CreateXmlDeclaration("1.0", "UTF-8", Nothing))
'Get the first node name --> Should be OFX
nodeName = getNodeName(newAllFile, curPosition)
aNode = xmlData.AppendChild(xmlData.CreateElement(nodeName))
curPosition = getStartNextNode(newAllFile, curPosition)
'Retrieve all node and add it where it belongs in the tree
While getStartNextNode(newAllFile, curPosition) <> -1
nodeValue = getNodeValue(newAllFile, curPosition)
nodeName = getNodeName(newAllFile, curPosition)
'Debug.Print("Node name : " & nodeName & ", Node value : " & nodeValue & ", Cursor position : " & curPosition)
'In there is a value after ">", add an attribute
If (nodeValue <> Nothing) Then
anAttribute = xmlData.CreateAttribute(nodeName)
anAttribute.Value = nodeValue
aNode.Attributes.Append(anAttribute)
'If it's a closing tag, go back to parent's node
ElseIf (Left(nodeName, 1) = "/") Then
aNode = aNode.ParentNode
'Else, create a new node
Else
aNode = aNode.AppendChild(xmlData.CreateElement(nodeName))
End If
curPosition = getStartNextNode(newAllFile, curPosition)
End While
'Delete the xml file created previously
Kill(xmlFileName)
'Save the new xml file
xmlData.Save(xmlFileName)
MsgBox(xmlFileName)
Else
Throw New Exception("No file specified.")
End If
End Sub
Public Function getNodeName(ByVal ofxFile As String, ByVal startNodePos As Integer) As String
If (ofxFile = "") Then
Throw New Exception("Ofx file's empty.")
ElseIf (ofxFile.IndexOf(">", startNodePos) = -1) Then
Throw New Exception("Openned bracket not closed.")
Else
startNodePos = startNodePos + 1
Return ofxFile.Substring(startNodePos, ofxFile.IndexOf(">", startNodePos) - startNodePos)
End If
End Function
Public Function getStartNextNode(ByVal ofxFile As String, ByVal startNodePos As Integer) As Integer
If (ofxFile = "") Then
Throw New Exception("Ofx file's empty.")
ElseIf (ofxFile.IndexOf(">", startNodePos) = -1) Then
Throw New Exception("Openned bracket not closed.")
Else
Return ofxFile.IndexOf("<", startNodePos + 1)
End If
End Function
Public Function getNodeValue(ByVal ofxfile As String, ByVal startNodePos As Integer) As String
Dim startPos As Integer
Dim endPos As Integer
If (ofxfile = "") Then
Throw New Exception("Ofx file's empty.")
ElseIf (ofxfile.IndexOf(">", startNodePos) = -1) Then
Throw New Exception("Openned bracket not closed.")
Else
'MsgBox(ofxfile.Substring(startNodePos, 14))
If (ofxfile.Substring(ofxfile.IndexOf(">", startNodePos) + 1, 1) <> "<") Then
startPos = ofxfile.IndexOf(">", startNodePos + 1) + 1
endPos = ofxfile.IndexOf("<", startPos) - 1
Return ofxfile.Substring(startPos, endPos - startPos + 1)
Else
Return Nothing
End If
End If
End Function
End Class |
Partager