MySQL:从异常格式化的XML文件中提取数据

时间:2017-12-02 21:49:43

标签: mysql xml

我有一个XML文件,其条目如下所示:

<card name="Fire Toad" id="7366070c-c74d-4bb9-b3d8-23177e887073">
    <property name="Sphere" value="Nature" />
    <property name="Cost" value="1" />
    <property name="Type" value="Creature" />
    <property name="Subtype" value="Animal Toad" />
    <property name="Attack" value="3" />
    <property name="Defense" value="2" />
    <property name="Gems" value="B" />
    <property name="Rules" value="" />
    <property name="Flavor" value="Fire toads have the uncanny ability to cook their food while chewing." />
    <property name="Rarity" value="Common" />
    <property name="Series" value="1" />
    <property name="Number" value="106" />
    <property name="Illustrator" value="Kevin Shoemaker" />
</card>

我想从中提取数据(对于每张卡:name,id和所有属性)并将其插入MySQL数据库的表中。如果我可以使用内置的LOAD XML语句会很棒,但不幸的是它不支持我的文件格式。最方便的方法是什么?

1 个答案:

答案 0 :(得分:1)

考虑XSLT,它非常像SQL是一种声明性的专用语言,但旨在将XML源转换为特定格式,如MySQL LOAD XML方法所需的XML。 XSLT可以通过命令行运行Bash / Powershell,任何通用语言(Java,C#,PHP,Perl,Python,R,VB),专用处理器,如Saxon/Xalan,甚至是您的日常Excel!

实际上,您甚至可以通过向终端发送shell命令直接从MySQL的命令行客户端运行XSLT。这是一个xsltproc(可用于Linux / Mac的处理器)调用:

mysql> \! xsltproc -o /path/to/Output.xml /path/to/Script.xsl /path/to/Input.xml

以下脚本解析为 card 节点,并将 property 子节点迁移到新元素和值。

XSLT (另存为.xsl,一个特殊的.xml文件)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/root">  
        <data> 
            <xsl:apply-templates select="cards"/>
        </data>
    </xsl:template>

    <xsl:template match="cards">  
        <xsl:apply-templates select="card"/>
    </xsl:template>

    <xsl:template match="card"> 
        <row>
           <name><xsl:value-of select="@name"/></name>
           <id><xsl:value-of select="@id"/></id>
           <xsl:apply-templates select="property"/>
        </row>       
    </xsl:template>

    <xsl:template match="property">
    <xsl:element name="{@name}"> 
        <xsl:value-of select="@value"/>
    </xsl:element>
    </xsl:template>

</xsl:stylesheet>

输入XML (假设采用此格式的结构,在XSLT中调整根名称)

<?xml version="1.0"?>
<root>
  <more_nodes/>
  <still_nodes/>
  <cards>
    <card name="Fire Toad 1" id="7366070c-c74d-4bb9-b3d8-23177e887073">
      <property name="Sphere" value="Nature 1"/>
      <property name="Cost" value="1"/>
      <property name="Type" value="Creature 1"/>
      <property name="Subtype" value="Animal Toad 1"/>
      <property name="Attack" value="3"/>
      <property name="Defense" value="2"/>
      <property name="Gems" value="B1"/>
      <property name="Rules" value=""/>
      <property name="Flavor" value="Fire toads have the uncanny ability to cook their food while chewing. 1"/>
      <property name="Rarity" value="Common 1"/>
      <property name="Series" value="1"/>
      <property name="Number" value="106"/>
      <property name="Illustrator" value="Kevin Shoemaker 1"/>
    </card>
    <card name="Fire Toad 2" id="7366070c-c74d-4bb9-b3d8-23177e887073">
      <property name="Sphere" value="Nature 2"/>
      <property name="Cost" value="1"/>
      <property name="Type" value="Creature 2"/>
      <property name="Subtype" value="Animal Toad 2"/>
      <property name="Attack" value="3"/>
      <property name="Defense" value="2"/>
      <property name="Gems" value="B2"/>
      <property name="Rules" value=""/>
      <property name="Flavor" value="Fire toads have the uncanny ability to cook their food while chewing. 2"/>
      <property name="Rarity" value="Common 2"/>
      <property name="Series" value="1"/>
      <property name="Number" value="106"/>
      <property name="Illustrator" value="Kevin Shoemaker 2"/>
    </card>
    <card name="Fire Toad 3" id="7366070c-c74d-4bb9-b3d8-23177e887073">
      <property name="Sphere" value="Nature 3"/>
      <property name="Cost" value="1"/>
      <property name="Type" value="Creature 3"/>
      <property name="Subtype" value="Animal Toad 3"/>
      <property name="Attack" value="3"/>
      <property name="Defense" value="2"/>
      <property name="Gems" value="B3"/>
      <property name="Rules" value=""/>
      <property name="Flavor" value="Fire toads have the uncanny ability to cook their food while chewing. 3"/>
      <property name="Rarity" value="Common 3"/>
      <property name="Series" value="1"/>
      <property name="Number" value="106"/>
      <property name="Illustrator" value="Kevin Shoemaker 3"/>
    </card>
  </cards>
  <other_nodes/>
</root>

输出XML (已准备好使用MySQL&#39; LOAD XML)

<?xml version="1.0"?>
<data>
  <row>
    <name>Fire Toad 1</name>
    <id>7366070c-c74d-4bb9-b3d8-23177e887073</id>
    <Sphere>Nature 1</Sphere>
    <Cost>1</Cost>
    <Type>Creature 1</Type>
    <Subtype>Animal Toad 1</Subtype>
    <Attack>3</Attack>
    <Defense>2</Defense>
    <Gems>B1</Gems>
    <Rules/>
    <Flavor>Fire toads have the uncanny ability to cook their food while chewing. 1</Flavor>
    <Rarity>Common 1</Rarity>
    <Series>1</Series>
    <Number>106</Number>
    <Illustrator>Kevin Shoemaker 1</Illustrator>
  </row>
  <row>
    <name>Fire Toad 2</name>
    <id>7366070c-c74d-4bb9-b3d8-23177e887073</id>
    <Sphere>Nature 2</Sphere>
    <Cost>1</Cost>
    <Type>Creature 2</Type>
    <Subtype>Animal Toad 2</Subtype>
    <Attack>3</Attack>
    <Defense>2</Defense>
    <Gems>B2</Gems>
    <Rules/>
    <Flavor>Fire toads have the uncanny ability to cook their food while chewing. 2</Flavor>
    <Rarity>Common 2</Rarity>
    <Series>1</Series>
    <Number>106</Number>
    <Illustrator>Kevin Shoemaker 2</Illustrator>
  </row>
  <row>
    <name>Fire Toad 3</name>
    <id>7366070c-c74d-4bb9-b3d8-23177e887073</id>
    <Sphere>Nature 3</Sphere>
    <Cost>1</Cost>
    <Type>Creature 3</Type>
    <Subtype>Animal Toad 3</Subtype>
    <Attack>3</Attack>
    <Defense>2</Defense>
    <Gems>B3</Gems>
    <Rules/>
    <Flavor>Fire toads have the uncanny ability to cook their food while chewing. 3</Flavor>
    <Rarity>Common 3</Rarity>
    <Series>1</Series>
    <Number>106</Number>
    <Illustrator>Kevin Shoemaker 3</Illustrator>
  </row>
</data>