| | |
How to display XML in a browser?
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
I would like to know how to display XML in a browser.
Also if possible to maniuplate the XML elements.
Havent touched based with XML in awhile and still abit rusty.
Thanks, Regards X
PS: Below is the listed code and below is the wished output.
Code :
Output :
Also if possible to maniuplate the XML elements.
Havent touched based with XML in awhile and still abit rusty.
Thanks, Regards X
PS: Below is the listed code and below is the wished output.
Code :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xsl:output method="html"> <url> <loc>http://www.google.com/</loc> <lastmod>2009-07-03T19:00:55+00:00</lastmod> <changefreq>monthly</changefreq> <priority>1.00</priority> </url>
Output :
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<a href="http://www.google.com/">Google</a>
"You never stop learning." - OmniX
It's either you can use plain stylesheet(css), XSLT(xsl) or Ajax/XMLDOM Parser's using javascript.
But here's a bit of example, formattimg its layout using XSLT, also try the XML section to get precise results over this issue.
From your XML file, you must add a single of this:
code for the test.xsl stylesheet:
The .xsl sample document will only work in IE6+/OPERA9+/FF2+
hope it helps...
But here's a bit of example, formattimg its layout using XSLT, also try the XML section to get precise results over this issue.
From your XML file, you must add a single of this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="./test.xsl"?>
...code for the test.xsl stylesheet:
xml Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40"> <xsl:output method="html"/> <xsl:template match="/"> <html> <head> <title>Displaying XML Document to a browser</title> </head> <body> <h1>XML-XSL Generated Content</h1> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="url"> <p><xsl:apply-templates/></p> </xsl:template> <xsl:template match="url/loc"> <a style="text-decoration:none;color:red;"> <xsl:attribute name="href"> <xsl:apply-templates/> </xsl:attribute> <xsl:apply-templates/> </a><br/> </xsl:template> </xsl:stylesheet>
The .xsl sample document will only work in IE6+/OPERA9+/FF2+
hope it helps...
I tired your document as a .html and a .xsl .
The html appeared in html and the xsl appeared in an xml format but nothing useful.
Any ideas? Thanks.
The html appeared in html and the xsl appeared in an xml format but nothing useful.
Any ideas? Thanks.
"You never stop learning." - OmniX
The only document that you need to open is your posted XML file, which you must referenced into my posted XSL file.
Converting the XSL document into html format, will get displayed as a normal tags in your browser or opening it directly will get parsed as a normal XML document.
Doing this with AJAX is more reliable than having it formatted as XML-based content and will take alot of work.
Converting the XSL document into html format, will get displayed as a normal tags in your browser or opening it directly will get parsed as a normal XML document.
Doing this with AJAX is more reliable than having it formatted as XML-based content and will take alot of work.
Here's a little demonstration of displaying XML content into a browser.
Saying that we have this declaration along with our XML doc's.
url.xml:
Saying that we have this declaration along with our XML doc's.
url.xml:
xml Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8" standalone='yes'?> <!DOCTYPE urlset [ <!ELEMENT urlset ( url* )> <!ELEMENT url ( loc, lastmod, changefreq, priority )> <!ATTLIST url id ID #IMPLIED title CDATA #IMPLIED xml:lang CDATA "en"> <!ELEMENT loc (#PCDATA)> <!ELEMENT changefreq ( schedule )> <!ATTLIST changefreq update CDATA #REQUIRED> <!ELEMENT schedule ( sunday, monday, tuesday, wednesday, thursday, friday, saturday )> <!ELEMENT sunday (#PCDATA)> <!ELEMENT monday (#PCDATA)> <!ELEMENT tuesday (#PCDATA)> <!ELEMENT wednesday (#PCDATA)> <!ELEMENT thursday (#PCDATA)> <!ELEMENT friday (#PCDATA)> <!ELEMENT saturday (#PCDATA)> <!ELEMENT lastmod (#PCDATA)> <!ELEMENT priority EMPTY> <!ATTLIST priority set CDATA #REQUIRED> ]> <urlset> <url xmlns:urlset="http://www.yoursite.org/2009/urlset" xml:lang="en"> <loc>http://www.google.com/</loc> <lastmod>2009-07-21T16:34:19+08:00</lastmod> <changefreq update="weekly"> <!-- I'll use it as a weekly basis to make it short. - you can add new elements for the monthly tags --> <schedule> <sunday>Sunday-Group</sunday> <monday>Monday-Group</monday> <tuesday>Tuesday-Group</tuesday> <wednesday>Wednesday-Group</wednesday> <thursday>Thursday-Group</thursday> <friday>Friday-Group</friday> <saturday>Saturday-Group</saturday> </schedule> </changefreq> <priority set="1.00" /> </url> </urlset>
And here's the start page, along with the script that will generate our XML content:
main.html:
hope it does what you need...
essential
main.html:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Iframe Page</title> <meta name="author" content="DANI-USER : essential" /> <style type="text/css"> /* <![CDATA[ */ td { text-align : center; } /* ]]> */ </style> <script type="text/javascript"> // <![CDATA[ var check = ( Boolean((( XMLHttpRequest ) ? 1 : 0 ))); var handleMyRequest = function() { /******************************* AJAX POWERED ******************************** - I've keep all of the basic method with innerHTML. - So that you can easily go through from each line's - Just concentrate on this function and forget about everything that is outside of this block. http://www.daniweb.com/ *******************************/ var seq = { 4 : "complete", complete : 4 }; if ( seq[ this.readyState ] ) { var xDoc = this.responseXML; var xNode = xDoc.getElementsByTagName( "*" ); var xRoot = xDoc.getElementsByTagName( xDoc.documentElement.nodeName ); var xChild = ( function( childX, rootX ) { if ( arguments.length < 2 ) { return xRoot[0].getElementsByTagName( childX ); } else { return rootX.getElementsByTagName( childX ); } } ); var xDate = new Date(); var xDiv = document.getElementById("main"); // You can start editing below this line \\ var xTable = ""; for ( var x = 0; x < xRoot.length; x++ ) { xTable += '\n<table frame="box" width="100%" rules="all" cellpadding="5" cellspacing="3" border="1">\n'; xTable += "<thead>\n"; for ( y = 0; y < xChild( "url" ).length; y++ ) { xTable += "<tr>\n"; xTable += "<th>Location</td>\n"; xTable += "<th>Last Modified</th>\n"; xTable += "<th>Schedule</th>\n"; xTable += "<th>Priority</th>\n"; xTable += "</tr>\n"; xTable += "</thead>\n"; xTable += "<tbody>\n"; xTable += "<tr>\n"; xTable += "<td>" + String( xChild( "loc" )[ y ].childNodes[0].nodeValue ).link( xChild( "loc" )[ y ].childNodes[0].nodeValue ) + "</td>"; xTable += "<td>" + xChild( "lastmod" )[ y ].childNodes[0].nodeValue + "</td>"; xTable += "<td>" + xChild( [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday" ][ xDate.getDay() ] )[ y ].childNodes[ 0 ].nodeValue + "</td>"; // Weekday scheduling. xTable += "<td>" + xChild( "priority" )[ y ].getAttribute( "set" ) + "</td>\n"; } xTable += "</tr>\n"; } xTable += "</tbody>\n"; xTable += "</table>\n"; xDiv.innerHTML = xTable; } }; /* WARNING! Please do not edit anything beyond this block */ var XHR = function( method, url, callback ) { this.createRequest = null; this.xml = 0; this.method = method || "GET"; this.url = url || null; this.callback = callback || null; if ( arguments.length < 3 ) { return false; } this.xmlDoc = ( function() { try { if ( check ) { this.xml = new XMLHttpRequest(); } else { try { var client = [ "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP" ]; for ( var i = 0; i < client.length; ++i ) { if ( new ActiveXObject( client[ i ] )) { this.xml = new ActiveXObject( client[ i ] ); break; } } } catch( e ) { this.xml = 0; } } } catch( er ) { if ( "createRequest" in window ) this.xml = createRequest(); else this.xml = 0; } return (( this.xml ) ? this.xml : 0 ); } )(); if ( this.xmlDoc ) { this.createRequest = ( function( xhr, callback, url, method ) { (( "overrideMimeType" in xhr ) ? xhr.overrideMimeType("text/xml") : xhr ); xhr.onreadystatechange = callback; xhr.open( method, encodeURI( url ), true ); (( method === "POST" && "setRequestHeader" in xhr ) ? xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8;") : xhr ); xhr.send((( method === "POST" ) ? " " : null )); } )( this.xmlDoc, this.callback, this.url, this.method ); } return this.createRequest; }; onload = function() { new XHR( "GET", "url.xml", handleMyRequest ); // You can create new instance of request by calling out, the same procedure above. } // ECMAScript v1.5 ]]> </script> </head> <body> <div id="main"></div> </body> </html>
hope it does what you need...
essential
![]() |
Similar Threads
- how to display XML file in textBox or Treeview using C# (C#)
- Display xml data in message box (VB.NET)
- Display Xml In Jsp (JSP)
- HTML display but xml content (HTML and CSS)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Progress Bar
- Next Thread: Form submission with Iframe
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp animate api automatically beta boarder box bug calendar card checkbox child class column cookies createrange() css cursor dependent disablefirebug dom download dropdown editor element engine error events explorer ext file firehose flash form forms game google gwt html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump math matrixcaptcha microsoft mimic mp3 mysql object offline onmouseoutdivproblem onreadystatechange parent passing pdf php player post problem progressbar rated rating regex runtime scroll search select session shopping size sql star stars stretch text textarea twitter validation w3c web website window windowofwords windowsxp wysiwyg xml xspf \n





