抛出:DOMException而克隆XML节点 [英] DOMException while cloning XML Node
问题描述
在下面的小例子的cloneNode()方法是基于Java 1.6的,但在Android的5投用code 14的抛出:DOMException。
根据SDK文档code 14的意思是:
NAMESPACE_ERR
如果试图创建或在某种程度上这是不正确关于命名空间改变对象
有没有人一个想法,这可能是为什么?
谢谢你在前进!
公共M级
{
公共静态无效的主要(字串[] args)
{
尝试
{
工厂的DocumentBuilderFactory = DocumentBuilderFactory.newInstance();
的DocumentBuilder建设者= factory.newDocumentBuilder(); 字符串文本=新的String(<泡壳>< /泡壳>);
文献文件= builder.parse(新的InputSource(新StringReader(文本))); 。document.getDocumentElement()cloneNode(真);
}
赶上(抛出:DOMException E)
{}
赶上(的ParserConfigurationException E)
{}
赶上(IOException异常E)
{}
赶上(SAXException的E)
{}
}
}
显然,这是既cloneNode和importNode一个已知的bug: HTTP ://stackoverflow.com/a/8438416/671393
变通办法就是用adoptNode代替,但显然,这将修改源DOM。
此错误是固定在以后的版本: HTTP :?//$c$c.google.com/p/android/issues/detail ID = 2735#C4
如果错误是确因节点不被感知名称空间(如bug报告的建议),则增加了一个虚拟命名空间的可能是另一种解决办法。
The cloneNode() method in the following minimal example works on java 1.6 but throws an DOMException with code 14 under android 5.
Code 14 according to sdk docs means: "NAMESPACE_ERR If an attempt is made to create or change an object in a way which is incorrect with regard to namespaces."
Has anybody an idea why this might be? Thank you in advance!
public class M
{
public static void main(String[] args)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
String text = new String("<blub></blub>");
Document document = builder.parse(new InputSource( new StringReader(text) ));
document.getDocumentElement().cloneNode(true);
}
catch(DOMException e)
{}
catch(ParserConfigurationException e)
{}
catch(IOException e)
{}
catch(SAXException e)
{}
}
}
Apparently this is a known bug with both cloneNode and importNode: http://stackoverflow.com/a/8438416/671393
The "workaround" is to use adoptNode instead, but obviously this will modify the source DOM.
This bug is fixed in later versions: http://code.google.com/p/android/issues/detail?id=2735#c4
If the bug is indeed due to nodes not being namespace aware (as suggested in the bug report), then the addition of a dummy namespace might be another workaround.
这篇关于抛出:DOMException而克隆XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!