If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > select from xml field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-31-12, 09:07
altarek altarek is offline
Registered User
 
Join Date: Jan 2012
Posts: 1
select from xml field

Hi

I have mysql field i want to select from it,

<?xml version="1.0" encoding="UTF-8"?>
<record
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://www.loc.gov/MARC21/slim">

<leader>00764nas </leader>
<controlfield tag="221">011258</controlfield>

<datafield tag="578" ind1=" " ind2=" ">
<subfield code="a">jaun</subfield>
</datafield>
<datafield tag="356" ind1=" " ind2=" ">
<subfield code="a"></subfield>
<subfield code="c"></subfield>
</datafield>
<datafield tag="333" ind1=" " ind2=" ">
<subfield code="d">ali</subfield>
<subfield code="p">22</subfield>
</datafield>
<datafield tag="222" ind1=" " ind2=" ">
<subfield code="c"></subfield>
<subfield code="d">1984</subfield>
</datafield>

</record>
mysql version is 5.0 not support
extract value function is there another way to solve it
Reply With Quote
  #2 (permalink)  
Old 02-01-12, 10:44
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
You can find a solution using PHP. There are very functions that handle XML parsing returning an structure. Have a look at some sample code:

Code:
$ cat test.php
<?php
	$file = "test.xml";
	$xml = simplexml_load_file($file);
	foreach($xml->datafield as $key => $value) {
		echo "Tag: " . $value->attributes()->tag . "\n";
		foreach($value->subfield as $k => $v) {
			echo "subfield: " . $v . " code: " . $v->attributes()->code . "\n";
		}
	}
$ cat test.xml
<?xml version="1.0" encoding="UTF-8"?>
<record
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns="http://www.loc.gov/MARC21/slim">

<leader>00764nas </leader>
<controlfield tag="221">011258</controlfield>

<datafield tag="578" ind1=" " ind2=" ">
<subfield code="a">jaun</subfield>
</datafield>
<datafield tag="356" ind1=" " ind2=" ">
<subfield code="a"></subfield>
<subfield code="c"></subfield>
</datafield>
<datafield tag="333" ind1=" " ind2=" ">
<subfield code="d">ali</subfield>
<subfield code="p">22</subfield>
</datafield>
<datafield tag="222" ind1=" " ind2=" ">
<subfield code="c"></subfield>
<subfield code="d">1984</subfield>
</datafield>

</record>
$ php test.php 
Tag: 578
subfield: jaun code: a
Tag: 356
subfield:  code: a
subfield:  code: c
Tag: 333
subfield: ali code: d
subfield: 22 code: p
Tag: 222
subfield:  code: c
subfield: 1984 code: d
With this you can then extract the information you want either reformatting it or create your own insert statements within the PHP and execute.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On