[jdom-interest] detach() [eg]

Per Kreipke per at onclave.com
Mon Apr 23 07:47:22 PDT 2001


I just subscribed to the list to find out the answer to the problem of
'replacing the root node of a document'. Fortuitious timing I guess.

> > I question the use case. How many times do you want to move the root
> > element of a document?
>
> Very seldom I would think.  It might be one way programmers try to change
> the dtd or schema of a document.

I'm not sure how to quantify 'seldom' but it's currently my chief concern
for two different reasons.

Use 1:

I'd like to push the root element inside of new element. In my case, the
example is that I'm taking a single item of type <foo> and pushing it down
into a set of <foo>.

E.g.

<?xml version="1.0"?>

<foo>
  <bar>...</bar>
</foo>

will become

<set attr1="ugh" attr2="blah">
  <foo>
    <bar>...</bar>
  </foo>
  <computed>...</computed>
</set>

The original <foo> tree is the input source, the rest is computed.

Perhaps my method is wrong and I should be building a separate document for
the output but I don't see why I can't do the above. Perhaps someone can
illustrate how.

Use 2:

Separately, I would like to do exactly what was questioned: "It might be one
way programmers try to change the dtd or schema of a document."

Basically, I'd like to rename the root of the document because its context
has now changed to a different DTD.

For example,

<employee>
  <name first="Elmo" last="Schmotz"/>
</employee>

Would change to:

<member>
  <name first="Elmo" last="Schmotz"/>
</member>

Can I do this without the detach() syntax currently in discussion?

Per.




More information about the jdom-interest mailing list