{"id":17243,"date":"2015-09-19T05:01:07","date_gmt":"2015-09-18T19:01:07","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=17243"},"modified":"2015-09-18T17:25:50","modified_gmt":"2015-09-18T07:25:50","slug":"xml-to-html-php-simplexml-translation-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/xml-to-html-php-simplexml-translation-tutorial\/","title":{"rendered":"XML to HTML PHP Simplexml Translation Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"XML to HTML Data Intelligence Translation Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html-php.jpg\" title=\"XML to HTML PHP Simplexml Translation Tutorial\"  style=\"float:left;\" id='xxtho' onmouseover=\" this.src=this.src.replace('.jpg','.xpng').replace('.png','.xjpg').replace('.x','.');   \"  \/><\/a><p class=\"wp-caption-text\">XML to HTML PHP Simplexml Translation Tutorial<\/p><\/div>\n<p>Yesterday&#8217;s <a target=_blank title='HTML Data Intelligence More Like XML Primer Tutorial' href='#hdimlxpt'>HTML Data Intelligence More Like XML Primer Tutorial<\/a> as shown below, talked about the <a target=_blank title='HTML5 information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/HTML5'>HTML5<\/a> Global Attributes under the umbrella of <a target=_blank title='HTML data-* Attributes information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_data.asp'>HTML data-* Attribute<\/a> usage.<\/p>\n<p>You might remember from that &#8230;<\/p>\n<blockquote>\n<p>Along the way doing this we want to mention PHP&#8217;s simplexml methods you can read about at <a target=_blank href='http:\/\/www.w3schools.com\/php\/php_ref_simplexml.asp'>this link<\/a>, which are powerful if you know what the data structure involved is.  But today we are writing a home grown translator (or XML parser) to translate XML to HTML in a more &#8220;act dumb&#8221; generic sense, where you assume no knowledge of the data structure.  We acknowledge that it may fall over for some data, especially as the concept is in its infancy.<\/p>\n<\/blockquote>\n<p> &#8230; and that PHP <a target=_blank href='http:\/\/www.w3schools.com\/php\/php_ref_simplexml.asp'>simplexml<\/a> usage was best <i>&#8220;if you know what the data structure involved is&#8221;<\/i>.  But in that statement we&#8217;d neglected the <a target=_blank title='onion stories' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/?s=onion'>&#8220;onion of the 4th dimension&#8221;<\/a> wonders of PHP&#8217;s <a target=_blank title='PHP eval method information' href='http:\/\/php.net\/manual\/en\/function.eval.php'>eval<\/a> method, and so, today, we allow PHP simplexml methods to team up with PHP eval to preanalyze XML input data to <i>see<\/i> whether it can be parsed without knowing ahead of time what that XML data structure entails &#8230; cute, huh?!  Think, personally, PHP eval (and Javascript eval) are responsible for more &#8220;cute, huh?!&#8217;s&#8221; than any other bits of functionality.  Of course, you must factor in that a lot of people either think of eval as &#8230;<\/p>\n<ul>\n<li>a security risk &#8230; or a &#8230;<\/li>\n<li>kludge<\/li>\n<\/ul>\n<p> &#8230; but, personally, think it&#8217;s great &#8230; remember it back in the early days of Basic as well &#8230; just <a target=_blank title='BASIC language information from Wikipedia ... thanks ... but could not face the all uppercase shouting here' href='https:\/\/en.wikipedia.org\/wiki\/BASIC'>Basic<\/a> &#8230; not Visual Basic.<\/p>\n<p>Piecing together PHP eval often blows my mind, with the difficulty of the delimitation, but you may find all that a breeze &#8230; and have heard that they call the wind Maria &#8230; but who am I to question?!<\/p>\n<p>The bottom line to this is that the complication of yesterday&#8217;s (need to do) &#8230;<\/p>\n<blockquote>\n<ol>\n<li>small amounts of XML data using method=&#8217;GET&#8217; to xml_to_html.html<\/li>\n<li>large amounts of XML data using method=&#8217;POST&#8217; to xml_to_html.php (which clones xml_to_html.html to some user specific HTML that it calls)<\/li>\n<\/ol>\n<\/blockquote>\n<p> &#8230; is now a bit obsolete, because today&#8217;s changes also add to the PHP <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php-GETME\" title='xml_to_html.php'>xml_to_html.php<\/a> an HTML form to enable POST method callbacks, that will be okay for quite sizeable amounts of data.  As per usual, though, the flip side is that not everybody has Apache\/PHP hosting to be able to use this simplexml PHP functionality, and so a feature of the relationship between <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.html_GETME\" title='xml_to_html.html'>xml_to_html.html<\/a> and <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php-GETME\" title='xml_to_html.php'>xml_to_html.php<\/a> is that the HTML uses an iframe of an absolute URL for the PHP so that it can use <a target=_blank title='PHP $_SERVER[] server variables' href='http:\/\/php.net\/manual\/en\/reserved.variables.server.php'>$_SERVER[&#8216;HTTP_REFERER&#8217;]<\/a> as a means to redirect back to the parent HTML (perhaps on a non http:\/\/www.rjmprogramming.com.au domain (maybe not using Apache\/PHP hosting)) as necessary, or back to itself otherwise.<\/p>\n<p>The PHP code you can call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php-GETME\" title='xml_to_html.php'>xml_to_html.php<\/a> and it changed from yesterday <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/PHP\/Geographicals\/diff.php?one=http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php-GETME\" title='xml_to_html.php'>in this way<\/a>.<\/p>\n<p>Please feel free to try a PHP simplexml way of translating XML to HTML (and try to retain data intelligence) <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php\" title='Click picture'>here<\/a> or an HTML (with PHP iframe) home grown XML to HTML translation live run <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.html\" title='Click picture of yesterday'>here<\/a> (as of yesterday).<\/p>\n<hr>\n<p id='xthditt'>Previous relevant <a target=_blank title='XML to HTML Data Intelligence Translation Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/xml-to-html-data-intelligence-translation-tutorial\/'>XML to HTML Data Intelligence Translation Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"XML to HTML Data Intelligence Translation Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.jpg\" title=\"XML to HTML Data Intelligence Translation Tutorial\"  style=\"float:left;\" id='xtho' onmouseover=\" this.src=this.src.replace('.png','.xgif').replace('.gif','.xjpg').replace('.jpg','.xpng').replace('.x','.');   \"  \/><\/a><p class=\"wp-caption-text\">XML to HTML Data Intelligence Translation Tutorial<\/p><\/div>\n<p>Yesterday, with <a target=_blank title='HTML Data Intelligence More Like XML Primer Tutorial' href='#hdimlxpt'>HTML Data Intelligence More Like XML Primer Tutorial<\/a> as shown below, we talked about the <a target=_blank title='HTML5 information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/HTML5'>HTML5<\/a> Global Attributes under the umbrella of <a target=_blank title='HTML data-* Attributes information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_data.asp'>HTML data-* Attribute<\/a> usage.<\/p>\n<p>You&#8217;d be right in guessing that now we want to try some <a target=_blank title='JSON information from w3schools' href='http:\/\/www.w3schools.com\/xml\/default.asp'>XML<\/a> to HTML translations making use of this.<\/p>\n<p>Along the way doing this we want to mention PHP&#8217;s simplexml methods you can read about at <a target=_blank href='http:\/\/www.w3schools.com\/php\/php_ref_simplexml.asp'>this link<\/a>, which are powerful if you know what the data structure involved is.  But today we are writing a home grown translator (or XML parser) to translate XML to HTML in a more &#8220;act dumb&#8221; generic sense, where you assume no knowledge of the data structure.  We acknowledge that it may fall over for some data, especially as the concept is in its infancy.<\/p>\n<p>The HTML code you can call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.html_GETME\" title='xml_to_html.html'>xml_to_html.html<\/a> supervises some PHP you could call <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.php_GETME\" title='xml_to_html.php'>xml_to_html.php<\/a> (which has the dual purpose of demonstrating simplexml and accepting POSTed user defined data (to translate) from the HTML form of its parent.  The parent offers two forms for &#8230;<\/p>\n<ol>\n<li>small amounts of XML data using method=&#8217;GET&#8217; to xml_to_html.html<\/li>\n<li>large amounts of XML data using method=&#8217;POST&#8217; to xml_to_html.php (which clones xml_to_html.html to some user specific HTML that it calls)<\/li>\n<\/ol>\n<p>Our tutorial today thanks w3schools for some XML examples from <a target=_blank href='http:\/\/www.w3schools.com\/xml\/xml_attributes.asp' title='useful link'>this link<\/a> and <a target=_blank href='http:\/\/www.w3schools.com\/xml\/simple.xml' title='useful link again'>that link<\/a>.<\/p>\n<p>So please try a <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/xml_to_html.html\" title='click picture'>live run<\/a> and do some XML to HTML translations that try to retain &#8220;data intelligence&#8221;, in sympathy with the HTML DOM <a target=_blank title='HTML DOM getAttribute method information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_element_getattribute.asp'>getAttribute<\/a> method available in Javascript.<\/p>\n<hr>\n<p id='hdimlxpt'>Previous relevant <a target=_blank title='HTML Data Intelligence More Like XML Primer Tutorial' href='https:\/\/www.rjmprogramming.com.au\/ITblog\/html-data-intelligence-more-like-xml-primer-tutorial\/'>HTML Data Intelligence More Like XML Primer Tutorial<\/a> is shown below.<\/p>\n<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"HTML Data Intelligence More Like XML Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.jpg\" title=\"HTML Data Intelligence More Like XML Primer Tutorial\"  style=\"float:left;\"  \/><\/a><p class=\"wp-caption-text\">HTML Data Intelligence More Like XML Primer Tutorial<\/p><\/div>\n<p>Today we wanted to start you thinking on the subject of &#8220;data with intelligence&#8221;.  Maybe you think of HTML as representing &#8220;data with intelligence&#8221; because it brings you, via the Internet so much great information.  That is true, but generally speaking, it is carrying &#8220;display&#8221; information, if it has not been massaged through a server side language, nor through some Ajax &#8220;smarts&#8221; that will pick up &#8220;data intelligence&#8221;.  The bottom line is, generally speaking, it is not carrying the structured &#8220;intelligence&#8221; (the <a target=_blank title='Protocol information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/Protocol'>protocols<\/a>) <a target=_blank title='JSON information from w3schools' href='http:\/\/www.w3schools.com\/xml\/default.asp'>XML<\/a> and <a target=_blank title='XML information from w3schools' href='http:\/\/www.w3schools.com\/json\/default.asp'>JSON<\/a> (often) carry.<\/p>\n<p>However, HTML can be structured to be more like XML, and we delve into a bit of this by talking about a set of HTML Global Attributes that were added when <a target=_blank title='HTML5 information from Wikipedia ... thanks' href='https:\/\/en.wikipedia.org\/wiki\/HTML5'>HTML5<\/a> came out, which you can read more about with the link <a target=_blank title='HTML data-* Attributes information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_data.asp'>HTML data-* Attribute<\/a>.<\/p>\n<p>We have a dual purpose with today&#8217;s illustrative &#8230;<\/p>\n<ol>\n<li>HTML <a target=_blank title='sidtest.html' href='http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.html_GETME'>sidtest.html<\/a><\/li>\n<li>Javascript <a target=_blank title='sidtest.js' href='http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.js_GETME'>sidtest.js<\/a><\/li>\n<\/ol>\n<p> &#8230; which is &#8230;<\/p>\n<ol>\n<li>this &#8220;poking our toes into the water&#8221; approach to <a target=_blank title='HTML data-* Attributes information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_data.asp'>HTML data-* Attribute<\/a> HTML &#8220;data with intelligence&#8221; (as illustrated, in the code with all the HTML element <i>data-type=&#8221;[HTML-element-type]&#8221;<\/i> usages (<a target=_blank title='?' href='http:\/\/www.youtube.com\/watch?v=vVr6b9851Xk'>shaken, not stirred<\/a>) throughout, like (the header &lt;head&gt; &#8230; &lt;\/head&gt; section&#8217;s) &#8230;<br \/>\n<code><br \/>\n&lt;title <i>data-type=\"title\"<\/i> id='myhtitle' title='myhtitle title'&gt;HTML Header Id and Title Proof of Concept Ideas of Source Code Printout - RJM Programming - September, 2015&lt;\/title&gt;<br \/>\n<\/code><br \/>\n&#8230; and how, subsequently, you could use Javascript code like &#8230;<br \/>\n<code><br \/>\n var huh=document.getElementById('myhtitle');<br \/>\n alert('Your HTML tag ' + huh.<a target=_blank title='HTML DOM getAttribute method information from w3schools' href='http:\/\/www.w3schools.com\/jsref\/met_element_getattribute.asp'>getAttribute<\/a>('data-type') + ' element has content as per ' + huh.innerHTML);<br \/>\n<\/code><br \/>\n&#8230; to access this information) &#8230;<\/li>\n<li>getting used to the idea that all HTML elements, even those in the header &lt;head&gt; &#8230; &lt;\/head&gt; section (as well as the usual body &lt;body&gt; &#8230; &lt;\/body&gt; section) can be assigned id= (and that you can make use of these) &#8230; even inline Javascript &lt;script&gt; &#8230; &lt;\/script&gt; elements<\/li>\n<\/ol>\n<p>How are these related?  It&#8217;s all to do with using Javascript to make use of the HTML <i>slightly<\/i> less in terms of a carrier of &#8220;display&#8221; information, and <i>slightly<\/i> more as, possibly, also a carrier of &#8220;data intelligence&#8221;.  Even though there is, now with HTML5, the existence of <a target=_blank title='HTML data-* Attributes information from w3schools' href='http:\/\/www.w3schools.com\/tags\/att_global_data.asp'>HTML data-* Attribute<\/a> to help facilitate some of these thoughts for the use of some &#8220;data intelligence&#8221; with HTML, you should go easy on this, perhaps, as the most efficient way to proceed when there is lots of data, as XML was designed for &#8220;data intelligence&#8221; that is &#8220;big time&#8221; or dealing with large data sets, but you may have a modest application where you want to &#8220;carry&#8221; that small amount of database &#8220;data intelligence&#8221; with you in your HTML, and maybe &#8220;kill two birds with one stone&#8221;, those two birds being the &#8220;display bird&#8221; and the &#8220;data intelligence bird&#8221; &#8230; please note, <i>no magpies were hurt during the construction of this blog posting<\/i>.<\/p>\n<p>The functionality of today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.html\" title='click picture'>live run<\/a> is to &#8220;print out&#8221; (on a web browser) the coding of our <a target=_blank title='sidtest.html' href='http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/sidtest.html_GETME'>sidtest.html<\/a>, and so, by running it, you&#8217;ll see what we mean.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d17207' onclick='var dv=document.getElementById(\"d17207\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=xml\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d17207' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d17231' onclick='var dv=document.getElementById(\"d17231\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=html5\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d17231' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n<hr>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d17243' onclick='var dv=document.getElementById(\"d17243\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/?tag=php\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d17243' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;s HTML Data Intelligence More Like XML Primer Tutorial as shown below, talked about the HTML5 Global Attributes under the umbrella of HTML data-* Attribute usage. You might remember from that &#8230; Along the way doing this we want to &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/xml-to-html-php-simplexml-translation-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,12,37],"tags":[1688,263,1689,354,399,1690,576,1687,578,652,664,932,997,1691,1301,1319,1480],"class_list":["post-17243","post","type-post","status-publish","format-standard","hentry","category-database","category-elearning","category-tutorials","tag-attributes","tag-conversion","tag-data-intelligence","tag-dom","tag-eval","tag-getattribute","tag-html","tag-html-global-attributes","tag-html5","tag-javascript","tag-json","tag-php","tag-programming","tag-simplexml","tag-translation","tag-tutorial","tag-xml"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17243"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=17243"}],"version-history":[{"count":8,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17243\/revisions"}],"predecessor-version":[{"id":17251,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/17243\/revisions\/17251"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=17243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=17243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=17243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}