Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #10881

Re: xml:id

From Michael Jung <miju@golem.phantasia.org>
Newsgroups comp.lang.java.programmer
Subject Re: xml:id
Date 2011-12-19 19:06 +0100
Message-ID <87vcpctozy.fsf@golem.phantasia.org> (permalink)
References <87wr9t1uxp.fsf@golem.phantasia.org> <33c64ae6-d7f1-466b-8534-35f777eb3c7c@h4g2000yqk.googlegroups.com> <87ehw0ap2i.fsf@golem.phantasia.org> <zHIHq.27163$8O1.9231@newsfe07.iad>

Show all headers | View raw


Arved Sandstrom <asandstrom3minus1@eastlink.ca> writes:
> On 11-12-19 05:27 AM, Michael Jung wrote:
[...]
> Not "forgotten" exactly. I tested with JDK 1.6 and 1.7, and I find that
> if the adopted node is placed _somewhere_ (appendChild or what have
> you), that if you obtain the list of "elem" elements in the new
> document, that you'll have 2 of them, and that if you inspect the isId()
> value of the 'id" attributes, that both of them are TRUE.
> Furthermore, if you retrieve by getElementById(), using "x" actually
> returns an element, but using "x2" does not. So that tells me that
> things are dubious overall if you've performed your scenario.
[...]

Some of this code was apparently garbled in my attempt to tone the
working example down. Here is some changed code that still yields the
output above.

[...]
   Document parsed = docBuilder.parse(new File("src/test.xml"));
   Element el = parsed.getElementById("x");
   System.out.println(el);

   Document parsed2 = docBuilder.parse(new File("src/test2.xml"));
   Element el2 = parsed2.getElementById("x");
   System.out.println(el2);

   parsed2.adoptNode(el);
   //parsed2.getDocumentElement().removeChild(el2);
   //parsed2.getDocumentElement().appendChild(el);
   parsed2.getDocumentElement().replaceChild(el, el2);
   System.out.println(parsed2.getElementById("x"));
[...]

(Create a copy of test.xml called test2.xml.) What I am attempting to
achieve should be obvious by now: replace one node with the other from
a different file.  The commented out lines replacing the one following
them also doesn't work.

> What *does* work is to (1) remove the first element, the one with value
> "x", and (2) to then setIdAttribute() on the adopted/placed node with id
> attribute of "x2".

That is true: ie. putting "el.setIdAttribute("id", true);" somewhere
in the code above.  This way I need to know the id attribute's name,
but I can live with that.

> Maybe I'm missing something, but if you're willing to call adoptNode()
> in the course of doing what you're doing, what's the problem with
> removing the element that is going to conflict, and also calling
> setIdAttribute()?

setIdAttribute seems out of place. It's not likely that the schema
changes in that respect but it still is odd.  And parsing the schema
separately just for that is overkill. At least this issue could be
documented somewhere in the javadoc (I couldn't find anything). Now,
if there were a getIdAttribute that would be better.

Thanks.

Michael

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-18 21:33 +0100
  Re: xml:id Lee Fesperman <firstsql@gmail.com> - 2011-12-19 00:24 -0800
    Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-19 10:27 +0100
      Re: xml:id Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-12-19 11:25 -0400
        Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-19 19:06 +0100
          Re: xml:id Arved Sandstrom <asandstrom3minus1@eastlink.ca> - 2011-12-19 17:26 -0400
            Re: xml:id Michael Jung <miju@golem.phantasia.org> - 2011-12-20 22:54 +0100

csiph-web