1
爪哇的Xerces 2.9.1插入XML:空间= '保留' 在DOM
insertHere.setAttributeNS(XMLConstants.XML_NS_URI, "xml:space", "preserve");
和
insertHere.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve")
都结束了的只是space='preserve'
,没有XML前缀的属性。
insertHere.setAttribute("xml:space", "preserve")
的作品,但它似乎有点不对。我错过了什么?
编辑
我检查。
我在setNamespaceAware打开的情况下阅读了一个模板文档。
然后,我使用以下来制作它的副本,然后我开始插入新的元素。
public static Document copyDocument(Document input) {
DocumentType oldDocType = input.getDoctype();
DocumentType newDocType = null;
Document newDoc;
String oldNamespaceUri = input.getDocumentElement().getNamespaceURI();
if (oldDocType != null) {
// cloning doctypes is 'implementation dependent'
String oldDocTypeName = oldDocType.getName();
newDocType = input.getImplementation().createDocumentType(oldDocTypeName,
oldDocType.getPublicId(),
oldDocType.getSystemId());
newDoc = input.getImplementation().createDocument(oldNamespaceUri, oldDocTypeName,
newDocType);
} else {
newDoc = input.getImplementation().createDocument(oldNamespaceUri,
input.getDocumentElement().getNodeName(),
null);
}
Element newDocElement = (Element)newDoc.importNode(input.getDocumentElement(), true);
newDoc.replaceChild(newDocElement, newDoc.getDocumentElement());
return newDoc;
}
不知何故,我怀疑我忘了'setNamespaceAware'。 – bmargulies 2010-08-12 17:43:34