Solved

Javascript to create XML files

Posted on 2001-09-04
5
400 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

759 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now