homepage Welcome to WebmasterWorld Guest from 54.234.128.25
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Importing XML to the body of a html/xhtml website
onlinewebsolutions




msg:4393946
 1:18 am on Dec 4, 2011 (gmt 0)

I'd like to have the answer to this also. I've already searched and came across a bunch of old tutorials that don't seem to work anymore.

What I'm looking for should be fairly simple.

Lets say I have an HTML website (index.html)and I have an xml document marked up with xsl on the same server in the same directory.

***Now this is where I need help. I can't believe this has been so hard to figure out!***

On the html (lets say somewhere in the middle after some text and some misc scripts I want to import the marked up xml document. How would I do that. I came across some javascripts that said it would import, but when I tried the xml just did not load.

 

Dijkgraaf




msg:4394208
 12:55 am on Dec 5, 2011 (gmt 0)

Which sample are you using?
This one here? [java2s.com...]

I copied the HTML into a file on my PC, copied the XML (without the comment tags around it) into worldCupFinals.xml, and after enabling JavaScript it worked fine.

What errors are you seeing?

onlinewebsolutions




msg:4394225
 2:43 am on Dec 5, 2011 (gmt 0)

huh, that works perfect! I was searching for hours yesterday. I bookmarked that site as I'm sure it will come in useful.


I found a few, but non seemed to work except for one php script. I think I could have used it but I would've had to change the extensions of my site from .html to .php and from my understanding I might have lost some backlinks after doing that.

Here are a few of the scripts I tried that I couldn't get to work: Thanks for your help!

Script 1

<html><head><title>index</title>
</head>
<body>



<script type="text/javascript">
javascript:function%20loadXML()
{
var dbug = 1;
var xmlfile = "test.xml";
var xmlDoc;
function import_xml(file)
{
if (dbug) alert('ImportXML');
var xmlDoc; var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');
if (moz)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = parse_xml;
}
else if (ie)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
}
if (dbug) alert('Load');
xmlDoc.load(file);
if (dbug) alert('Loaded');
}
function parse_xml()
{
if (dbug) alert('parseXML');
}
if (dbug) alert('loadXML');
import_xml(xmlfile);
}loadXML();void(null)

</script>

</body>
</html>






Script 2


<html><head><title>index</title>
</head>
<body>



<script type="text/javascript">
function importXML()
{
if (document.implementation && document.implementation.createDocument)
{
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.onload = createTable;
}
else if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.onreadystatechange = function () {
if (xmlDoc.readyState == 4) createTable()
};
}
else
{
alert('Your browser can\'t handle this script');
return;
}
xmlDoc.load("test.xml");
}

function createTable()
{
var x = xmlDoc.getElementsByTagName('catalog');
var newEl = document.createElement('TABLE');
newEl.setAttribute('cellPadding',5);
var tmp = document.createElement('TBODY');
newEl.appendChild(tmp);
var row = document.createElement('TR');
for (j=0;j<x[0].childNodes.length;j++)
{
if (x[0].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TH');
var theData = document.createTextNode(x[0].childNodes[j].nodeName);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
for (i=0;i<x.length;i++)
{
var row = document.createElement('TR');
for (j=0;j<x[i].childNodes.length;j++)
{
if (x[i].childNodes[j].nodeType != 1) continue;
var container = document.createElement('TD');
var theData = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue);
container.appendChild(theData);
row.appendChild(container);
}
tmp.appendChild(row);
}
document.getElementById('writeroot').appendChild(newEl);
}


</body>
</html>



Script 3

<html>
<head>
<title>Importing and Manipulating XML</title>
<script language="javascript">

/*************************************
DECLARE GLOBAL VARIABLES
*************************************/
var c
var d='';
var e='';
var f='';
var g='';
var xml_doc

/*************************************
MOZILLA - EXTRACT NODE INFORMATION
*************************************/
function readFileMoz()
{
node_list = xml_doc.getElementsByTagName("catalog")
for (i = 0; i <= node_list.length-1; i++)
{
c = node_list[i].childNodes[1].childNodes[0].nodeValue
d = node_list[i].childNodes[3].childNodes[0].nodeValue
//e = node_list[i].childNodes[5].nodeValue
//f = node_list[i].childNodes[7].nodeValue
//g = node_list[i].childNodes[9].nodeValue
alert("C = " + c + " D = " + d)
}
}


/*************************************
INTERNET EXPLORER - EXTRACT NODE INFORMATION
*************************************/
function readFileIE()
{
node_list = xml_doc.getElementsByTagName("catalog")

for (i = 0; i <= node_list.length-1; i++)
{
c = node_list[i].childNodes[0].childNodes[0].nodeValue
d = node_list[i].childNodes[1].childNodes[0].nodeValue
//e = node_list[i].childNodes[2].nodeValue
//f = node_list[i].childNodes[3].nodeValue
//g = node_list[i].childNodes[4].nodeValue
alert("C = " + c + " D = " + d)
}

}


/*************************************
This function loads the file.
*************************************/
function importXML(zFileName)
{
var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined')

if (moz)
{
xml_doc = document.implementation.createDocument("", "", null)
xml_doc.onload = readFileMoz
}

else if (ie)
{
xml_doc = new ActiveXObject("Microsoft.XMLDOM")
xml_doc.onreadystatechange = function()
{
if (xml_doc.readyState == 4) setTimeout(readFileIE,0)
}
}

xml_doc.load(zFileName)

}

importXML('test.xml');

</script>


</head>
<body>
</body>
</html>





Here are the XML and XSL sheets respectively. I had called them test.xml and test2.xsl


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test2.xsl"?>


<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
<cd>
<title>Still got the blues</title>
<artist>Gary Moore</artist>
<country>UK</country>
<company>Virgin records</company>
<price>10.20</price>
<year>1990</year>
</cd>
<cd>
<title>Eros</title>
<artist>Eros Ramazzotti</artist>
<country>EU</country>
<company>BMG</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>One night only</title>
<artist>Bee Gees</artist>
<country>UK</country>
<company>Polydor</company>
<price>10.90</price>
<year>1998</year>
</cd>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>Maggie May</title>
<artist>Rod Stewart</artist>
<country>UK</country>
<company>Pickwick</company>
<price>8.50</price>
<year>1990</year>
</cd>
<cd>
<title>Romanza</title>
<artist>Andrea Bocelli</artist>
<country>EU</country>
<company>Polydor</company>
<price>10.80</price>
<year>1996</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Black angel</title>
<artist>Savage Rose</artist>
<country>EU</country>
<company>Mega</company>
<price>10.90</price>
<year>1995</year>
</cd>
<cd>
<title>1999 Grammy Nominees</title>
<artist>Many</artist>
<country>USA</country>
<company>Grammy</company>
<price>10.20</price>
<year>1999</year>
</cd>
<cd>
<title>For the good times</title>
<artist>Kenny Rogers</artist>
<country>UK</country>
<company>Mucik Master</company>
<price>8.70</price>
<year>1995</year>
</cd>
<cd>
<title>Big Willie style</title>
<artist>Will Smith</artist>
<country>USA</country>
<company>Columbia</company>
<price>9.90</price>
<year>1997</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
<cd>
<title>Stop</title>
<artist>Sam Brown</artist>
<country>UK</country>
<company>A and M</company>
<price>8.90</price>
<year>1988</year>
</cd>
<cd>
<title>Bridge of Spies</title>
<artist>T`Pau</artist>
<country>UK</country>
<company>Siren</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Private Dancer</title>
<artist>Tina Turner</artist>
<country>UK</country>
<company>Capitol</company>
<price>8.90</price>
<year>1983</year>
</cd>
<cd>
<title>Midt om natten</title>
<artist>Kim Larsen</artist>
<country>EU</country>
<company>Medley</company>
<price>7.80</price>
<year>1983</year>
</cd>
<cd>
<title>Pavarotti Gala Concert</title>
<artist>Luciano Pavarotti</artist>
<country>UK</country>
<company>DECCA</company>
<price>9.90</price>
<year>1991</year>
</cd>
<cd>
<title>The dock of the bay</title>
<artist>Otis Redding</artist>
<country>USA</country>
<company>Atlantic</company>
<price>7.90</price>
<year>1987</year>
</cd>
<cd>
<title>Picture book</title>
<artist>Simply Red</artist>
<country>EU</country>
<company>Elektra</company>
<price>7.20</price>
<year>1985</year>
</cd>
<cd>
<title>Red</title>
<artist>The Communards</artist>
<country>UK</country>
<company>London</company>
<price>7.80</price>
<year>1987</year>
</cd>
<cd>
<title>Unchain my heart</title>
<artist>Joe Cocker</artist>
<country>USA</country>
<company>EMI</company>
<price>8.20</price>
<year>1987</year>
</cd>
</catalog>



xsl

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>

</xsl:stylesheet>




There were a few more, but I don't have them anymore.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved