Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript to create XML files

Posted on 2001-09-04
5
Medium Priority
?
450 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

886 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