mysql - 将内容指定为属性的 XML 导入 MySQL 表?

标签 mysql xml

我有一个如下所示的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<comments>
  <row Id="1" PostId="1" Score="3" Text="Maxis uses this in The Sims when clicking on your person to perform actions. ex: call-&gt;friend-&gt;[friends name]" CreationDate="2010-08-09T19:52:30.690" UserId="12" />
  <row Id="2" PostId="1" Score="1" Text="Here are some examples: http://en.wikipedia.org/wiki/Pie_menu#Notable_implementations" CreationDate="2010-08-09T19:54:54.007" UserId="31" />
  <row Id="3" PostId="9" Score="13" Text="How about making it short and with less steps :)" CreationDate="2010-08-09T19:55:57.670" UserId="35" />
  <row Id="4" PostId="3" Score="1" Text="10 sec seems like an awful lot when you are browsing." CreationDate="2010-08-09T19:57:57.057" UserId="32" />
  ...
</comments>

我环顾四周,发现 this slightly related question ,但它有如下所示的行:

<row>
  <field name="personal_number">539</field>
  <field name="firstname">Name</field>
  <field name="lastname">Surname</field>
  <field name="email">email.domain.com</field>
  <field name="start_time">2011-04-02 13:30:00</field>
  <field name="end_time">2011-04-02 18:15:00</field>
  <field name="employee_category">1,2,4,5,22,37,38,39,41,43,44</field>
</row>

每行的每个字段都有一个 XML 元素 - 而不是像我那样,每行的单个元素将内容指定为属性。

如何将其导入 MySQL?我几乎可以使用任何技术,但我宁愿远离任何需要 Windows 的技术。

最佳答案

参见 http://dev.mysql.com/doc/refman/5.6/en/load-xml.html LOAD XML INFILE 支持的 XML 格式声明。

手册页指出:

the import routine automatically detects the format for each row and interprets it correctly. Tags are matched based on the tag or attribute name and the column name.

其中一种受支持的格式确实与您的类似——将列存储为 <row> 的属性元素。

如果您需要将 XML 数据转换为 MySQL 的 LOAD XML 支持的格式之一,您可能会使用 xsltproc .

我假设如果您的 XML 数据格式不完全一致,它会混淆 LOAD XML INFILE。

关于mysql - 将内容指定为属性的 XML 导入 MySQL 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552826/

相关文章:

iphone - 如何获取 RSS 提要中的下 20 个项目?

mysql - 将 MySQL 表导出到 Microsoft SQL 服务器

php - 如何为从 PHP 中的数据库中获取的数据创建下一个、上一个链接?

php - 如何通过连接两个表来显示所有结果和空值?

xml - 如何在 SOAPUI 中处理 http 日志中的 UTF-8 字符编码

xml - 如何在 Swagger 文档中添加 xml 示例

android - 对 api Slim 框架的请求失败

mysql - 忽略类型转换 IN

java - XML 模式创建需要很长时间

java - 如何将 org.jdom.Document 转换为字符串