Java DOM XML:需要命名空间前缀,但不需要元素名称
在用Java构建XML的开始部分,我有以下代码块。根元素显然是根元素
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.newDocument();
root = doc.createElementNS(NS,"xsi:" + ROOT_ELEM);
doc.appendChild(root);
} catch (ParserConfigurationException e) {
LOG.error("Unable to create an XML document.", e);
}
root = doc.createElementNS(NS,"xsi:" + ROOT_ELEM);
部分为XML行添加带有xsi前缀的名称空间:
<xsi:rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
我的问题是xsi
部分需要xmlns:xsi=...
而不是xsi:rootElement
部分。这可能吗?我知道我可以将整个过程转换为String
并进行搜索和替换,但这是一种不成熟的做法,我想知道是否有一种方法可以有系统地使用DOM解析器api。这是我想要的一句话:
<rootElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
共 (0) 个答案