MYSQL解析XML数据

来源:互联网 发布:机械演示制作软件 编辑:程序博客网 时间:2024/06/07 04:42

解析MYSQL数据库中的xml数据

本次分享是解析数据库中的xml文件,获取节点的属性及值:

  • 准备xml数据
  • 解析xml节点属性及值

准备XML数据

<?xml version="1.0" encoding="utf-8"?><root>   <meta name="description">我在做测试</meta>    <element name="节点1">     <child name="子节点1">子节点1值</child>   </element>    <element name="节点2">     <child name="子节点2">子节点2值</child>   </element> </root>

解析xml节点属性及值

Mysql采用内置函数EXTRACTVALUE(XML_document,XPath_string)。筛选规则【/节点】

SET @temp_xml = '<?xml version="1.0" encoding="UTF-8"?><root>    <meta name="description">我在做测试</meta>    <element name="节点1">        <child name="子节点1">子节 点1值</child>    </element>    <element name="节点2">        <child name="子节点2">子节点2值</child>    </element>  </root>';select extractvalue(@temp_xml,'/root/element/child/@name') attr, extractvalue(@temp_xml,'/root/element/child') value 
原创粉丝点击