Solved

Javascript to create XML files

Posted on 2001-09-04
5
414 Views
Last Modified: 2013-11-19
Can anyone please tell me how to go about creating XML files using Javascript?
Is this possible?
0
Comment
Question by:desmondtsk
5 Comments
 
LVL 3

Expert Comment

by:Patterson
ID: 6453785
You can do something like this:

<script language="JavaScript">

xmlObj = new ActiveXObject("Microsoft.XMLDOM");
xmlObj.async = false;
xslObj = new ActiveXObject("Microsoft.XMLDOM");
xslObj.async = false;
xslObj.load("somexslfile.xsl");


function makeXML() {
  var str = "";
  str = "<?xml version='1.0'?><mainnode>";
  for (var i = 0; i < 10; i++) {
    str += "<item>" + i + "</item>";
  }
  str+= "</mainnode>";
  xmlObj.loadXML(str);
  somediv.innerHTML = xmlObj.transformNode(xslObj);
}

</script>

<body>
<div id="somediv"></div>

</body>

You can generate any type of xml document you want using a variable and using the loadXML method you can transform it using a xsl stylesheet.

you can also do xmlObj.load("somepage.asp") or xmlObj.load("somefile.xml") as well.

Hope that helps,

Patterson
0
 
LVL 1

Expert Comment

by:iamjhkang
ID: 7015403
<html>
<head>
<script language="JavaScript"/>
function makeDOM(){
//DOM object
 var domDoc = new ActiveXObject("Msxml.DOMDocument");
     
//PI
 var pi = domDoc.createProcessingInstruction("xml","version='1.0'");
 domDoc.insertBefore(pi, domDoc.childNodes.item(0));

 var root = domDoc.createNode(1,'ROOTElement','');//first para : NodeType(1 means NodeElement), second para: name of node, third para : namespace

 domDoc.appendChild(root);

 var firstChild = domDoc.createNode(1,'FirstChild','');
 root.appendChild(firstChild);
 firstChild.text = "PCDATA of The FirstChild Element";

 var secondChild = domDoc.createNode(1,'SecondChild','');
 root.appendChild(secondChild);

//attributes of the secondChild element
          secondChild.setAttribute('revision','1.0');
alert(domDoc.xml);
}
</script>
</head>
<body>
<span onclick="makeDOM()">click</span>
</body>
</html>

If you want to make or handle XML documents, you can think of them as a DOMDocument.
DOM works in the following way.
Make imaginary DOMDocument, in this case domDoc.
Make root element and append this to the imaginary DOMDocument, in this case root = 'blabla' & domDoc.appendChild(root).
make immediate children of the root. in this case firstChild = 'blabla' & root.appendChild(firstChild).
goes on and on.
Attributes works some differently.
Don't use appendChild method, instead just use the method of setAttribute(string para1,string para2).

For more node types visit http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmmthcreatenode.asp.

HTH.
0
 
LVL 12

Expert Comment

by:ahosang
ID: 7962577
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
ahosang
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8022008
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ (NO REFUND)
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 
LVL 5

Accepted Solution

by:
Netminder earned 0 total points
ID: 8092609
Per recommendation, points NOT refunded and question closed.

Netminder
EE Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Scroll To Top 5 38
Add shadow behind div 5 25
How to check if a session is disconnected in php 9 31
JQuery Syntax... 4 31
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question