MySQL 解析 XML

版权声明:大家好,我是笨笨,笨笨的笨,笨笨的笨,转载请注明出处,谢谢! https://blog.csdn.net/jx520/article/details/85196938

extractvalue 查找

extractvalue("XML内容", "用于匹配的表达式") 另名

SET @my_xml ='
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<root>
  <child att="子节点1属性" >子节点1内容,<grandson att="孙节点1属性">孙节点1内容,</grandson></child>
	<child att="子节点2属性" >子节点2内容,<grandson att="孙节点2属性">孙节点2内容,</grandson></child>
	<foster_child att="养子节点1属性" >养子节点1内容,<grandson att="养子孙节点1属性">养子孙节点1内容,</grandson></foster_child>
</root>
<隔壁老王>
  <child att="便宜儿子节点1属性" >便宜儿子节点1内容,<grandson att="便宜孙子节点1属性">便宜孙子节点1内容,</grandson></child>
</隔壁老王>
<child>一个逆子<grandson>一个逆孙子,</grandson></child>
';
select extractvalue(@my_xml,'/root/child/@att')`att`, -- 取属性用 @
extractvalue(@my_xml,'/root/child') `child_node`, -- 子节点
extractvalue(@my_xml,'/root/child/grandson') `grandson_node`, -- 孙节点
extractvalue(@my_xml,'//child') `any_child_node`, -- // 取任何位置的 child
extractvalue(@my_xml,'/*/*/grandson') `root_grandson_node` -- * 通配符 

在这里插入图片描述

UpdateXML 更新

UpdateXML("XML内容", "用于匹配的表达式", "要替换的值") as 另名

SET @my_xml ='
<root>
  <child father="root" >子节点1内容,<grandson att="孙节点1属性">孙节点1内容,</grandson></child>
</root>
';
SELECT UpdateXML(@my_xml, '/root/child/@father', 'father="隔壁老王"') as `真爹`,
UpdateXML(@my_xml, '/root/child', '<child>便宜儿子</child>') as `便宜儿子`

在这里插入图片描述

官方传送门 https://dev.mysql.com/doc/refman/5.7/en/xml-functions.html

猜你喜欢

转载自blog.csdn.net/jx520/article/details/85196938