Link to home
Start Free TrialLog in
Avatar of desmondtsk
desmondtsk

asked on

Javascript to create XML files

Can anyone please tell me how to go about creating XML files using Javascript?
Is this possible?
Avatar of Patterson
Patterson

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
<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.
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
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
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial