如何使用DOM importNode没有抛出:DOMException [英] how to using DOM importNode without DOMException

查看:113
本文介绍了如何使用DOM importNode没有抛出:DOMException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个节点复制到不同的Documentg,但它总有抛出:DOMException约

I want to copy an node to a different Documentg, but it always has DOMException about

org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)

org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)

这是我的code

private String getString(Node seqNode) {
    try {           
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = factory.newDocumentBuilder();          
        Document doc = docBuilder.newDocument();

        Element root = doc.createElement("Test");
        doc.appendChild(root);

        /* following line will cause DOMException */
        doc.importNode(seqNode, true);
        ...
        ...
       } catch (Exception e) {

       }
}

在哪里seqNode属于其他文件

where seqNode belongs to other Document

做任何机构有对这个问题的想法? : - )

does any body has idea about this issue? :-)

推荐答案

我碰到这个问题了。要么调用importNode()或cloneNode(),当我得到这个异​​常。和BTW,在XML我解析/生成未使用的命名空间。

I ran into this problem too. I was getting this exception when calling either importNode() or cloneNode(). And BTW, the XML I was parsing/generating was not using namespaces.

似乎包含在Android的DOM解析器(Apache的和谐)是越野车。请参阅此链接:发行2735:和谐DOM实现越野车和不合规。如果使用普通的Java 1.6(这是不是基于课程的和谐)执行相同的code,一切工作正常。

It seems that the DOM parser (from Apache Harmony) that is included in Android is buggy. See this link: Issue 2735: Harmony DOM implementation is buggy and noncompliant. Everything works fine if the same code is executed using plain Java 1.6 (which isn't based on Harmony of course).

我试着设置上的DocumentBuilder setNamespaceAware(真),但这并没有帮助。

I tried setting setNamespaceAware(true) on the DocumentBuilder, but this did not help.

最后,我放弃了,用adoptNode(围绕这一问题的工作),而不是importNode()。这是一种乱伦,因为它是从一个文档树偷一个节点,并把它变成另一回事。但对我来说,第一个文档树只是暂时的,所以我可以做的事情这样。

Eventually, I gave up and worked around the issue by using adoptNode() rather than importNode(). This is kind of incestuous, because it is stealing a node from one Document tree and putting it into another. But in my case, the first Document tree was only temporary, so I could do things this way.

这篇关于如何使用DOM importNode没有抛出:DOMException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆