Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract value from xml in mysql

Tags:

mysql

xml

i am trying to extract values from xml. i am getting problem when xml has attribute. like following Stored Procedure

DELIMITER $$
DROP PROCEDURE IF EXISTS `excel`.`insert_items` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `insert_items`()
BEGIN
SET @xml = '<items><item>
        <value columntype="0">Single Line Text_01</value>
    <value columntype="1">Single Line Text_12341</value>
    <value columntype="2">Single Line Text_21</value>
    <value columntype="3">Single Line Text_31</value>
    <value columntype="4">Single Line Text_41</value>
    </item>
</items>';
SELECT @columntype, ExtractValue(@xml, 'items/item/value[items/item/value/@columntype=0]');    
END $$
DELIMITER ;
like image 242
KSHiTiJ Avatar asked May 10 '13 05:05

KSHiTiJ


People also ask

How to extract value in SQL?

extractValue() [Oracle SQL] extractValue() is similar to extract(), but it extracts the value without the XML element tags. It only works if it operates on a single element, otherwise, it throws an ORA-19025: EXTRACTVALUE returns value of only one node.

Can we use XML and MySQL together?

It is possible to obtain XML-formatted output from MySQL in the mysql and mysqldump clients by invoking them with the --xml option.

How read data from XML file and insert into database in PHP?

php $con = mysql_connect("localhost","user","username"); if (! $con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db_name", $con); if(! $xml=simplexml_load_file('syntest.

Which version of MySQL has load XML option?

MySQL :: MySQL 8.0 Reference Manual :: 13.2. 8 LOAD XML Statement.


1 Answers

To get a value of an element with attribute columntype="0"

SELECT ExtractValue(@xml, 'items/item/value[@columntype=0]') value;

Output:

|               VALUE |
-----------------------
| Single Line Text_01 |

SQLFiddle

like image 156
peterm Avatar answered Sep 27 '22 21:09

peterm