473,657 Members | 2,316 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.app endChild DOMWrap.createF ragment( _
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 1402
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**********@y ahoo.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
2005
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 lists (or tuples) in place by using a simple variant of the current idiom, i.e. list_of_lists.sort(*columns) where *columns is a tuple that specifies the column order for the sort. If *columns is left blank, the sort ought to work as it does...
3
1919
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
2019
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 which you can compile an application with the debug code or without it (the default). Dan
12
1834
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 data type" is transparent and automatic since the typing system figure out what goes to what. But in Python, when one wants to be able to pass different data types into a single "entry point" for functionality, how is that best done? To in a...
6
22251
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 header, I found "zh" is not standard. I found this line in apache2's default httpd.conf # Simplified Chinese (zh-CN) AddLanguage zh-CN .zh-cn
7
2318
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 shown by the example: -- code fragment 1 ---------------------------------------------------- class MyClass
5
2452
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 is picked up. The problem is that I've got a "clock" on the web page that updates the "current time" every second since the app is time critical. The clock is a jscript routine. It seems to be picking up the local time. I'm just instantiating a...
13
1811
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}} } What if instead you could write:
14
3160
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 has a nice article you can read. Anyway, I discovered that if you make all members in the implementation class mutable, you can in fact use this idiom without any "unnecessary" memory allocation. Here's a minimal example of the method: // In the...
0
8411
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.