Frage

Ich verwende eine Funktion, die zwei DOM-Elemente - Elternteil und Kind aus verschiedenen Dokumenten nimmt.Ich importiere das untergeordnete Element, verwandel es, dann hängen Sie ihn an das übergeordnete Element an.Die letzte Zeile des folgenden Codes ist jedoch jedoch eine DOM-Ausnahme: org.w3c.dom.domexception: falsch_document_err: Ein Knoten wird in einem anderen Dokument verwendet als derjenige, der es erstellt hat.

Bitte sehen Sie den untenstehenden Code: generasacodicetagpre.

War es hilfreich?

Lösung

The short answer is that the child element on the penultimate line belongs to the document created by the line

Document document = builder.parse(new InputSource(new StringReader(transformedChildXml)));

and not the owner document of the parent. So you would have to use importNode again to transfer it to your target document.

But don't do that. Your code has two serialize to String and parse back to Document cycles, which is very inefficient and neither should be necessary. Once you've called importNode at the start, simply fix up the ids in place, and append the child to the parent at the end.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top