简体   繁体   中英

Why this “extractvalue” returns null

I need to extract values directly from an xml file, one field precisely, but I always get NULL values.

I have no problems importing the file into a table. The file is located in c:\\xampp\\mysql\\data\\test_folder\\test_file.xml.

I use this way to import it:

LOAD XML INFILE 'test_file.xml'
INTO TABLE tbl_tutorials
CHARACTER SET utf8
ROWS IDENTIFIED BY '<row>'
;

And I've allready created the table this way

    CREATE TABLE IF NOT EXISTS tbl_tutorials(
    item_id INT(11) NOT NULL,
    title VARCHAR(100) NOT NULL,
    link VARCHAR(120) NOT NULL,
    description VARCHAR(400) NOT NULL,
    keywords VARCHAR (50) NOT NULL
) ;

My test_file.xml is this

<?xml version="1.0" encoding="UTF-8"?>
<tbl_tutorials>
<row>
    <item_id>1</item_id>
    <title>test title 1</title>
    <link>test link 1</link>
    <description>test description 1</description>
    <keywords>test keyword  1</keywords>
</row>
<row>
    <item_id>2</item_id>
    <title>test title 2</title>
    <link>test link 2</link>
    <description>test description 2</description>
    <keywords>test keyword 2</keywords>
</row>
<row>
    <item_id>3</item_id>
    <title>test title 3</title>
    <link>test link 3</link>
    <description>test description 3</description>
    <keywords>test keyword 3</keywords>
</row>

</tbl_tutorials>

What I'm trying to do is create a "var" (don't know if this is te exact term) that contais all the xml code and then, from that var, extract the values needed.

This way ...

SET @xmlFile = load_file('c:\xampp\mysql\data\test_folder\test_file.xml ');
SELECT extractvalue(@xmlFile , '/tbl_tutorials/row/keywords') keywords;

What i get is

------------
| keywords |
------------
|NULL      |
------------

And what i need is

----------------
| keywords     |
----------------
|test keyword 1|
----------------

I think that i must specify exactly which child element is needed, but I don't know why allways returns me null.

I'm ussing mySql workbench and Xampp

Thanks everybody

Edit

I just added antoher "\\" backslash to the path of the file and now the extractvalue returns "blob", this way

SET @xmlFile = load_file('c:\\xampp\\mysql\\data\\test_folder\\test_file.xml ');



------------
| keywords |
------------
|BLOB      |
------------

Your xml file is invalid, it is missing the closing </tbl_tutorials> , thus you get null (for whatever you try to extract).

If you fix it, your query will return every keyword though, see the documentation for ExtractValue :

If multiple matches are found, the content of the first child text node of each matching element is returned (in the order matched) as a single, space-delimited string.

To get the keywords for the first "row"-tag, you can use

SELECT ExtractValue(@xmlFile , '/tbl_tutorials/row[1]/keywords') keywords

Note that the "row"-tags are numbered, not the keywords. If you use eg

SELECT ExtractValue(@xmlFile , '/tbl_tutorials/row/keywords[1]') keywords

you get the first keyword of every row, which is in your case every keyword; you can use this if you eg have several keywords per row, then this would give you the first (of each row).

You can also specify the row by its attributes, eg an item_id :

SELECT ExtractValue(@xmlFile , '/tbl_tutorials/row[item_id="1"]/keywords') keywords

To get the total number of keyword tags (that would be returned by your original query), you can use

SELECT ExtractValue(@xmlFile , 'count(/tbl_tutorials/row/keywords)') cnt 

I found a solution to the problem of BLOB and the question in general here

MySQL Workbench shows results as BLOB

Thanks everybody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM