Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.musoftware.de!wum.musoftware.de!news.karotte.org!news.space.net!newsout01.versatel.de!news.versatel.de!not-for-mail From: Michael Jung Newsgroups: comp.lang.java.programmer Subject: Re: xml:id Date: Mon, 19 Dec 2011 19:06:41 +0100 Lines: 60 Message-ID: <87vcpctozy.fsf@golem.phantasia.org> References: <87wr9t1uxp.fsf@golem.phantasia.org> <33c64ae6-d7f1-466b-8534-35f777eb3c7c@h4g2000yqk.googlegroups.com> <87ehw0ap2i.fsf@golem.phantasia.org> NNTP-Posting-Host: i59f763f3.versanet.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news01.versatel.de 1324318001 4848 89.247.99.243 (19 Dec 2011 18:06:41 GMT) X-Complaints-To: abuse@versatel.de NNTP-Posting-Date: Mon, 19 Dec 2011 18:06:41 +0000 (UTC) User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Cancel-Lock: sha1:T4uNX/kv7N4PwVS8sIER8HVYXbI= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10881 Arved Sandstrom 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