Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
|
Using XML Parser for Java, 18 of 22
I am trying to add an XMLDocument as a child to an existing element. Here's an example:
import org.w3c.dom.*; import java.util.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class ggg {public static void main (String [] args) throws Exception { new ggg().doWork();; public void doWork() throws Exception {XMLDocument doc1 = new XMLDocument(); Element root1=doc1.createElement("root1"); XMLDocument doc2= new XMLDocument(); Element root2=doc2.createElement("root2"); root1.appendChild(root2); doc1.print(System.out);};};
This reports:
D:\Temp\Oracle\sample>c:\jdk1.2.2\bin\javac -classpath D:\Temp\Oracle\lib\xmlparserv2.jar;. ggg.javaD:\Temp\Oracle\sample>c:\jdk1.2.2\bin\java -classpath D:\Temp\Oracle\lib\xmlparserv2.jar;. gggException in thread "main" java.lang.NullPointerException at oracle.xml.parser.v2.XMLDOMException.(XMLDOMException.java:67) at oracle.xml.parser.v2.XMLNode.checkDocument(XMLNode.java:919) at oracle.xml.parser.v2.XMLNode.appendChild(XMLNode.java, Compiled Code) at oracle.xml.parser.v2.XMLNode.appendChild(XMLNode.java:494) at ggg.doWork(ggg.java:20) at ggg.main(ggg.java:12)
The following works for me: :
DocumentFragment rootNode = new XMLDocumentFragment(); DOMParser d = new DOMParser(); d.parse("http://.../stuff.xml"); Document doc = d.getDocument(); Element e = doc.getDocumentElement(); // Important to remove it from the first doc // before adding it to the other doc. doc.removeChild(e); rootNode.appendChild(e);
You need to use the DocumentFragment class to do this as a document cannot have more than one root.
Actually, isn't this specifically a problem with appending a node created in another document, since all nodes contain a reference to the document they are created in? While Document Fragmentsolves this, it isn't a more than one root problem, is it? Is there a quick or easy way to convert a com.w3c.dom.Document to org.w3c.dom.DocumentFragment?
I have this piece of code:
XSLStylesheet XSLProcessorStylesheet = new XSLStylesheet(XSLProcessorDoc, XSLProcessorURL); XSLStylesheet XSLRendererStylesheet = new XSLStylesheet(XSLRendererDoc, XSLRendererURL); XSLProcessor processor = new XSLProcessor(); // configure the processorprocessor.showWarnings(true); processor.setErrorStream(System.err); XMLDocumentFragment processedXML = processor.processXSL(XSLProcessorStylesheet, XMLInputDoc); XMLDocumentFragment renderedXML = processor.processXSL(XSLRendererStylesheet, processedXML); Document resultXML = new XMLDocument(); resultXML.appendChild(renderedXML);
The last line causes Exception in thread "main" oracle.xml.parser.v2.
XMLDOMException: Node of this type cannot be added.
Do I have to create a root element _every time_, even if I know that the resulting DocumentFragment is a well formed XML Document (and of course has only one root element!)?
It happens, as you have guessed, because a Fragment can have more than one "root" element (for lack of a better term). In order towork around this, use the Node functions to extract the one rootelement from your fragment and cast it into an
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|