Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

generate xml with javascript

Posted on 2003-03-12
8
Medium Priority
?
682 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
7 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to count occurrences of each item in an array.
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

564 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