XML output from MySQL

后端 未结 2 1177
遇见更好的自我
遇见更好的自我 2020-12-06 18:10

is there any chance of getting the output from a MySQL query directly to XML?

Im referring to something like MSSQL has with SQL-XML plugin, for example

2条回答
  •  Happy的楠姐
    2020-12-06 18:41

    The mysql command can output XML directly, using the --xml option, which is available at least as far back as MySql 4.1.

    However, this doesn't allow you to customize the structure of the XML output. It will output something like this:

    
    
      
        129
        107
        Eastern
      
    
    

    And you want:

    
    
      
        
      
    
    

    The transformation can be done with XSLT using a script like this:

    
    
    
      
      
    
      
        
          
        
      
    
      
        
          
        
      
    
    
    

    This is obviously way more verbose than the concise MSSQL syntax, but on the other hand it is a lot more powerful and can do all sorts of things that wouldn't be possible in MSSQL.

    If you use a command-line XSLT processor such as xsltproc or saxon, you can pipe the output of mysql directly into the XSLT program. For example:

    mysql -e 'select * from table' -X database | xsltproc script.xsl -
    

提交回复
热议问题