i am trying to upload an xml file using javascript. my javascript code is given below:
var xmlDoc =null;
var abc = new Array();
if (window.ActiveXObject){
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument){
xmlDoc=document.implementation.createDocument("","",null);
}
else{
alert('Browser cannot handle this script');
}
if (xmlDoc!=null){
xmlDoc.async=false;
xmlDoc.load("employee.xml");
var x = xmlDoc.getElementsByTagName("EMP");
for (i=0;i<x.length;i++)
{
abc[0] = x[0].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
document.write("a is "+abc[0]);
abc[1] = x[0].getElementsByTagName("ID1")[0].childNodes[0].nodeValue;
document.write("<br>b is "+abc[1]);
}
}
and my xml file is:
<EMPLOYEE>
<EMP>
<ID>10.99</ID>
<ID1>20.54</ID1>
</EMP>
</EMPLOYEE>
the code is working properly in IE as well as firefox but in Google Chrome, it is not showing anything. can some one tell me where i am wrong or how to correct it.
In order to get the XML file in Chrome you need to execute a GET. For security reasons you cannot load it in a straight forward manner from the file system. Here's code:
var xmlhttp = new window.XMLHttpRequest();
xmlhttp.open("GET", xmlsrc, false);
xmlhttp.send(null);
var xmlDoc = xmlhttp.responseXML.documentElement;
Related
I am struggling to perform an XSLT transform in the browser using just html and javascript (I have tried many different ways and none of them work). Here is what I want (and I just can't make it work).
1) I have an XSLT transform, I can make it available as a file (e.g. xslt), I can embed it in the HTML (I don't care), it is static.
2) I want the user to enter their XML into a text area.
3) I want the user to press a button and then I want the XSLT to occur and the result (which is html) to be displayed in a new tab.
Thanks for your help.
Finally got my own javascript to work. Here it is for anyone who needs it.
/**
* Load an XML document from server
*
* #param filename filename
*
* #return DOMDocument
*/
function loadXMLDoc(filename) {
if ((window.ActiveXObject) || (!(window.ActiveXObject) && "ActiveXObject" in window)) {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
else {
xhttp = new XMLHttpRequest();
}
xhttp.open("GET", filename, false);
try {
xhttp.responseType = "msxml-document"
}
catch (err) { } // Helping IE11
xhttp.send("");
return xhttp.responseXML;
}
/**
* Text XML template: perform XSLT on user XML
*
*/
function testXMLTemplate() {
// create xml string from user xml in text area
var xmlstr = document.getElementById("test-xml").value;
xmlstr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + xmlstr;
// load XSLT doc
var xsldoc = loadXMLDoc("xml/paramToHtml.xslt")
if ((window.ActiveXObject) || (!(window.ActiveXObject) && "ActiveXObject" in window)) {
// code for IE
var xmldoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
xmldoc.async = false;
xmldoc.loadXML(xmlstr);
ex = xmldoc.transformNode(xsldoc);
var xmlTestWin = window.open();
xmlTestWin.document.write(ex);
}
else if (document.implementation && document.implementation.createDocument) {
// code for Chrome, Firefox, Opera, etc.
var xmlparser = new DOMParser();
var xmldoc = xmlparser.parseFromString(xmlstr, "text/xml");
xsltProcessor = new XSLTProcessor();
xsltProcessor.importStylesheet(xsldoc);
resultDocument = xsltProcessor.transformToDocument(xmldoc);
var xmlTestWin = window.open();
xmlTestWin.document.write(resultDocument.firstChild.innerHTML);
}
}
Hello I am new to the community. I would like to ask a question.
I am trying to create a template HTML5 for loading and doing quizzes. I have the xml file with the question and the answers and I am trying to load it, in my template.
The code I use is this:
To load the xml file
// The Script that loads the XML File Locally only works in Firefox for now
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;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}
To Pass the Contents to my HTML5 template
xmlDoc=loadXMLDoc("test"+file+".qxml");
My problem is that the data from the xmlfile are not retrieved. While on the server or on any other browser the xmlDoc variable appears as null.
Can you point me in some direction as I am new to the Javascript xmlhttprequest methods. Thanks a lot in advance for your time.
The file extension is not xml (it is .qxml). The problem is the extension of the file .qxml. So it there any way to bypass this and use my extension qxml instead of xml ?
Try to override the mime type returned by the server, and tell your browser that the data is XML.
// The Script that loads the XML File Locally only works in Firefox for now
function loadXMLDoc(XMLname) {
var xmlDoc;
if (window.XMLHttpRequest) {
xmlDoc = new window.XMLHttpRequest();
xmlDoc.open("GET", XMLname, false);
xmlDoc.overrideMimeType('text/xml');
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;
}
else {
xmlhttp = new XMLHttpRequest();
//Open the file using the GET routine
xmlhttp.open("GET", XMLname, false);
xmlhttp.overrideMimeType('text/xml');
//Send request
xmlhttp.send(null);
//xmlDoc holds the document information now
return xmlDoc.responseXML;
}
alert("Error loading document!");
return null;
}
I have a xml file whose content is
<?xml version="1.0" encoding="UTF-8"?>
<ReturnMessage>
<root>ReturnMessage</root>
<cancelMessage>Request cancelled. /cancelMessage>
<confirmMessage>Click 'Create Document' to continue.</confirmMessage>
</ReturnMessage>
I load my xml like this
var result = responseText;
if (document.implementation && document.implementation.createDocument)
{
alert("firefox");
xml=document.implementation.createDocument("","",null);
xml.load(result);
}
When execute the below code
var cnfmMsgCnt = xml.getElementsByTagName("confirmMessage");
alert(cnfmMsgCnt.lenght);
it alerts a 0 is firefox.
var displayMsg = xml.getElementsByTagName("confirmMessage").item(0).text
also does not provide any output in FF.
It works perfect in IE but it is not working in Firefox.
The method load() takes a file name as argument, not an XML string. See https://developer.mozilla.org/en/DOM/document.load (and more normative: W3C DOM Level 3 Load & Save module)
What you probably want is explained here https://developer.mozilla.org/en/Parsing_and_serializing_XML
var sMyString = "<a id=\"a\"><b id=\"b\">hey!<\/b><\/a>";
var oParser = new DOMParser();
var oDOM = oParser.parseFromString(sMyString, "text/xml");
I am trying to call a php file using GetXmlHttpObject object and having some luck, but seem to have problem with the URL variable.
Is there something I have to do with the URL string differently?
Here is the relevant code:
remoteCounter('marks');//invoking function
document.write("<div id=\"marks\"></div>");//destination div
function GetXmlHttpObject () {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari, IE 7+
xmlHttp = new XMLHttpRequest();
} catch (e) {
// Internet Explorer - old IE - prior to version 7
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function remoteCounter(param){
counterObj = GetXmlHttpObject();
var url="HTTP://dnsname/directory/view.php?" + param;
alert(url + " " + counterObj);
counterObj.open("GET", url , true);
counterObj.onreadystatechange=updateCounter;
counterObj.send(null);
}
function updateCounter(){
//alert('updateCounter');
if (counterObj.readyState == 4 || counterObj.readyState == "complete"){
document.getElementById("marks").innerHTML=counterObj.responseText;
}
}
I can replace the counterObj.responseText variable in document.getElementById("marks").innerHTML=counterObj.responseText;
and see the test string correctly in the source document, so I know the html and jscript source is not the problem.
I have commented out code in the view.php file to just echo a simple string, but that is not showing either - which again makes me think the problem is in the request to the file, not in the file, not in the source.
Actual server name and directory have been replaced by dnsname/directory for this post. Including alert for debugging.
thank you for assistance.
Replace order of two first lines:
document.write("<div id=\"marks\"></div>"); //destination div
remoteCounter('marks'); //invoking function
It's necesary declarate div tag with id "marks" first.
I have html file with javascript code the contents of the file are as under:
I tried this code on Safari and it was working fine. But when I tried this on Firefox, it’s not working. Can any one suggest how to make it working on firefox.
<html><head></head><body><button type="button" onClick="handleButtonClick();">undo</button> <button type="button">redo</button><select><option value="V1">V1</option><option value="V2">V2</option><option value="V3">V3</option><option value="V4">V4</option><option value="V5">V5</option></select> <script type="text/javascript">function handleButtonClick(){var xmlHttp, handleRequestStateChange; handleRequestStateChange = function() {if (xmlHttp.readyState==4 && xmlHttp.status==200) { var substring=xmlHttp.responseText; alert(substring); } }
xmlHttp = new XMLHttpRequest();xmlHttp.open("GET", "http://csce.unl.edu:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77540ceb", true);xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null);}</script>
</body>
</html>
if you copy the above code to your system on clicking on undo button u will get the contents
but the same code I am inserting through C++ as a string it is not working... what i figured out strange about the above code what that it required enter (return key) to be pressed before xmlHttp = new XMLHttpRequest(); othervise it was not working for html page also. The same thing I implemented in C++ code and the code is not working. The code is as under:
std::string login="<button type=\"button\" onClick=\"handleButtonClick();\">undo</button> <button type=\"button\">redo</button>< select><option value=\"V1\">V1</option><option value=\"V2\">V2</option><option value=\"V3\">V3</option><option value=\"V4\">V4</option><option value=\"V 5\">V5</option></select> ";
std::string jscriptstring1="<script type=\"text/javascript\">function handleButtonClick(){var xmlHttp, handleRequestStateChange; handleRequestStateChang e = function() {if (xmlHttp.readyState==4 && xmlHttp.status==200) { var substring=xmlHttp.responseText; alert(substring); } }";
std::string jscriptstring2="xmlHttp = new XMLHttpRequest();xmlHttp.open(\"GET\", \"http://csce.unl.edu:8080/test/index.jsp?id=c6c684d9cc99476a7e7e853d77 540ceb\", true);xmlHttp.onreadystatechange = handleRequestStateChange; xmlHttp.send(null);}</script>";
std::string ret="\n";
login+=jscriptstring1;
login+=ret;
login+=jscriptstring2;
Please can any one suggest what is going wrong?
First of all if you are going to create an XMLHttp object for requesting the server in different browsers be sure it is valid in the browser you are using (different browser have different manner to refer the XMLHttp object), normally this object is created as follow:
function getRequestObject(){
var xmlHttp;
if (window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
else if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); }
return xmlHttp;
}
In the other hand, depending on your browser you can debug your script to see what error code is browser returning (if any).