Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在使用DOM4J创建副本时,在xsd:schema中保留或添加默认命名空间_Java_Xml_Xsd_Dom4j - Fatal编程技术网

Java 在使用DOM4J创建副本时,在xsd:schema中保留或添加默认命名空间

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

将DOM树从一个文档复制到另一个文档时,我希望将
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;
}