<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Open Coder &#187; Databases</title>
	<atom:link href="http://www.opencoder.co.uk/category/databases/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.opencoder.co.uk</link>
	<description>Helping the fellow geek</description>
	<lastBuildDate>Fri, 15 Apr 2011 12:25:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Regular expressions in mysql</title>
		<link>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/</link>
		<comments>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 21:38:00 +0000</pubDate>
		<dc:creator>Chris McDonald</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Servers]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[openfire]]></category>
		<category><![CDATA[regular expressions]]></category>

		<guid isPermaLink="false">http://www.opencoder.co.uk/?p=73</guid>
		<description><![CDATA[It has been a very long time since I posted anything so I thought I would share this small snippet. Recently I had to extract some data which was stored as xml in a field within a mysql database table. To explain briefly, this data was actually saved as part of an XMPP (jabber) chat [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a very long time since I posted anything so I thought I would share this small snippet.</p>
<p>Recently I had to extract some data which was stored as xml in a field within a mysql database table. To explain briefly, this data was actually saved as part of an XMPP (jabber) chat message by an openfire server. <a title="Openfire real time collaboration server" href="http://www.igniterealtime.org/projects/openfire/" target="_blank">Openfire</a> is a real time collaboration server which includes jabber functionality. To store the chat history Openfire records the chat message as an xml chunk in a field named <em>body</em> in a particular database table <em>ofMucConversationLog</em>. I needed to extract certain data that our system had communicated as a chat message through openfire, to do this I used a combination of regular expressions and string manipulation.</p>
<p>Ok, lets say I have messages that are either in format A or B as below:</p>
<pre>&lt;messageformata&gt;
  &lt;child1&gt;some text&lt;/child1&gt;
  &lt;child2&gt;some more text&lt;/child2&gt;
&lt;/messageformata&gt;

&lt;messageformatb&gt;
  &lt;child1&gt;some text&lt;/child1&gt;
  &lt;child2&gt;some more text&lt;/child2&gt;
&lt;/messageformatb&gt;</pre>
<p>Each row in the database can contain xml in the format of A or B and there are many rows. If I need to extract only the contents of the first child of message format a, I could use the following SQL code to do it (assuming I&#8217;ve created a temporary table called <em>temptable</em> to store these values):</p>
<div class="codesnip-container" >
<div class="sql codesnip" style="font-family:monospace;"><span class="kw1">INSERT</span> <span class="kw1">INTO</span> temptable <span class="br0">&#40;</span>child1value<span class="br0">&#41;</span><br />
<span class="kw1">SELECT</span><br />
SUBSTRING<span class="br0">&#40;</span>body<span class="sy0">,</span> LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span><span class="sy0">+</span><span class="nu0">8</span><span class="sy0">,</span> <br />
&nbsp; LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;/child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span> <span class="sy0">-</span> LOCATE<span class="br0">&#40;</span><span class="st0">&#8216;&lt;child1&gt;&#8217;</span><span class="sy0">,</span> body<span class="br0">&#41;</span> <span class="sy0">-</span> 8<span class="br0">&#41;</span><br />
<span class="kw1">FROM</span> ofMucConversationLog <span class="kw1">WHERE</span> body <span class="kw1">REGEXP</span> <span class="st0">&#8216;&lt;messageformata&gt;*&#8217;</span>;</div>
</div>
<p>Unfortunately the REGEXP operator can only be used for testing true or false and so is probably only useful in a WHERE clause. There are no capturing options like in other languages (php, perl java etc.), however you can use it in combination with the substring and locate functions to get the job done, although it is admittedly tedious.</messageformata></child1></p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencoder.co.uk/2009/10/regular-expressions-in-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

