`
hejiajunsh
  • 浏览: 407224 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

mysql 查询字段存储为xml格式中的某个标签

阅读更多

Mysql 5.1新增了操作XML的函数,

ExtractValue()  解析(搜索数据)

UpdateXML()  更新,还是蛮方便的。

 

详见:http://ftp.nchu.edu.tw/MySQL/tech-resources/articles/mysql-5.1-xml.html

 

MySQL version 5.1.5 has functions for searching and changing XML documents. This article has examples.

Let's make a database and put two XML documents in it.

CREATE TABLE x (doc VARCHAR(150));

INSERT INTO x VALUES
('
<book>
<title>A guide to the SQL standard</title>
<author>
<initial>CJ</initial>
<surname>Date</surname>
</author>
</book>
');

INSERT INTO x VALUES
('
<book>
<title>SQL:1999</title>
<author>
<initial>J</initial>
<surname>Melton</surname>
</author>
</book>
');

The doc columns have an internal hierarchical structure, with books containing titles and authors, and authors in turn containing initials and surnames. It's a popular way to format and store, and the "markup" -- words like "<book>" and </book>" -- makes it easy to see the hierarchy if you're careful about indentation.

ExtractValue()

Syntax
EXTRACTVALUE (XML_document, XPath_string);
1st Parameter
XML_document string formatted as in the example
2nd Parameter
XPath_string (XPath is a "sub-language")
Action
returns string containing a value from the document
Example #E1
mysql> SELECT EXTRACTVALUE(doc,'/book/author/initial') FROM x;
+------------------------------------------+
| EXTRACTVALUE(doc,'/book/author/initial') |
+------------------------------------------+
| CJ |
| J |
+------------------------------------------+
2 rows in set (0.01 sec)

What happened here? Books contain authors which contain initials. With EXTRACTVALUE() we navigated down through the hierarchy to get the values at the final node points: 'CJ' and 'J'. A basic extraction is just a matter of specifying the hierarchy in the XPath_string argument.

Example #E2
mysql> SELECT EXTRACTVALUE(doc,'/*/*/initial') FROM x;
+----------------------------------+
| EXTRACTVALUE(doc,'/*/*/initial') |
+----------------------------------+
| CJ |
| J |
+----------------------------------+
2 rows in set (0.01 sec)

You don't have to list the whole hierarchy. When part of a path is a wildcard, that means "any name will do".

Example #E3
mysql> SELECT extractValue(doc,'/book/child::*') FROM x;
+---------------------------------------------+
| extractValue(doc,'/book/child::*') |
+---------------------------------------------+
| A guide to the SQL standard |
| SQL:1999 |
+---------------------------------------------+
2 rows in set (0.00 sec)

With /book/child:: we find what's immediately below book, namely the title data. We could use a variety of operators here:
child ... what's immediately below
descendant ... what's below at all levels
parent ... what's immediately above
ancestor ... what's above at all levels
following-sibling ... what's next at same level
preceding-sibling ... what's before at same level
self ... not before, not after, same level

Example #E4
mysql> select
extractValue(doc,'/book/author/surname[self:text()="Date"]') from x;
+--------------------------------------------------------------+
| extractValue(doc,'/book/author/surname[self:text()="Date"]') |
+--------------------------------------------------------------+
| Date |
| |
+--------------------------------------------------------------+
2 rows in set (0.00 sec)

And here's one way to add a predicate (a conditional expression). By saying "in the text of self, that is, in the text of surname because the predicate immediately comes after surname, look for value = Date", we include book/author/surname=Date and we exclude book/author/surname=Melton. The Melton row is blank. Naturally = isn't the only operator we could use here; we could have self:text()>="Date"self:text()="Date" OR self:text()="Melton", and so on.

What you've seen is: an XPath expression can contain nodes separated by slashes (vaguely like a Unix path expression), and you can pick values from one or more nodes. Wildcards, navigation aids, and predicates are supported. Although the examples all used extractValue() in the SELECT list, it can be used in any statement wherever an expression is allowed. A good tip is to combine XML columns with fulltext indexing.

UpdateXML()

Now here's a new function for updating the structure.

Syntax
UPDATEXML (XML_document, XPath_string, new_value);
1st Parameter
XML_document string formatted as in the example
2nd Parameter
XPath_string (XPath is a "sub-language")
3rd Parameter
new_value to replace whatever is found
Action
changes string containing a value from the document
Example #U1
mysql> select UpdateXML(doc,'/book/author/initial','!!') from x;
+----------------------------------------------------------+
| UpdateXML(doc,'/book/author/initial','!!') |
+----------------------------------------------------------+
|
<book>
<title>A guide to the SQL standard</title>
<author>
!!
<surname>Date</surname>
</author>
</book> |
|
<book>
<title>SQL:1999</title>
<author>
!!
<surname>Melton</surname>
</author>
</book> |
+----------------------------------------------------------+
2 rows in set (0.00 sec)

UpdateXML's first two arguments are the same as for ExtractValue because the first thing we want to do is navigate to the node. The third argument is a replacement string. So we change book/author/initial to !!. The return value is the complete new document. To replace the document permanently, you could say UPDATE x SET doc = UpdateXML(doc,'/book/author/initial','!!');

But this is probably a mistake! We didn't just change the text to !!. We changed <initial>CJ></initial> to !! So we changed the document structure. Normally, we only want to change the contents. For that, we should say: select UpdateXML(doc,'/book/author/initial','<initial>!!</initial>') from x;

Example #U2
mysql> select
extractvalue(
UpdateXML(doc,'/book/author/initial','<initial>!!</initial>'),'/book/author/
initial') from x;
+---------------------------------------------------------------------------
--------------------------+
|
extractvalue(
UpdateXML(doc,'/book/author/initial','<initial>!!</initial>'),'/book/author/
initial') |
+---------------------------------------------------------------------------
--------------------------+
| !!
|
| !!
|
+---------------------------------------------------------------------------
--------------------------+
2 rows in set (0.01 sec)

This final example, a combination of ExtractValue() and UpdateXML(), shows what would happen if we change the initial node to !! and then select the initial node. Naturally, we get !!.

分享到:
评论

相关推荐

    将关系型数据库MySQL存储数据转换为XML文件的实现

    为了解决这一问题,将MySQL数据库中的数据转换为XML格式,既能满足网络传输的需求,又能确保数据的完整性和安全性。 XML是一种自我描述的语言,它的标记可以根据需要自由定义,使得数据结构清晰,易于解析和处理。...

    mysql数据表导出生成xml文件和树形结构

    MySQL 数据表导出生成 XML 文件是一项常见的数据转换任务,它允许我们把数据库中的结构化数据转化为一种便于交换和处理的格式。XML(eXtensible Markup Language)是一种标记语言,常用于存储、传输和表示数据,尤其...

    里面有mysql基本操作,自定义jsp标签,XML的解析

    `:查询表中的所有字段。 - `SELECT 字段1, 字段2, ... FROM 表名;`:查询表中指定的字段。 - **条件查询**: - `SELECT 字段1, 字段2, ... FROM 表名 WHERE 条件表达式;`:根据指定条件筛选数据。 - 使用 `IN` ...

    MySql读写Xml两种方式性能比较

    在MySQL数据库中,处理XML数据是一项常见的任务,特别是在需要与外部系统交换数据或者存储结构化但非表格形式的数据时。本篇文章将详细探讨MySQL中利用存储过程读取和写入XML数据的两种方法,主要关注使用`...

    Xml_2_Mysql.rar_XML mysql_mysql xml

    在实际应用中,XML数据转换为MySQL通常涉及以下几个步骤: 1. **解析XML文件**:首先,我们需要解析XML文件以获取其结构和内容。PHP中可以使用DOMDocument或者SimpleXMLElement类来解析XML。这些类提供了读取、遍历...

    解析wthrcdn.etouch的xml格式天气存入mysql数据库

    - 从XML中提取的数据可能需要进一步清洗,比如日期可能需要从字符串转换为日期类型,温度可能需要处理单位等。 - Python的`datetime`模块可以帮助我们进行日期转换,而`float`或`int`可以用于数值类型的转换。 4....

    mysql数据库对xml的支持

    这些功能使得MySQL不仅能够高效地存储XML数据,还能方便地查询和更新这些数据。 #### 创建XML数据表及插入XML文档 在创建支持XML的表时,通常会选择`VARCHAR`或`TEXT`类型来存储XML文档。例如: ```sql CREATE ...

    XML备份MySQL资料库

    XML备份MySQL数据库是一种将数据库中的数据转换为XML格式并存储为文件的过程,这对于数据的迁移、备份或在不同系统间交换数据非常有用。XML(eXtensible Markup Language)是一种可扩展标记语言,它允许结构化数据的...

    数据库转为XML格式

    在实际场景中,例如,Web服务通常通过RESTful API返回XML格式的数据,或者数据库备份和恢复时,将数据导出为XML便于长期存储和跨系统迁移。 总结,数据库转为XML格式是一项重要的数据处理技能,涉及数据库查询、...

    将数据库数据以XML文件格式保存

    在IT行业中,数据库是存储和管理结构化信息的核心工具,而XML(eXtensible Markup Language)是一种用于标记数据的标准格式,广泛应用于数据交换、配置文件以及数据存储。本主题聚焦于如何将数据库中的数据导出并...

    mysql和Oracle在对clob和blob字段的处理

    - 存储机制:MySQL的`TEXT`和`BINARY`系列数据类型通常存储在数据页中,而Oracle的`CLOB`和`BLOB`可以存储在表空间中,允许更大的数据量。 - 索引支持:Oracle支持对`CLOB`和`BLOB`字段建立索引,而MySQL的索引...

    jsp读取大对象CLOB并生成xml文件示例

    例如,我们有一个`documents`表,其中有一个名为`content`的CLOB字段,存储着XML格式的数据。 ```sql CREATE TABLE documents ( id INT PRIMARY KEY, title VARCHAR(255), content CLOB ); ``` 接下来,我们将...

    爬取数据并存储到mysql

    本主题将围绕“爬取数据并存储到MySQL”这一核心任务展开,讲解相关的技术知识点。 首先,我们要理解“爬取数据”的概念。爬虫(Spider)是一种自动浏览互联网并抓取网页内容的程序。在Python中,有许多流行的爬虫...

    从mysql数据库中批量下载Blob图片到本地

    本教程将深入探讨如何在Java环境下,批量地从MySQL数据库中下载存储在Blob字段中的图片,并将其保存到本地文件系统。 首先,我们需要理解Blob类型。Blob是MySQL中的一个数据类型,用于存储大量二进制数据。它分为四...

    mybatis-mysql数据库生成mybatis相关文件xml、mapper、bean

    在MySQL数据库中生成MyBatis相关的XML、Mapper接口和Bean类,可以极大地提高开发效率,减少手动编写这些基础代码的工作量。这个过程通常涉及到以下几个步骤和知识点: 1. **MySQL数据库连接**:在配置XML文件时,...

    数据库与xml的导入导出

    2. 导入XML:Oracle的XMLType是内置的数据类型,可以直接存储XML文档。可以使用`XMLType`对象的`getClob()`方法将XML数据转换为CLOB,然后插入到支持CLOB类型的列。或者使用`INSERT INTO ... VALUES (XMLType('&lt;xml&gt;...

    mybatis导出xml文件(只支持mysql数据库)

    总结来说,"mybatis导出xml文件(只支持mysql数据库)"涉及到MyBatis框架中的一种自动化工具,它能帮助开发者快速生成针对MySQL数据库的MyBatis XML映射文件,从而简化开发流程,提高工作效率。这种工具通常基于数据库...

    mysql 让hibernate支持text字段的方言

    在MySQL中,`TEXT`字段类型用于存储大量的文本数据,包括`TINYTEXT`, `TEXT`, `MEDIUMTEXT`, 和 `LONGTEXT`,它们分别有不同的最大长度。然而,Hibernate在默认情况下可能不直接支持所有这些字段类型,尤其是在老...

    matlab开发-generateXML

    在MATLAB中生成XML文件是将数据转换为结构化格式并进行存储或交换的有效方法,尤其是在需要与数据库系统交互时。XML(eXtensible Markup Language)是一种自描述的标记语言,广泛应用于数据交换、配置文件和存储结构...

    sql_xml.rar_.sql文件数据表_sql to xml_xml 数据库

    例如,在SQL Server中,我们可以使用`FOR XML`子句,将查询结果集转化为XML格式。这是一个简单的例子: ```sql SELECT * FROM Customers FOR XML AUTO; ``` 这段代码会将`Customers`表中的所有数据以XML格式返回。...

Global site tag (gtag.js) - Google Analytics