Mysql 从XML数据创建SQL表

Mysql 从XML数据创建SQL表,mysql,xml,Mysql,Xml,我有一个包含以下数据的XML文件: <?xml version="1.0" encoding="utf-8"?> <countries> <country id="Canada"> <location> <code>CAXX0001</code> <name>Abbotsford</name> </locat

我有一个包含以下数据的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<countries>
    <country id="Canada">
        <location>
            <code>CAXX0001</code>
            <name>Abbotsford</name>
        </location>
        <location>
            <code>CAXX0002</code>
            <name>Agassiz</name>
        </location>
    </country>
    <country id="Belgium">
        <location>
            <code>BEXX0001</code>
            <name>Anderlecht</name>
        </location>
    </country>
</countries>
我需要使用其中的数据创建一个数据库表(MYSQL 5.1) 使用以下字段

countryName:(从国家标签的id属性中读取),
位置代码:(从代码子标记的值中读取),
位置名称:(从名称子标记的值中读取)

任何有关SQL语法的帮助都将不胜感激。

谢谢

使用Python SAX解析器处理XML文件。

谢谢。 我找到了一个使用loadXMLinfle方法的解决方案


http://grox.net/doc/mysql/refman-5.5-en.html-chapter/sql-syntax.html#load-xml使用PHP来实现这一点。这真的很简单。:-)

因此,基本上,您首先获取XML,然后删除任何特殊字符(有时XML将作为&-lt-;列&-gt-;(当然要减去破折号!),因此您可以使用特殊字符解码来消除这种情况,例如“”。然后使用简单的XML将字符串加载到XML数组中。然后使用json_编码将XML转换为json。然后使用json_解码将json数组转换回关联数组。一旦将其转换为该形式,您就可以使用FOREACH命令对数组进行迭代。我注意到,这似乎是ys生成一个数组,您可以遍历该数组,该数组中有两个元素:NewDataSet和Table。所以我总是这样做:

 foreach( $array['NewDataSet']['Table'] as $k=>$v ){
      .   .   .
 }
它还可以在其中插入空白数组。这些数组是在没有某个值时出现的。因此INSERT命令如下所示:

foreach( $array["NewDataSet"]["Table"] as $k=>$v ){
    $sql = "insert into categories (";
    $val = "values (";
    foreach( $v as $k1=>$v1 ){
        $sql .= "$k1,";

        if( is_numeric($v1) ){ $val .= "$v1,"; }
            else if( is_array($v1) ){ $val .= "'',"; }
            else { $val .= "'$v1',"; }
        }

    $sql = substr( $sql, 0, -1 ) . ") " . substr( $val, 0, -1 ) . ")";
    $s = dosql( $sql );
    }
但是,您可以只更改一些内容来执行CREATETABLE命令

<BrandUpdate>
<Input>
<param name="CustomerNumber" maxlength="5" type="xs:numeric-string">Customer Number</param>
<param name="UserName" maxlength="50" type="xs:string">User Name</param>
<param name="Password" maxlength="15" type="xs:string">Password</param>
<param name="Source" maxlength="8" type="xs:string">Description of source using service</param>
</Input>
<Output>
<DataSet>
<xs:schema id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="BRDNO" type="xs:decimal" minOccurs="0" MaxLength="4,0" Description="Brand Id"/>
<xs:element name="BRDNM" type="xs:string" minOccurs="0" MaxLength="50" Description="Brand Name"/>
<xs:element name="BRDURL" type="xs:string" minOccurs="0" MaxLength="50" Description="Brand URL"/>
<xs:element name="ITCOUNT" type="xs:int" minOccurs="0" Description="Count of Brand Items"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element></xs:schema>
</DataSet>
</Output>
</BrandUpdate>
我通过以下方式打开连接:

echo "Establishing a connection to the database...please wait.\n";
$mysqli = new mysqli( "<HOST>", "<USR>", "<PWD>", "<TABLE>" );
if( $mysqli->connect_errno ){
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    exit;
    }
echo“正在建立与数据库的连接…请稍候。\n”;
$mysqli=newmysqli(“,”,“,”,“,”);
如果($mysqli->connect\u errno){
echo“未能连接到MySQL:(“$mysqli->connect\u errno.”“$mysqli->connect\u error;
出口
}

我希望这能对您有所帮助。:-

有没有一种方法可以用纯SQL语法来实现呢??
################################################################################
#   dosql(). Do the SQL command.
################################################################################
function dosql( $sql )
{
    global $mysqli;

    echo "SQL = $sql\n";
    $res = $mysqli->query( $sql );
    if( !$res ){
        $ary = debug_backtrace();
        if( isset($ary[1]) ){ $a = $ary[1]['line']; }
            else if( isset( $ary[0]) ){ $a = $ary[0]['line']; }
            else { $a = "???"; }

        echo "ERROR @ " . $a . " : (" .  $mysqli->errno . ")\n" . $mysqli->error . "\n\n";
        echo "SQL = $sql\n";
        exit;
        }

    if( preg_match("/insert/i", $sql) ){ return $mysqli->insert_id; }
    if( preg_match("/delete/i", $sql) ){ return true; }
    if( !is_object($res) ){ return null; }

    $cnt = -1;
    $ary = array();
    $res->data_seek(0);
    while( $row = $res->fetch_assoc() ){
        $cnt++;
        foreach( $row as $k=>$v ){ $ary[$cnt][$k] = $v; }
        }

    return $ary;
}
echo "Establishing a connection to the database...please wait.\n";
$mysqli = new mysqli( "<HOST>", "<USR>", "<PWD>", "<TABLE>" );
if( $mysqli->connect_errno ){
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    exit;
    }