如何使用自闭标签在MySQL中加载XML?

时间:2014-08-21 10:20:05

标签: mysql xml

我试图将一些XML加载到MySQL数据库,但我遇到了自关闭标记的问题。用于演示我的问题的示例SQL:

CREATE TEMPORARY TABLE `person` (
    `name` VARCHAR(75)
);

CREATE TEMPORARY TABLE `individual` LIKE `person`;

LOAD XML INFILE 'document.xml'
    INTO TABLE `individual`
    ROWS IDENTIFIED BY '<individual>';

LOAD XML INFILE 'document.xml'
    INTO TABLE `person`
    ROWS IDENTIFIED BY '<person>';

当自闭标签后跟一个带有开始和结束标签的元素时,负载按预期工作:

<?xml version="1.0"?>
<document>
    <individual name="John Smith"></individual>
    <person name="Joe Doe" />
    <person name="Jane Doe" />
</document>


SELECT * FROM `individual`;
+------------+
| name       |
+------------+
| John Smith |
+------------+
1 row in set
SELECT * FROM `person`;
+----------+
| name     |
+----------+
| Joe Doe  |
| Jane Doe |
+----------+
2 rows in set

但是在使用自闭标签表示的一个元素之后,未加载以下元素:

<?xml version="1.0"?>
<document>
  <individual name="John Smith" />
  <person name="Joe Doe" />
  <person name="Jane Doe" />
</document>


SELECT * FROM `individual`;
+------------+
| name       |
+------------+
| John Smith |
+------------+
1 row in set
SELECT * FROM `person`;
Empty set

如何将带有多个自关闭标签的XML文件加载到MySQL中?

1 个答案:

答案 0 :(得分:1)

这似乎是一个错误,您可以将其报告给MySQL Bugs

作为一种可能的解决方法,您可以生成如下所示的XML:

<?xml version="1.0"?>
<document>
  <table>
    <individual name="John Smith" />
  </table>
  <table>
    <person name="Joe Doe" />
    <person name="Jane Doe" />
  </table>
</document>