[
Lists Home |
Date Index |
Thread Index
]
- From: Rick JELLIFFE <ricko@allette.com.au>
- To: THOMAS PASSIN <tpassin@idsonline.com>
- Date: Thu, 02 Mar 2000 02:14:29 +0800
THOMAS PASSIN wrote:
>
> ashutosh dandavate wrote
> > I am looking out for a xml doc generator, at the
> > client browser after client enters data in fields,
> > that data i want to send to web server as xml doc
> > can xml script be used for that purpose? or any
> > other method you know of?
> I favor about doing it with Javascript in the client browser. You could
> store the generated XML in a hidden field in another form and POST the form
> to send the xml to the server. With Javascript, you could even create
> classes for every data field, if you wanted, that generate the xml. Some
> people say they don't like Javascript because some browsers don't have it,
> but some browsers don't do forms either. And "efficiency" won't be
> important because a human-usable form to collect data won't be that long or
> complicated.
Here is a small JavaScript library that serializes a certain kind of
tree into XML
and sends it in a hidden field. There is no XML parser: I figure that it
would be better (for download time reasons) for the originating server
to convert XML data into JavaScript directly. Less work for the client.
It uses Gavin Nicol's great _charset_ trick so that character encoding
is handled correctly in multilingual sites. (Note: people with
transcoding proxies will need some extra work here.) Some people have
suggested that XML would be simplified if only UTF-8 were allowed: apart
from the fact that one of the big browsers cannot handle UTF-8 for
sending forms data (at least, in the version I tested), requiring UTF-8
only would be a burdon for client-side programmers (how do you transcode
in JavaScript?) and no relief for target server programmers (who will
have to do transcoding anyway, if they are using Java or have a
multilingual site.)
Rick Jelliffe
<html>
<head><title>Example Document</title>
<script type="text/javascript">
// ========================================================
// JavaScript Functions for serializing a tree to XML
//
// Rick Jelliffe, GeoTempo Inc, ricko@geotempo.com
// 2000-02-22 status: beta, tested Netscape only
//
// Copyright (C) 2000 Rick Jelliffe and GeoTempo Inc.
// Permission granted to use and publish this under
// GPL or MPL. Please send any fixes or improvements.
//
// These routines serialize a certain kind of tree
// into XML and puts the result into a hidden field.
// This way you can send structured data over HTTP
// using GET or POST. The routines handle delimiting
// and encoding (charset) labels.
//
// The tree is made from nodes, one node per element.
// A node is an object with three properties:
// "gi" (a string), "attributes" (an object containing
// properties), and "children" (an array of strings or
// nodes).
//
// There is no way to set PIs or comments or CDATA sections.
// However, an XML header must be generated. The encoding
// must be set: it is best if each form has a hidden attribute
// "_charset_" which can be used, since the incoming charset
// is almost always the outgoing charset.
//
// Elements, data content, attributes and attribute values
// can be inserted and deleted as required. However, it
// is a Crash Causing Error (CCE) if there is no gi property
// or the gi property has no value.
//
// Bug: an attribute name cannot be a reserved word in
// Javascript, such as "class".
// ====== Start library
// Private function: make delimiter-characters-used-as-data
// safe in normal data
function delimitContentForXML( s ){
var buf = new String();
for ( var i = 0; i < s.length; i++ ){
switch ( s[i] ){
case "<": buf += "<"; break;
case ">": buf += ">"; break;
case "&": buf += "&"; break;
default: buf += s[i];
}
}
return buf;
}
// Private function: make delimiter-characters-used-as-data
// safe in attributes
function delimitAttributeForXML( s ){
var buf = new String();
for ( var i = 0; i < s.length; i++ ){
switch ( s[i] ){
case "<": buf += "<"; break;
case ">": buf += ">"; break;
case "&": buf += "&"; break;
case "\'": buf += "'"; break;
case "\"": buf += """; break;
default: buf += s[i];
}
}
return buf;
}
// Private function: serialize a single element
function serializeNodeToXML( node ){
var buf = new String();
buf = "<" + node.gi;
if ( node.attributes ){
for( attname in node.attributes ) {
if (node.attributes[attname] != null) {
buf += "\n " + attname + "='"
+ delimitAttributeForXML(
node.attributes[attname]) + "'";
}
}
}
if ( node.children ) {
buf += "\n>"
for ( var i = 0; i < node.children.length; i++){
if ( node.children[i] != null) {
if ( typeof node.children[i] == "object"
) {
buf
+=serializeNodeToXML(node.children[i]);
}
else {
buf += delimitContentForXML(
node.children[i]);
}
}
}
buf += "</" + node.gi + ">" ;
}
else {
buf += "/>"
}
return ( buf );
}
// Private function: append data to a nodes' children
// E.g.
// appendDataToNode( root, "ZZZ" );
// appendDataToNode( root, { gi:"xxx", attributes:{zzz:"eee"}} );
function appendDataToNode( node, data ){
root.children[node.children.length] = data ;
return true;
}
// To add an attribute is easy !
// node.attributes.newname = "newvalue";
// Public function: serialize a tree to XML
function serializeTreeToXML ( rootNode , encoding ){
var buf = "<?xml version=\"1.0\" encoding=\""
+ encoding + "\" ?>\n" ;
buf += serializeNodeToXML(rootNode);
return buf;
};
// Public function: displays an alert box with the XML-ized tree
function viewTreeAsXML(root, encoding){
alert( serializeTreeToXML( root, encoding) );
return true;
}
// Public function:
function insertTreeInField( field, root, encoding ){
field.value = serializeTreeToXML( root, encoding);
return true;
}
// ====== End library
// Some data is here--it is like our own miniDOM !
var tree = {
gi:"cat",
attributes:{id:"Bede" },
children: [{
gi:"physique",
attributes:{id:"bedephyz"},
children:[{
gi:"hair",
attributes:{ length:"high"}
}, {
gi:"voice",
attributes:{ occurs:"nocturnal"},
"miao"
}]
}]
};
</script>
</head>
<body>
<form
onSubmit="insertTreeInField( document.example.xml, tree,
document.example._charset_.value ); "
action="YOUR TARGET URL HERE"
name="example"
method="post">
<input type="hidden" name="_charset_" value="Big5" >
<input type="hidden" name="xml" >
<input type="submit" >
</form>
</body>
***************************************************************************
This is xml-dev, the mailing list for XML developers.
To unsubscribe, mailto:majordomo@xml.org&BODY=unsubscribe%20xml-dev
List archives are available at http://xml.org/archives/xml-dev/threads.html
***************************************************************************
|