Converting xml data to csv data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bimeldip
    New Member
    • Aug 2007
    • 27

    Converting xml data to csv data

    Is it posibble to develop codes to convert and xml data to csv data ?
    If it is...can i know if there are any online toturials that i can follow to learn on this ?
  • scissors
    New Member
    • Aug 2007
    • 4

    #2
    While I'm pretty sure there are some IDEs out there that can do that, I would recommend an XSLT to do so.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      If you have an xml in the following form:
      <table>
      <row>
      <data>value</data>
      <data>value</data>
      </row>
      </table>

      You can easily create either scripts or xslt to convert the document. eg. xslt:
      Code:
      <xsl:template match="row">
        <xsl:for-each select="data">
          <xsl:value-of select="."/>
          <xsl:if test="not(last())">,</xsl:if>
        </xsl:for-each>
        <xsl:text>
      </xsl:text><!-- new line -->
      </xsl:template>
      However, if you have a very large document and performance is an issue, I would try an alternative to XSLT since XSLT requires a decent amount of overhead to build a DOM.

      As it is, we'd have to know what languages you can work with, and the form of your input to know more.

      Comment

      • bimeldip
        New Member
        • Aug 2007
        • 27

        #4
        Hi,
        i want to convert xml data to csv data so that i can export it into MySQL database....usi ng php or javascript...th e aren't much working examples online....can anyone suggest any tutorial online site for me to visit...?
        Thanks

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          You need to break this down into 2 seperate tasks:

          1. Convert xml to csv.
          2. Load csv into MySQL

          For #2, you're probably going to be using PHP as opposed to javascript.
          One way is here:
          http://dev.mysql.com/doc/refman/5.0/en/load-data.html[CODE="SQL"]LOAD DATA LOCAL INFILE '/importfile.csv'
          INTO TABLE test_table
          FIELDS TERMINATED BY ','
          LINES TERMINATED BY '\n'
          (field1, field2, field3);[/CODE]
          For #1, again taking advantage of existing XSLT processors would probably be the easiest for you, using the XSLT given above.
          http://ca3.php.net/manual/en/functio...orm-to-xml.php[CODE="php"]<?php

          // Load the XML source
          $xml = new DOMDocument;
          $xml->load('collecti on.xml');

          $xsl = new DOMDocument;
          $xsl->load('collecti on.xsl');

          // Configure the transformer
          $proc = new XSLTProcessor;
          $proc->importStyleShe et($xsl); // attach the xsl rules

          echo $proc->transformToXML ($xml);

          ?> [/CODE]

          Comment

          • bimeldip
            New Member
            • Aug 2007
            • 27

            #6
            Hi,
            Just out of curiousity....c an we export xml data to mySQL database directy?
            Without converting it to csv?
            Thanks for helping...!!!

            Comment

            • asearle
              New Member
              • Sep 2006
              • 39

              #7
              Hi guys,

              I've been following this thread as I also need to export to CSV (so that the user can import to Excel) but unfortunately I cannot use any server-side actions (e.g. PHP).

              I am using XSLT along with JavaScript and so am hoping that someone can point me towards a HOWTO that shows me how I could (easily) export data (often already dynamically filtered in with JavaScript/XSLT) to file.

              Any tips here would be most welcome.

              Regards,
              Alan Searle

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                Old way of doing it with javascript:
                Sorry! We can't seem to find the resource you're looking for

                but instead of using document.write, save the result to a file.

                Another way:
                An XSLTProcessor applies an XSLT stylesheet transformation to an XML document to produce a new XML document as output. It has methods to load the XSLT stylesheet, to manipulate parameter values, and to apply the transformation to documents.


                Converting xml to csv with xslt:


                I'm unaware of how to bulk load xml with mySQL; there are definitely methods with MSSQL, using mapping schemas.

                Comment

                • asearle
                  New Member
                  • Sep 2006
                  • 39

                  #9
                  These tips look great. Many thanks: I will experiment with these techniques and am sure that one will fit my needs.

                  There is one other thing that I will need to sort out which is how to save the result to a separate (text) file or maybe open directly in Notepad.

                  Here I have been googling on key words but tend to come up with a lot of commercial products offering this functionality. That's very nice but really I would like to understand the whole process and be able to do it myself so was wondering whether you have any pointers to a HOWTO showing how this can be done (maybe with javascript)?

                  Many thanks,
                  Alan Searle

                  Comment

                  Working...