Hi

Tim O'Donnell wrote that he wanted to "merge" several DOM documents into a
single DOM document (see his full message attached below for more detail).
One of the things that occurs to me is that if you already have XML files
for these documents AND the "root" of those documents is where you want to
merge from, then you can use a nifty trick like this to "merge" them.

<!DOCTYPE AllProjects [
  <!ELEMENT AllProjects (Project+)>
  <!ELEMENT Project (#PCDATA)>
  <!ENTITY Project1 SYSTEM "project1.xml">
  <!ENTITY Project2 SYSTEM "project2.xml">
  <!ENTITY Project3 SYSTEM "project3.xml">
]>
<AllProjects>
  &project1;
  &project2;
  &project3;
</AllProjects>

You could write the above XML into a stream, then parse from the stream.
You wouldn't even have to make a tmp file!

If you are merging from a "sub-element" in the source documents, then this
trick isn't going to help.
    
Regards,

David I. Bell
Hewlett-Packard Laboratories
mailto:[EMAIL PROTECTED]
Tel: 650.236-5007
FAX: 650.852-8488

Hewlett-Packard Laboratories
Mailstop 1U-14
1501 Page Mill Rd.
Palo Alto, CA  94304-1126


-----Original Message-----
From: Tim O'Donnell [mailto:[EMAIL PROTECTED]
Sent: Wednesday, February 23, 2000 11:00 PM
To: [EMAIL PROTECTED]
Subject: "Cut & Paste" From DOM to DOM


Hi,

        I have a general XML/DOM question that I've been grappling with all
night
tonight.  It's probably not Xerces specific, so just tell me to take a hike
if that's the case.  But I'm not sure where else to go...

        I have several XML documents with the following structure:

<Projects>
        <Project id="<some unique number>">
                (more nodes here)
        </Project>
</Projects>

My goal is to "merge" all of these documents such that the structure
becomes:

<Projects>
        <Project id="<some number>">
                ...
        </Project>
        <Project id="<some number>">
                ...
        </Project>
        ...
</Projects>

In other words, I want all of the "Project" structures to be children of the
"Projects" element.

So I created a new XmlDocument, created a "Projects" element and appended it
to the new document.  Now I thought I'd be able to open each file, grab the
"Project" node, and append it to my "Projects" node.  Apparently I was
wrong.  My friendly JRE informs me that:

com.sun.xml.tree.DomEx: That node doesn't belong in this document.

Not quite sure what exactly that error means. I tried to grab the entire
document this time and append it to my new XmlDocument.  Still no luck.  I'm
really stuck as to how I go about "cutting & pasting" the Project node from
one XML document, and appending it to another tree.

My guts tell me I need to use a DocumentFragment, thus preserving all of the
children of the Project nodes.  But the only method I see in the DOM spec
which uses a DocumentFragment is for the XmlDocument class
(createDocumentFragment).  However, that method only creates an empty
DocumentFragment.  I'm terrified that I might have to create an empty
DocumentFragment and somehow recursively add all of the children nodes, AND
THEN append the whole DocumentFragment to my XmlDocument.

So here I am.  Confused (what does that error message really mean?), weary
(I've tried almost everything I can think of, and seen that error so many
times I just may cry myself to sleep tonight), and scared (that I may have
to write the recursive routine to build the docfrag).

I guess I just want someone to point me in the right direction.  Am I
correct in my docfrag thoughts?  Or is there a better/simpler way to
accomplish this task?  I have to imagine this is a rather common task in
XML, so there has to be a simple way to do it.

Thanks so much for any help you guys can provide!  In the immortal words of
our favorite princess: "Help me Xerces-dev.  You're my only hope!"


-Tim

Reply via email to