Visual Basic - pasar de txt a xml

Life is soft - evento anual de software empresarial
 
Vista:

pasar de txt a xml

Publicado por Chiki (3 intervenciones) el 17/11/2008 11:16:39
Hola,

Alguien podria ayudarme con algún trozo de código en el que a partir de un archivo en txt , con los campos separados por comas, pueda pasarlo a xml con sus tags incluidos.

Gracias de antemano
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:pasar de txt a xml

Publicado por Jose Hernandez (10 intervenciones) el 19/11/2008 08:18:00
Este código hace lo que pides. Utiliza ADO para leer un archivo delimitado y genera 2 archivos XML: uno usando directamente el motor ADO y el otro usando el objeto MSXML de Microsoft. Saludos.

Dim appPath As String
Dim rs As New ADODB.Recordset
Dim cnn As New ADODB.Connection
Dim cString
Dim fld As ADODB.field
Dim xml As MSXML2.DOMDocument
Dim record As MSXML2.IXMLDOMElement
Dim field As MSXML2.IXMLDOMElement
Dim cDataSecion As MSXML2.IXMLDOMCDATASection
Set cnn = New ADODB.Connection
appPath = App.Path
If Right(appPath, 1) <> "" Then appPath = appPath & ""
cString = "Provider=MSDASQL;Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=" & appPath
cnn.ConnectionString = cString
cnn.Open
rs.Open "datos.txt", cnn
If Dir(appPath & "datos1.xml") <> "" Then
Kill appPath & "datos1.xml"
End If
rs.Save appPath & "datos1.xml", adPersistXML
Set xml = New MSXML2.DOMDocument
xml.loadXML "<records/>"
Do While Not rs.EOF
Set record = xml.createElement("record")
For Each fld In rs.Fields
fldName = fld.Name
fldValue = fld.Value & ""
Set field = xml.createElement(fldName)
Set cDataSection = xml.createCDATASection(fldValue)
field.appendChild cDataSection
record.appendChild field
Next
xml.documentElement.appendChild record
rs.MoveNext
Loop
xml.Save appPath & "datos2.xml"
rs.Close
Set rs = Nothing
cnn.Close
Set cnn = Nothing
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar