473,803 Members | 4,157 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing the document element of a DOM

Hi,

I'm using XML DOM in Java and I have a function like this

Element makeSomeXml(Doc ument doc) {..}

This returns an Element owned by (but not appended to) the specified
document.

Now, when I call this function, sometimes I want to append the result
somewhere in the document, but sometimes I want to have the returned element
as a document in it's own right (i.e. as the root of the XML document). In
principle this seems like a reasonable requirement. I have an XML Element
and it's up to me whether I want to use it as the root element or plug it
into a bigger document.

The problem is that AFAIK a document must always have a root element, and
the root element cannot be replaced. nor can an existing element be
"upgraded" to be a document in it's own right. So I can't see a clean way of
doing what I want.

This seems a bit of a limitation in the way DOM represents the document
element - it seems to treat the root element and the document itself as the
same thing which isn't really the case. If I was editing XML in a file I
could leave all the namespace declarations and other stuff in place and
replace the root element.

Have I misunderstood the way the DOM works at the top level? Is there a
simple clean solution to what I want to do?

Many thanks in advance

Andy


Jul 20 '05 #1
1 2623


Andy Fish wrote:

This seems a bit of a limitation in the way DOM represents the document
element - it seems to treat the root element and the document itself as the
same thing which isn't really the case.


The W3C DOM clearly distinguishes Document nodes and Element nodes. I
don't see why you cannot replace the documentElement node of a document:

import javax.xml.parse rs.DocumentBuil derFactory;
import javax.xml.parse rs.DocumentBuil der;

import org.w3c.dom.DOM Implementation;
import org.w3c.dom.Doc ument;
import org.w3c.dom.Ele ment;

public class Test20040703 {
public static void main (String[] args) {
try {
DocumentBuilder Factory documentBuilder Factory =
DocumentBuilder Factory.newInst ance();
documentBuilder Factory.setName spaceAware(true );
DocumentBuilder documentBuilder =
documentBuilder Factory.newDocu mentBuilder();

DOMImplementati on domImplementati on =
documentBuilder .getDOMImplemen tation();

Document xmlDocument = domImplementati on.createDocume nt("",
"gods", null);

Element god = xmlDocument.cre ateElement("god ");

xmlDocument.get DocumentElement ().appendChild( god);

Element devils = xmlDocument.cre ateElement("dev ils");

xmlDocument.rep laceChild(devil s, xmlDocument.get DocumentElement ());

Element devil = xmlDocument.cre ateElement("dev il");

xmlDocument.get DocumentElement ().appendChild( devil);

System.out.prin tln(xmlDocument .getDocumentEle ment().getNodeN ame());
}
catch (Exception e) {
System.out.prin tln("Error: " + e);
}
}
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

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

Similar topics

9
1934
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using top.A.tableVar = document.getElementById("someTable"); As long as Frame B is *not* 'refreshed/ reloaded' witk another page the
31
5728
by: Arthur Shapiro | last post by:
I'm the webmaster for a recreational organization. As part of one page of the site, I have an HTML "Calendar at a Glance" of the organization's events for the month. It's a simple table of a calendar, 7 across by whatever needed down, and I manually create it each month - not a big deal. Every day I go in and darken the background color of the current day's cell by changing the appropriate <TD> entry to <TD bgcolor="c63800"> and...
8
3373
by: horos | last post by:
hey all, Ok, a related question to my previous one on data dumpers for postscript. In the process of putting a form together, I'm using a lot of placeholder variables that I really don't care about in the submitted action. I'd therefore like to get rid of them by doing something like:
2
5262
by: rpesq | last post by:
Hi, I Need help changing the content of a DIV. I sincerely researched the issue and have not found a solution except to scrap the idea and stick with the iframe code that I had been using. My purpose here is to avoid iframes. I know how to change the DIV content to other basic html statements, such as basic IMG's or Lists, but I want to put a Javascript into the DIV, and have not found a way to do it.
7
6065
by: Stefan Finzel | last post by:
Hi, is there a way to change the display property on Windows Mobile 2003 SE Mobile/Pocket Internet Explorer? See following example. Please note: visibilty property has the same problem. Is there any other way to display/undisplay parts of web pages? TIA
2
1725
by: Urs Vogel | last post by:
Hi When using XmlDocument, I can create nodes and attributes as I like. What I didn't achieve is changing the Value of a node (created with createElement), it claims that it's the wrong node type. When changing the InnerText, I loose all child nodes. When cloning the node and reassigning the child nodes of the original node to the cloned node, the children are not visilble in the InnerXml. To make it simple. How do I change the value...
6
2295
by: reynard | last post by:
I have a page that uses this doctype <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> but when I change to this: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> it breaks my javascript. Particularly I have a code that try to set element.style.left, when I use the second doctype, FF complains "Error in parsing value for property 'left'. Declaration dropped"
4
1562
by: VK | last post by:
As per discussion in the thread http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/117e4f3a30f4d1c3 I'm proposing to add new FAQ entry: ---------------------------------------------------------------- I'm changing my page but nothing is changing on the screen. Why? ---------------------------------------------------------------- You may be experiencing the repaint delay problem. All existing browsers do not update...
7
3650
by: petethebloke | last post by:
Can anyone help? I have a client who has made a "dynamic interactive map" of our city using Dreamweaver. Each map file has hotspots that pop-up a div with a little image when the mouse goes over them. They also link to another file. I've converting the application to a PHP-AJAX system at http://www.ilex-urc-maps.com/testing.html but I can't get the dynamically altered hotspots to work. Any ideas? Please don't tell me that the client...
0
9703
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
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10316
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
10295
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
10069
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
9125
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
6842
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();...
1
4275
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
2
3798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.