{"id":1395,"date":"2018-01-19T19:22:47","date_gmt":"2018-01-19T19:22:47","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/dynamic-xml-conversion-using-the-sax-parser-page-3\/"},"modified":"2018-01-19T19:24:45","modified_gmt":"2018-01-19T19:24:45","slug":"dynamic-xml-conversion-using-the-sax-parser-page-3","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/dynamic-xml-conversion-using-the-sax-parser-page-3\/","title":{"rendered":"Dynamic XML Conversion Using the SAX Parser Page 3"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By PHP Builder Staff<\/div>\n<div class=\"\">on April 28, 2003<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<h2>Creating the conversion functions<\/h2>\n<div class=\"articlePara\">\nEach conversion function has two arguments: <\/div>\n<div class=\"articleList\">\n<ol>\n<li><code class=\"example\">$contents<\/code>: The character data between the opening and closing tag.<br \/>\nThis can include text, HTML tags and the output of conversion functions for tags nested between the<br \/>\ncurrently processed. The function should include this in its return value.<\/li>\n<li><code class=\"example\">$attrs<\/code>: An array containing the attributes of the tag. Note that the EXPAT parser<br \/>\nautomatically changes the attribute names to uppercase.<\/li>\n<\/ol>\n<\/div>\n<div class=\"articlePara\">So the function to handle this tag: &lt;font face=&#8221;bold&#8221;&gt;blah&lt;\/font&gt;<br \/>\nreceives these attributes:<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/>$contents<\/span><span style=\"color: #007700\">=<\/span><span style=\"color: #DD0000\">'blah'<\/span><span style=\"color: #007700\">;<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">$attrs<\/span><span style=\"color: #007700\">=array(<\/span><span style=\"color: #DD0000\">'FACE'<\/span><span style=\"color: #007700\">=&gt;<\/span><span style=\"color: #DD0000\">'bold'<\/span><span style=\"color: #007700\">);\u00a0<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">Here is an example conversion function: <\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/><\/span><span style=\"color: #007700\">function\u00a0<\/span><span style=\"color: #0000BB\">handle_bigheadline<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$contents<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$attrs<\/span><span style=\"color: #007700\">)\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0return(<\/span><span style=\"color: #DD0000\">'&lt;b&gt;&lt;font\u00a0size=\"5\"\u00a0face=\"Verdana\"&gt;'<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #0000BB\">$contents<\/span><span style=\"color: #007700\">.<\/span><span style=\"color: #DD0000\">'&lt;\/font&gt;&lt;\/b&gt;'<\/span><span style=\"color: #007700\">);<br \/>\n<br \/>}<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nThe function wraps the contents it receives into some HTML to make it look like a headline.<br \/>\nIt then returns this modified content to the conversion framework, which uses it as part of the<br \/>\ncontents parameter for the function that converts the tag surrounding it.\n<\/div>\n<div class=\"articlePara\">\nHere is the function to convert the &lt;dayofweek \/&gt; &#8211; tag:\n<\/div>\n<div class=\"articlePhpEx\">\n<font face=\"courier\"><code><span style=\"color: #000000\"><\/p>\n<p><span style=\"color: #0000BB\">&lt;?php<br \/>\n<br \/><\/span><span style=\"color: #007700\">function\u00a0<\/span><span style=\"color: #0000BB\">handle_dayofweek<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #0000BB\">$contents<\/span><span style=\"color: #007700\">,<\/span><span style=\"color: #0000BB\">$attrs<\/span><span style=\"color: #007700\">)\u00a0{<br \/>\n<br \/>\u00a0\u00a0\u00a0\u00a0return(<\/span><span style=\"color: #0000BB\">date<\/span><span style=\"color: #007700\">(<\/span><span style=\"color: #DD0000\">\"l\"<\/span><span style=\"color: #007700\">));<br \/>\n<br \/>}<br \/>\n<br \/><\/span><span style=\"color: #0000BB\">?&gt;<br \/>\n<br \/><\/span><br \/>\n<\/span><br \/>\n<\/code><\/font><\/div>\n<div class=\"articlePara\">\nThe function ignores the contents and attributes which are passed and just returns the name of the current<br \/>\nweekday. So if you put some stuff between <dayoweek>-Tags, it would never show up in the converted HTML.<br \/>\n<\/dayoweek><\/div>\n<div class=\"articleHeader\">\nBuilding the Conversion Framework<\/div>\n<div class=\"articlePara\">\nAs mentioned before, we will use the SAX parser to build the conversion framework.<br \/>\nSAX is an event based parser. It works like this:<br \/>\nIt chops up the document into elements. There are three element types: Start tags, end tags and character data. (Actually there are some more, but we won&#8217;t need them in this example.)<br \/>\nThen for each element it encounters in the document, the parser calls a function assigned to the element type.\n<\/div>\n<div class=\"articlePara\">\nThe element handling functions receive these parameters:<\/div>\n<div class=\"articlePara\">\n<table cellpadding=\"5\" border=\"1\" cellspacing=\"0\">\n<tr>\n<th>\nFunction<\/th>\n<th>Parameters<\/th>\n<\/tr>\n<tr>\n<td>Opening Tag<\/td>\n<td>Name of the tag, array containing its attributes<\/td>\n<\/tr>\n<tr>\n<td>Character data\t<\/td>\n<td>A string containing the characters<\/td>\n<\/tr>\n<tr>\n<td>Closing Tag<\/td>\n<td>Name of the tag<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<div class=\"articlePara\">\nWe want to combine these three functions, so that we can use the attribute data to handle the contents data.<br \/>\nOur conversion functions will be called when the parser encounters a closing tag.\n<\/div>\n<div class=\"articleList\">\n<ol>\n<li>An array containing the attributes of the tag (the SAX parser passes this to the opening tag function) <\/li>\n<li>\nThe character data between the opening and closing tag (can contain the output of conversion functions<br \/>\nfor other tags nested between these tags)\n<\/li>\n<\/ol>\n<\/div>\n<div class=\"articlePara\">\nWe will have to think up a way to store these values when we receive them so that we have them at hand when the<br \/>\nparser encounters the closing tag.\n<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"scheffler200303254658.html?page=2\">\u00ab Previous Page<\/a><\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler20030325.html\">1<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler200303254658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">3<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler20030325fdb0.html?page=4\">4<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler20030325af4d.html?page=5\">5<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler20030325c575.html?page=6\">6<\/a> <\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"scheffler20030325235c.html?page=7\">7<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"scheffler20030325fdb0.html?page=4\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>This article describes an alternative way of converting XML to HTML using the SAX parser. For each tag you<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1395","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1395"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1395\/revisions"}],"predecessor-version":[{"id":3265,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1395\/revisions\/3265"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}