?
Solved

generate xml with javascript

Posted on 2003-03-12
8
Medium Priority
?
680 Views
Last Modified: 2013-11-19
How can I generate an xml doc/s using javascript?
links or code would be greatly appreciated
0
Comment
Question by:drn
[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
8 Comments
 
LVL 5

Expert Comment

by:kasandra
ID: 8125176
What do you mean?

What is your source?
0
 
LVL 10

Expert Comment

by:kollegov
ID: 8125209
Slightly modified example from MSDN (local installation)
Work on MIE5+
For more info search in MSDN for
XMLDOMDocument,XMLDOMNode

In my old local installation of MSDN it's here
\MSDN\DNQ000ENU3\MSDN\inet.chm::/xml/reference/scriptref/xmldomdocument_object.htm
Looks like now Microsoft have newer object replacing old ActiveX
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmobjxmldomdocument.asp

In online MSDN I found other thing you probably may want to use
http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/xml.asp

Some more usefull article
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncodecorn/html/corner01182000.asp

And so on around it :)))

--------- Example -----------
<script>
var xmldoc;
function Load()
{
    xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    xmldoc.onreadystatechange = CheckState;
    xmldoc.loadXML(xml.value);
}
function CheckState()
{
    var state = xmldoc.readyState;
    RESULTS.innerHTML += "readyState = " + state + "<BR>"
    if (state == 4)
    {
      var err = xmldoc.parseError;
        if (err.errorCode != 0)
          RESULTS.innerHTML += err.reason + "<BR>"
        else
          RESULTS.innerHTML +="success" + "<BR>"
          alert(xmldoc.xml)
    }
}
</script>

<textarea id=xml>
 <top>
   <child>
       Yahoo
   </child>
</top>
</textarea>
<input type=button value=LOAD onclick="jscript:Load()">
<div id=RESULTS style="color:red; font-weight:bold;"></div>



0
 
LVL 1

Author Comment

by:drn
ID: 8125407
What do you mean? - I need to use javascript to dynamically generate an (as in one) xml document and add users id, ip etc to it each time someone logs on.

What is your source? - i dont have one yet, but the xml would look somthing tike this:

<INFO>
<USER>
<ID>123456<ID>
<IP>123.123.123</IP>
<DATE>1/2/2003></DATE>
</USER>
..
</INFO>

-does the script have to create the xml doc or can i just add to one thats already on the server?

thanks for the links, i hope this clers up my question
0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 28

Expert Comment

by:sybe
ID: 8125614
If you need to get the XML to the server with some post action, you could also use an XML-formatted string, put it in a hidden form-variable and post the form.

At the server you have to process the XML (but you don't say what you want to do with the xml)

0
 
LVL 10

Accepted Solution

by:
kollegov earned 1000 total points
ID: 8133639
Much easier and stable is to generate string and set form field value.
You would need to xml escape some characters like & < > or
you can wrap tag value in <![CDATA[ ..... ]]>

In your case you do not need XML object you only need to generate string presentation of XML data
you will POST on server.

<script>

function doEscape(s) {
   s=s.replace(/\&/,"&amp;")
   s=s.replace(/\"/,"&quot;")
   s=s.replace(/>/,"&gt;")
   s=s.replace(/</,"&lt;")
   return s;
}

function doIt() {
  var s='<INFO>'
  s+='<USER>\n'
  s+='<ID>123456</ID>\n'
  s+='<IP>123.123.123</IP>\n'
  s+='<DATE>1/2/2003></DATE>\n'
  s+='<somefield>' +doEscape("field values with & or < > chars")+'</somefield>\n'
  s+='<someCDATAfield><![CDATA[' +'some field to be wrapped in CDATA'+']]></someCDATAfield>\n'
  s+='</USER>\n'
             ..
  s+=' </INFO>\n'
  alert(s)
  document.myform.xmlfield.value=s;
}
</script>
<form name="myform" onSubmit="doIt()" method="POST">
   <input type="hidden" name="xmlfield" value="">
   <input type="submit" value="Send">
</form>

Isn't this much simplier???
0
 
LVL 10

Expert Comment

by:kollegov
ID: 8133658
But really I'm quite confused with the following questions
1. Do you really need XMLdocument Object or just a string with XML content ?
2. Where you need it on sclient side (in browser) or on server side ?
3. How you suppose to use this generated XML ? ( this may be important for optimal solution )
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10130524
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: kollegov {http:#8133639}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

764 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