简体   繁体   中英

DOM handling using marklogic java client api

I'm new to MarkLogic java API and trying to create an xml document where Document is constructed using DocumentBuilderFactory and DocumentBuilder and everything is working fine with the following code.

 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
 DocumentBuilder docBuilder=factory.newDocumentBuilder();
 Document doc=docBuilder.newDocument();  //Works fine

Now since I have doc reference I can call doc.CreateElement() to create an xml structured document.

In the same way if I refer a Document using DOMHandle from com.marklogic.client.io.DOMHandle;

 DOMHandle handle=new DOMHandle();
 Document doc=handle.get();
 doc.createElement();         //NULL POINTER EXCEPTION

Now the document reference created from handle gives an null pointer exception. I understood that I am getting document from a getter method which returns an empty document but I am not trying to access anything from the empty document. Instead trying to create a document element using doc.createElement() where null pointer exception is arising.

Please explain the issue.

A DOMHandle represents XML content as a DOM Document. It is not a factory which would create a DOM Document. The handle is just an adapter that wraps a document that we read from the database or create in Java. Unless explicitly set with the constructor DOMHandle(Document content) or with the method public void set(Document content) , the content of the DOMHandle would be null and hence the NullPointerException . You should probably do one of these

DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=factory.newDocumentBuilder();
Document doc=docBuilder.newDocument();
// Build the Document completely and assign it to the handle and use the handle
DOMHandle handle = new DOMHandle(); 
handle.set(doc);
// or DOMHandle handle = new DOMHandle(doc); 
// or DOMHandle handle = new DOMHandle().with(doc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM