Use Javascript to assemble a XML file?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TPK

    Use Javascript to assemble a XML file?

    Is there any way javascript can be used to assemble a XML file?

    Any resources out there that may explain how to do this (if possible)?

    TPK

  • Julian Turner

    #2
    Re: Use Javascript to assemble a XML file?


    TPK wrote:
    Is there any way javascript can be used to assemble a XML file?
    >
    Any resources out there that may explain how to do this (if possible)?
    >
    TPK
    Hi

    The answer is yes.

    JavaScript generally has no native XML handling (although there is an
    extension to Firefox which implements E4X I think :
    <URL:http://www.ecma-international.o rg/publications/standards/Ecma-357.htm>)

    However, the latest web browsers all provide a host objects/ActiveX
    objects which Javascript can access to create XML documents.

    If I type in "Javascript and XML" in google, I get approximately 34
    million responses, and there are probably just as many posts on the
    subject if you search this newsgroup. I suggest you start by doing
    some searching and following up a few of those yourself.

    The cross-browser Sarissa Javascript library is also useful to look at,
    or the zXML library provided by <URL:http://www.nczonline.n et>.

    Regards

    Julian Turner

    Comment

    • Martin Honnen

      #3
      Re: Use Javascript to assemble a XML file?

      TPK wrote:
      Is there any way javascript can be used to assemble a XML file?
      >
      Any resources out there that may explain how to do this (if possible)?
      You can use the DOM to build an in memory XML DOM document. Saving to a
      local file is usually (meaning in the browser sandbox) not possible but
      you can use XMLHttpRequest so send such a DOM document to your server.

      var xmlDocument = null;

      if (document.imple mentation && typeof
      document.implem entation.create Document != 'undefined') {
      xmlDocument = document.implem entation.create Document('', 'dummy', null);
      }
      else if (typeof ActiveXObject != 'undefined') {
      try {
      xmlDocument = new ActiveXObject(' Msxml2.DOMDocum ent');
      }
      catch (e) {
      try {
      xmlDocument = new ActiveXObject(' Microsoft.XMLDO M');
      }
      catch (e) {}
      }
      }

      if (xmlDocument != null) {
      var root = xmlDocument.cre ateElement('god s');
      if (xmlDocument.do cumentElement) {
      xmlDocument.rep laceChild(root, xmlDocument.doc umentElement);
      }
      else {
      xmlDocument.app endChild(root);
      }
      var god = xmlDocument.cre ateElement('god ');
      god.appendChild (xmlDocument.cr eateTextNode('K ibo'));
      root.appendChil d(god);
      if (typeof XMLSerializer != 'undefined') {
      alert(new XMLSerializer() .serializeToStr ing(xmlDocument ));
      }
      else {
      alert(xmlDocume nt.xml);
      }
      }

      createDocument is W3C DOM Level 2 Core to create an XML DOM document.
      That method wants the tag name (and namespace) of the root element so
      the newly created document has already a root element. The method is
      supported by browsers like Mozilla/Firefox, Opera 8/9.
      new ActiveXObject(' Msxml2.DOMDocum ent') (respectively new
      ActiveXObject(' Microsoft.XMLDO M')) is for IE/Win using MSXML to create
      an XML DOM document which is empty.

      --

      Martin Honnen

      Comment

      Working...