Solved

Javascript to create XML files

Posted on 2001-09-04
5
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

632 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