473,325 Members | 2,792 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,325 software developers and data experts.

Simplified DOM idiom for building XML - revisited

A while ago, I posted a 1/2 formed idea for what was probably an
overcomplicated scheme for improving how we create content using the DOM API.
Since then, I've been refactoring some real-world code that builds content
with DOM, and have come up with a much simpler, more practical idea that I've
had success in implementing.

The problem in a nutshell is that, to build a tree or fragment in the DOM,
it's just about impossible to arrange the code to be structured much like the
XML being generated. Furthermore, the size of the code and the degree of
coupling with the DOM is outrageuos. You have to invoke a method of the
document object to create each new node, you have to invoke an appendChild
method of each node to add content to it, and you have to pass a namespace
argument to each createNode call if you want it to have a namespace URI, even
if they're all the same. Finally, the syntax for creating and appending
attribute nodes is totally nuts, and nothing like adding child elements.

Of course, you can solve all these problems with an appropriate wrapper class
or set of wrapper classes, but we're each inventing our own wrappers to do
this, each solving the problems described above with varying degrees of
success.

The solution I came up with, I think is really nice, and involves using the
native capabilities of most languages to build nested arrays in-line along
with some very trivial parsing to allow the use of compact node definition
strings (syntax roughly similar to XPath). The inability of nested arrays to
represent children of sepcific nodes is solved simply by treating any nested
array as the set of children for the preceeding node in the array.

Here's a hypothetical example (similar to some real, working code) in VB...

DOMWrap.DOM.appendChild DOMWrap.createFragment( _
Array("Invoice", Array(
"@date=", Date, _
"Customer", Array( _
"@name=", CustName),
"Body",
InvoiceLines()))), _
"uri:fooinc.com:invoice"

In the array, each element may be a node definition string, a node object, or
a fragment. A node definition may end with a dangling = sign, in which case,
the next array item supplies the text content for the node. Notice that the
code above has a structure much like the XML it is generating, and is vastly
more compact and easy to read than if it were written as discrete createNode
and appendChild calls. Also notice that, if all the nodes being created from
string definitions are in the same namespace, I only have to pass a single
namespaceURI argument.

So - my suggestion is to create this simple wrapper in many languages (at
least Java, C#, VB, Perl, and Python), put it on SourceForge, and try to get
people to use it as a de facto standard, so no one has to reinvent this wheel
again.

Would anyone besides me be interested in having me do that?

- Steve J.
Aug 26 '05 #1
4 1390
I personally feel, DOM is a nice API. I think, it was natural outcome
of the hierarchial nature of XML. By using arrays, you are introducing
a linear storage to map to a hierarchical object(i.e. XML) which is
hard to grasp..

Regards,
Mukul

Aug 26 '05 #2
On 26 Aug 2005 07:35:45 -0700, "Mukul Gandhi" <mu**********@yahoo.com> wrote:
I personally feel, DOM is a nice API. I think, it was natural outcome
of the hierarchial nature of XML. By using arrays, you are introducing
a linear storage to map to a hierarchical object(i.e. XML) which is
hard to grasp..

Regards,
Mukul


I'll start my reply by acknowledging that it's silly to ask for feedback, then
argue with it, but I'm human, and therefore silly.

I agree that the DOM has its advantages, or I'd never have bothered with it
enough to get tired of its verboseness. Yes, it's great that DOM allows such
fine control over nodes and abstracts away much of how they actually appear in
XML text. That does not mean, however, that I always want to use something
like 8 verbose procedure calls to build a minor 3-node fragment. Furthermore,
to decipher that code later takes a lot of effort, but I value writing code
that's obvious just looking at it, so the unwrapped DOM raises some real
issues for me. It's the unwrapped DOM that seems to me to be hard to grasp
for some parts of a task.

The wrapper I propose leaves the whole DOM API in place to use as much as you
like, and adds the ability to easily toss together simple fragments as needed.
Good uses for this include building the high-level skeleton of a document and
building skeletons of smallish fragments that will have details added to them,
and then be added as children somewhere in the main document skeleton. In
short, I have some control over the granularity of the code rather than being
stuck at the finest granularity for every single node.
Aug 27 '05 #3
your wrote:
That does not mean, however, that I always want to use something
like 8 verbose procedure calls to build a minor 3-node fragment.


sorry, but I don't mind writing tons of code if I am writing in a
standard language(that too published by W3C)..

certainly if people read your approach, they will find merit in it.. I
did'nt say I did'nt like this proposal :)

Regards,
Mukul

Aug 27 '05 #4
Steve Jorgensen wrote:
Would anyone besides me be interested in having me do that?


Well, have you ever looked at the ML language? Defining types and
constructing object structures of them is a breeze.

Soren
Aug 28 '05 #5

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

Similar topics

2
by: Thomas Philips | last post by:
I recently had the need to sort a large number of lists of lists, and wondered if an improvement to the Decorate-Sort-Undecorate idiom is in the works. Ideally, I would like to sort the list of...
3
by: David Morgenthaler | last post by:
In many of my scripts I've used the following idiom for accessing data files placed nearby: BASEDIR = os.path.dirname(__file__) .. .. .. fp = file(os.path.join(BASEDIR,"somefile.txt")) .. ..
9
by: Dan Perl | last post by:
Is there a mechanism or an idiom for adding code for debugging so that it can easily be removed in the production code? I am thinking of something similar to the C/C++ preprocessor statements with...
12
by: Frans Englich | last post by:
Hello, Since Python doesn't have static typing, how is the same result as traditional function overloads results in acheived? With function overloads the "selection of code path depending on...
6
by: Zhang Weiwu | last post by:
Hello. I am working with a php software project, in it (www.egroupware.org) Chinese simplified locate is "zh" while Traditional Chinese "tw". I wish to send correct language attribute in http...
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
5
by: sandman | last post by:
I've been testing my web app on another workstation to simulate using the server time. The test pc's time is an hour behind the server time and when the user processes a request, the server time...
13
by: WXS | last post by:
Is there or should there be a simplified property syntax? For example we have to write: class MyClass { private int _field; public int Field { get { return _field;} set { _field=value}} } ...
14
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.