Java 在使用DOM4J创建副本时,在xsd:schema中保留或添加默认命名空间
将DOM树从一个文档复制到另一个文档时,我希望将Java 在使用DOM4J创建副本时,在xsd:schema中保留或添加默认命名空间,java,xml,xsd,dom4j,Java,Xml,Xsd,Dom4j,将DOM树从一个文档复制到另一个文档时,我希望将xmlns=”“中的xsd:schema节点从原始文档保留到新文档 但是,当我使用DOM4JcreateCopy()时,它排除了这一点 原始文件包含 <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> hy, 对于复制联合国文档xm
xmlns=”“
中的xsd:schema
节点从原始文档保留到新文档
但是,当我使用DOM4J
createCopy()
时,它排除了这一点
原始文件包含
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
hy,
对于复制联合国文档xml,您可以使用.clone(),以下是一个示例:
public static void copyDocument() {
//read document
Document documentOld = getDocument("C:\\test\\test.xml");
if (documentOld != null) {
Element elRoot = documentOld.getRootElement();
Element elRootCopy = (Element) elRoot.clone();
Document documentNew = DocumentHelper.createDocument();
documentNew.add(elRootCopy);
//save the new document
}
else {
System.out.println("document not found or document is not well format ");
}
}
public static Document getDocument(String pathname) {
Document document = null;
SAXReader saxReader = null;
try {
saxReader = new SAXReader();
document = saxReader.read(new File(pathname));
}
catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
SAXReader reader = new SAXReader();
Document document0 = reader.read(new File(filePath).toURI().toURL());
document1.add(document0.getRootElement().createCopy());
// Create output format
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
format.setIndent(true);
// lets write to a file
XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF8"), format);
writer.write(document);
writer.close();
public static void copyDocument() {
//read document
Document documentOld = getDocument("C:\\test\\test.xml");
if (documentOld != null) {
Element elRoot = documentOld.getRootElement();
Element elRootCopy = (Element) elRoot.clone();
Document documentNew = DocumentHelper.createDocument();
documentNew.add(elRootCopy);
//save the new document
}
else {
System.out.println("document not found or document is not well format ");
}
}
public static Document getDocument(String pathname) {
Document document = null;
SAXReader saxReader = null;
try {
saxReader = new SAXReader();
document = saxReader.read(new File(pathname));
}
catch (DocumentException e) {
e.printStackTrace();
}
return document;
}