Results 1 to 10 of 10

Thread: [RESOLVED] convert csv to xml

  1. #1

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Resolved [RESOLVED] convert csv to xml

    hi ...

    i got one file:

    filename -test.csv

    and i need to convert him to an xml file (ex:test.xml)

    anyone know how can i make it possible.

    i have already tried many things but nothing work's.

    Please help me.

    thx

  2. #2

  3. #3

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Re: convert csv to xml

    little example:

    csv header:

    tiplin,tipdoc,numdocid,merc,name........

    csv data:

    C,C,123456787,0,name1.......
    C,C,123456788,1,name2.......
    C,C,123456789,2,name3......

    I was clear???

    thx

  4. #4
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: convert csv to xml

    Essentially what you would do is read in the CSV file line-by-line and parse it into a XML document object then save the XML object to disk. It's a fairly straightforward task although the syntax of using the XML document object is a bit tricky at first glance. Is there a particular part of the process you're getting hung up on?

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: convert csv to xml

    Here's a small start.

    vb Code:
    1. Dim ff As Integer
    2.     Dim strParts() As String
    3.     Dim strOneLine As String
    4.     ff = FreeFile
    5.     Dim strNodeNames() As String
    6.     Dim lngIndex As Long
    7.    
    8.     Open "C:\temp\csv.txt" For Input As #ff
    9.    
    10.     ReDim strNodeNames(0)
    11.    
    12.     Do Until EOF(ff)
    13.         Line Input #ff, strOneLine
    14.         strParts = Split(strOneLine, ",")
    15.         If UBound(strNodeNames) = 0 Then
    16.             ReDim strNodeNames(UBound(strParts))
    17.             For lngIndex = 0 To UBound(strParts) - 1
    18.                 strNodeNames(lngIndex) = strParts(lngIndex)
    19.             Next
    20.         Else
    21.             ' store the node values
    22.         End If
    23.     Loop
    24.    
    25.     ' When done write the nodes

  6. #6
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: convert csv to xml

    Two other ways:

    If you're only going to do this once, and have Excel available, read the file with Excel and save it as a worksheet.

    If you're going to be doing it more than once (I do it at least once a week, due to a client who can't understand that a csv file isn't a spreadsheet) and you have Excel available, just automate Excel to load the file you choose and save it as a worksheet. There's an excellent tutorial, including code, here.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: convert csv to xml

    Here's a complete solution.

    vb Code:
    1. Dim ff As Integer
    2.     Dim strParts() As String
    3.     Dim strOneLine As String
    4.     ff = FreeFile
    5.     Dim strAttributes() As String
    6.     Dim lngIndex As Long
    7.    
    8.     Dim oxmlCommentNode As IXMLDOMNode
    9.     Dim oxmlTips As IXMLDOMElement
    10.     Dim oxmlDoc As MSXML2.DOMDocument40
    11.     Dim oxmlPI As IXMLDOMProcessingInstruction
    12.     Dim oxmlRootNode As IXMLDOMElement
    13.    
    14.     Open "C:\temp\csv.txt" For Input As #ff
    15.    
    16.     ReDim strAttributes(0)
    17.    
    18.     Set oxmlDoc = New DOMDocument40
    19.    
    20.     ' Create the XML declaration
    21.     Set oxmlPI = oxmlDoc.createProcessingInstruction("xml", "version='1.0' encoding='ISO-8859-1'")
    22.     oxmlDoc.appendChild oxmlPI
    23.    
    24.     ' Add a comment nodes
    25.     Set oxmlCommentNode = oxmlDoc.createComment("Created by MartinLiss")
    26.     oxmlDoc.appendChild oxmlCommentNode
    27.  
    28.     ' Create the oxmlRootNode element
    29.     Set oxmlRootNode = oxmlDoc.createElement("Tips")
    30.     oxmlDoc.appendChild oxmlRootNode
    31.    
    32.     Do Until EOF(ff)
    33.         Line Input #ff, strOneLine
    34.         strParts = Split(strOneLine, ",")
    35.         If UBound(strAttributes) = 0 Then
    36.             ReDim strAttributes(UBound(strParts))
    37.             For lngIndex = 0 To UBound(strParts) - 1
    38.                 strAttributes(lngIndex) = strParts(lngIndex)
    39.             Next
    40.         Else
    41.             ' Create a <Tip> element and add it to the oxmlRootNode node
    42.             Set oxmlTips = oxmlDoc.createElement("Tip")
    43.             For lngIndex = 0 To UBound(strAttributes) - 1
    44.                 oxmlRootNode.appendChild oxmlTips
    45.                 ' Add the attributes and their values
    46.                 oxmlTips.setAttribute strAttributes(lngIndex), strParts(lngIndex)
    47.             Next
    48.         End If
    49.     Loop
    50.    
    51.  
    52.     ' Save to the XML file
    53.     oxmlDoc.save "C:\temp\csv.xml"
    54.     Close #ff
    If you want to be able to successfully copy/paste that code you can use the app you can download from the Copy/Paste VB Code link in my signature or an Add-In that does the same thing from the Paste VB Code Add-In link. You can also paste it into WordPad and then copy/paste from there into your app.

  8. #8

  9. #9
    Hyperactive Member
    Join Date
    Jun 2006
    Posts
    372

    something simpler

    ok, i know i'm beating a dead horse here, but here my shot:



    dim labs() as string

    Function tags(t) As String
    On Error Resume Next
    Dim z As Long

    For z = 0 To UBound(labs)
    t(z) = Join(Array("<", labs(z), ">", t(z), "</", labs(z), ">"), "")
    Next z

    tags = Join(t, "")

    End Function

    Function csv2xml(strCsv As String)
    On Error Resume Next 'handle missing blocks/invalid chars
    Dim ln() As String : Dim x As Long

    ln() = Split(strCsv, vbCrLf)
    labs = Split(ln(0), ",")

    For x = 1 To UBound(ln) 'if vba/vbs use a buffer var
    ln(x) = tags(Split(ln(x), ","))
    Next x

    csv2xml = "<csv>" & Join(ln(), vbCrLf) & "</csv"
    Erase labs()
    End Function

    'usage csv2xml(TheCsvAsString)


    pros/cons-
    non validating/parsing
    -wont freeze on erronous data, but not guaranteed to be well formed (though my trys were).

    main advantage is speed - >10X faster than msxml!
    this is due to not using any string concatenation in the loop, mainly a javascript principle.

    if you don't have xml-incompatible chars in your data, (your example didn't), this is the way to go. a few simple comment tags, and you can use it in IE/ASP as well.

    i just wrote this over the last half hour, so it's not perfect, but i like it, and it seems to be working. i parsed the win32api csv in 1.7 seconds!
    Last edited by rnd me; Mar 19th, 2007 at 05:46 PM.

  10. #10

    Thread Starter
    Member nf_vb's Avatar
    Join Date
    Mar 2007
    Location
    Portugal
    Posts
    35

    Re: convert csv to xml

    thx guys.

    you saved me the day

    I am owing one.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width