将节点导入到新文档中
我正在尝试将节点从基本文档导入到新文档中,但是当我使用 Document.importNode() 方法时,它会生成异常。有趣的是,这个异常不包含消息,所以除了堆栈跟踪之外,我几乎是盲目的。 (我是java新手)
这是我的方法,它将创建一个新文档并导入节点:
- this.Builder = DocumentBuilder
- this.Element = Node
代码:
public XMLSelection extract ()
{
if (this.Element != null)
{
try
{
Document newDoc = this.Builder.newDocument();
Node node = newDoc.importNode(this.Element, true);
newDoc.appendChild(node);
return new XMLSelection(newDoc);
}
catch(Exception e)
{
Debug.error("XMLSelection::extract", e);
}
}
return null;
}
以及堆栈跟踪:
02-06 14:27:53.328: W/System.err(9082): org.w3c.dom.DOMException
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.ElementImpl.(ElementImpl.java:50)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.createElementNS(DocumentImpl.java:336)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.shallowCopy(DocumentImpl.java:156)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.cloneOrImportNode(DocumentImpl.java:208)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.importNode(DocumentImpl.java:222)
02-06 14:27:53.328: W/System.err(9082): at com.xxxx.xxxx.XMLSelection.extract(XMLSelection.java:57)
I'm trying to import a node from a base-document into a new document but when I use the Document.importNode() method, it generates an exception. The funny part is that this exception does not contain a message so I'm pretty much blind except for the stack-trace. (I'm new to java)
Here is my method that will create a new document and import the node:
- this.Builder = DocumentBuilder
- this.Element = Node
Code:
public XMLSelection extract ()
{
if (this.Element != null)
{
try
{
Document newDoc = this.Builder.newDocument();
Node node = newDoc.importNode(this.Element, true);
newDoc.appendChild(node);
return new XMLSelection(newDoc);
}
catch(Exception e)
{
Debug.error("XMLSelection::extract", e);
}
}
return null;
}
And the stack-trace:
02-06 14:27:53.328: W/System.err(9082): org.w3c.dom.DOMException
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.ElementImpl.(ElementImpl.java:50)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.createElementNS(DocumentImpl.java:336)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.shallowCopy(DocumentImpl.java:156)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.cloneOrImportNode(DocumentImpl.java:208)
02-06 14:27:53.328: W/System.err(9082): at org.apache.harmony.xml.dom.DocumentImpl.importNode(DocumentImpl.java:222)
02-06 14:27:53.328: W/System.err(9082): at com.xxxx.xxxx.XMLSelection.extract(XMLSelection.java:57)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
cloneNode
后跟adoptNode
来使用子元素作为新Document
的根元素。Try a
cloneNode
followed by anadoptNode
to use a child element as the root element of a newDocument
.