简体   繁体   English

使用Marklogic Java客户端API进行DOM处理

[英]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. 我是新来的MarkLogic的Java API,并试图创造一个XML文档Document使用构造DocumentBuilderFactoryDocumentBuilder ,一切工作正常用下面的代码。

 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. 现在,由于有文档参考,因此可以调用doc.CreateElement()创建xml结构的文档。

In the same way if I refer a Document using DOMHandle from com.marklogic.client.io.DOMHandle; 以相同的方式,如果我使用com.marklogic.client.io.DOMHandle中的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. 现在,从handle创建的文档引用给出了空指针异常。 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. 我知道我正在从getter方法获取文档,该方法返回一个空文档,但是我没有尝试从空文档访问任何内容。 Instead trying to create a document element using doc.createElement() where null pointer exception is arising. 而是尝试使用doc.createElement()创建文档元素,其中会出现空指针异常。

Please explain the issue. 请说明问题。

A DOMHandle represents XML content as a DOM Document. DOMHandle将XML内容表示为DOM文档。 It is not a factory which would create a DOM Document. 它不是将创建DOM文档的工厂。 The handle is just an adapter that wraps a document that we read from the database or create in Java. 句柄只是一个适配器,用于包装我们从数据库中读取的文档或使用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 . 除非使用构造函数DOMHandle(Document content)或方法public void set(Document content)显式设置,否则DOMHandle(Document content)的内容将为null,因此为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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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