MySQL从XML文件

时间:2016-04-07 15:28:32

标签: java mysql xml

我有一个像这样的xml文件:

<dbReference type="PM" id="17224074"/>
<dbReference type="DOI" id="10.1186/bcr1637"/>
</citation>
<scope>VARIANTS ILE-282 AND ASN-777</scope>
</reference>
<comment type="function">
<text evidence="24">Calcium.</text>
</comment>
<comment type="function">
<text evidence="24">Has a strong inhibitory effect on APP C99 and C83 production.</text>
</comment>
<comment type="subunit">
<text evidence="5 13">Homodimer; disulfide-linked.</text>
</comment>
<comment type="interaction">
<interactant intactId="EBI-727477"/>
<interactant intactId="EBI-7644904">
<id>Q9JIY2</id>
<label>Cbll1</label>
</interactant>
<organismsDiffer>true</organismsDiffer>
<experiments>21</experiments>
</comment>

我想只提取

中的信息
<comment type="function">...</comment>

在这个例子中是:'钙'。 AND'作为对APP C99和C83产生的强烈抑制作用。'

我有这个表,我想保存数据:

CREATE TABLE IF NOT EXISTS INFORMATION (id varchar(255) NOT NULL, name varchar(255), entry varchar(255), comment longtext, PRIMARY KEY (id)); 

我会保存'钙'。 AND'对APP C99和C83的生产具有很强的抑制作用。在名为“comment”的列中。 我认为我可以使用LOAD XML将此信息直接从xml插入到表中,但我的xml文件有太多不同的字段。 我该怎么做呢?我是否必须首先从xml中提取数据然后插入表中?

2 个答案:

答案 0 :(得分:2)

一个有用的选项:

文件:'/ path / to / xml / file / xmlfile.xml'

mysql> DROP TEMPORARY TABLE IF EXISTS `temp_information`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> DROP TABLE IF EXISTS `information`;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE IF NOT EXISTS `information` (
    -> `id` VARCHAR(255) NOT NULL,
    -> `name` VARCHAR(255),
    -> `entry` VARCHAR(255),
    -> `comment` LONGTEXT,
    -> PRIMARY KEY (`id`)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TEMPORARY TABLE IF NOT EXISTS `temp_information` (
    -> `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    -> `type` VARCHAR(20),
    -> `text` TEXT,
    -> `evidence` VARCHAR(20)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> LOAD XML INFILE '/path/to/xml/file/xmlfile.xml'
    -> INTO TABLE `temp_information`
    -> ROWS IDENTIFIED BY '<comment>';
Query OK, 4 rows affected (0.00 sec)
Records: 4  Deleted: 0  Skipped: 0  Warnings: 0

mysql> INSERT INTO `information` (`id`, `comment`)
    -> SELECT UUID(), GROUP_CONCAT(`text` ORDER BY `id` SEPARATOR ' ')
    -> FROM
    -> `temp_information`
    -> WHERE
    -> `type` = 'function'
    -> GROUP BY `evidence`;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> SELECT
    -> `id`,
    -> `name`,
    -> `entry`,
    -> `comment`
    -> FROM
    -> `information`;
+--------------------------------------+------+-------+------------------------------------------------------------------------+
| id                                   | name | entry | comment                                                                |
+--------------------------------------+------+-------+------------------------------------------------------------------------+
| e720d259-fcde-11e5-be3f-a4badbf9ce21 | NULL | NULL  | Calcium. Has a strong inhibitory effect on APP C99 and C83 production. |
+--------------------------------------+------+-------+------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> DROP TEMPORARY TABLE IF EXISTS `temp_information`;
Query OK, 0 rows affected (0.00 sec)

MySQL命令行:

"series".pluralize
# => "series"

"series".singularize
# => "series"

检查:

答案 1 :(得分:1)

使用XML解析器(例如SAXParser)首先解析文件,然后遍历节点,查找注释节点。对于每个具有&#34;函数&#34;类型的文件,将节点文本放入数据库中。

相关问题