473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

JavaScript string value serialization to XML

Hello,

I have been developing an Ajax-style framework for couple of years now.
Now I am reworking some parts of it. The problem was that I used to use
JSON for JavaScript value serialization/deserialization, but this
approach has some limitations when it comes to large data - the
JavaScript at the client side has too much load working with it. So, I
thought, I'd rework the serialization to XML format.

The idea is short-as-possible bare-bones serialization:

For boolean values:
<bool value="true"/>

For numeric values:
<num value="1234.567"/>

For string values:
<str value="ABC"/>

For arrays:
<arr>
<bool index="0" value="true"/>
<num index="1" value="777"/>
<str index="2" value="abc"/>
</arr>

For objects:
<obj>
<bool key="dataFieldA" value="true"/>
<num key="dataFieldB" value="777"/>
<str key="dataFieldC" value="abc"/>
</obj>

For classes:
<obj class="Person">
<str key="name" value="John Smith"/>
<str key="age" value="30"/>
</obj>
etc. You can see where I am landing.

The problem I am stuck with is that in JavaScript, you can have
perfectly legal string with fancy charcodes:

var str = String.fromCharCode(7);

The problem is that the following XML does not validate:

<str value=""/>

You can test this out online (MSIE):
http://www.jurjans.lv/dhtml/XMLparse.html

Right now, it seems that the only exit is to have another layer of
encoding/decoding, handled by JavaScript, when building.parsing the XML
data, something like

<str value="\x07"/>

It makes me somewhat sad, because it brings back to having
JavaScript-based string encoding/decoding (ie, using string.replace),
that might be time and load consuming when doing large strings.

Maybe someone has good ideas how to handle these special charcode
values?

Regards,
Pavils

Mar 16 '06 #1
8 2277
Pavils Jurjans wrote:
The problem is that the following XML does not validate:

<str value=""/>

You can test this out online (MSIE):
http://www.jurjans.lv/dhtml/XMLparse.html

Right now, it seems that the only exit is to have another layer of
encoding/decoding, handled by JavaScript, when building.parsing the XML
data, something like

<str value="\x07"/>


It would be much more sensible to put strings into the tag as text
or cdata content:

<str></str>

Mar 16 '06 #2
This doesn't solve the problem - your example doesn't parse either. The
problem is that  isn't valid character, so if I have such
JavaScript string, I can't encode in XML it with simple means.

Mar 16 '06 #3


Pavils Jurjans wrote:

The idea is short-as-possible bare-bones serialization:

For boolean values:
<bool value="true"/>

For numeric values:
<num value="1234.567"/>

For string values:
<str value="ABC"/>
It is up to you to make up your own notation but you might want to look
into existing schema languages and/or into SOAP web services.

The problem I am stuck with is that in JavaScript, you can have
perfectly legal string with fancy charcodes:

var str = String.fromCharCode(7);

The problem is that the following XML does not validate:

<str value=""/>


That is not a question of validation (where an XML instance document is
checked against a grammar defined by a DTD or schema), rather that is
well-formedness problem, with XML 1.0 that character is not allowed,
whether you use a numeric character reference or not.
With XML 1.0 you would need to encode such characters, e.g. base64
encode them.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Mar 16 '06 #4
Pavils Jurjans wrote:
This doesn't solve the problem - your example doesn't parse either. The
problem is that  isn't valid character, so if I have such
JavaScript string, I can't encode in XML it with simple means.

Please learn to quote appropriate context.

Yes, sorry, you are correct. Characters less than 32, other than tab,
linefeed and carriage return are illegal in XML. Sorry, my suggestion will
do perfectly well for getting legal non-ascii characters into your xml, but
it won't let you put in control characters.

See http://www.w3.org/TR/REC-xml/#charsets

Mar 16 '06 #5
> That is not a question of validation (where an XML instance document is
checked against a grammar defined by a DTD or schema), rather that is
well-formedness problem, with XML 1.0 that character is not allowed,
whether you use a numeric character reference or not.
True, I have been using the wrong term.
With XML 1.0 you would need to encode such characters, e.g. base64
encode them.


Ok, so that's the answer. Another layer of JavaScript-driven
encoding/decoding will be needed. I will try to find the most effective
(speed-wise) way how to do that in both directions. My application has
a form where user can navigate through pages of record entries. When he
select page, Ajax call is made to server, and response contains HTML
that must be injected in the appropriate DIV tag. The thing is that
this HTML is full with special characters, and currently JavaScript has
problems extracting it from the response XML - it's just too slow.

Mar 16 '06 #6
> Sorry, my suggestion will
do perfectly well for getting legal non-ascii characters into your xml, but
it won't let you put in control characters.


Truth been said. It's just that there's no difference - either I store
the string value in attribute or in tag content. It' s a heated debate
for differeny XML-apostles, I'm just judging that the "string variable"
would not need another nested XML elements, so I opt to place the value
in "value" attribute. In a way, I wouldn't like that in example..
<str>some stuff<element>sdfsd</element></str>
... the "element" tag is treated as part of the string value, because
it's actually part of XML doc. Because of that, I want to store the
value in attribute, where it's just not possible to create nested tags.
And, probably, I am right now entering the domain of heated debate :)

Mar 16 '06 #7
Pavils Jurjans wrote:
Another layer of JavaScript-driven
encoding/decoding will be needed. I will try to find the most effective
(speed-wise) way how to do that in both directions.


encodeURIComponent/decodeURIComponent would probably be a good start for
this.
Mar 16 '06 #8
> encodeURIComponent/decodeURIComponent would probably be a good start for
this.

Now this is a fine suggestion! This function has completely escaped my
attention! This probably will do fine. The "bad" thing here is that it
will encode all the unicode characters into at least 6 bytes per
character - although they would be completely Ok for UTF-8 encoded XML
file. But, those extra 4 bytes (6 bytes in encodeURIComponent encoded
form, while only 2 in UTF-8 encoded form) for every unicode character
probably is very small price to pay because of improved speed of
processing.

Thanks!

Regards,

Pavils

Mar 17 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Amy L. | last post by:
I have a class that contains a string array. However, I can't get this object to serialize in the xml output. Is there a trick to get a string to serialize? Thanks Amy.
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
4
by: TT (Tom Tempelaere) | last post by:
Hey there, I need a string stream, but I can't find one in .NET. I thought StringWriter would derive from Stream, alas it doesn't do so. Which leads me to my next question: What is the purpose...
1
by: Kevin Hodgson | last post by:
I store a users SMTP UserName and Password in a settings class, which is serialized to an XML Settings File. I need to encrypt the password string in some manner. Does anyone have any ideas how...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
4
by: John Smith | last post by:
How can I allow someone to cast my C# class into System.String? Is it possible in C#? In C++ I used "operator" keyword to mark C++ member function.
0
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the...
2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.