Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
Python 将xml节点和子节点复制到新的xml文件_Python_Xml_File_Tree_Copy - Fatal编程技术网

Python 将xml节点和子节点复制到新的xml文件

Python 将xml节点和子节点复制到新的xml文件,python,xml,file,tree,copy,Python,Xml,File,Tree,Copy,我有以下代码在一组记录上循环并将每条记录移动到一个新文件: import os import xml.etree.cElementTree as ET for filename in os.listdir('modemuze'): if filename.endswith('.xml'): original_tree = ET.ElementTree(file='modemuze/'+filename) root = original_tree.get

我有以下代码在一组记录上循环并将每条记录移动到一个新文件:

import os
import xml.etree.cElementTree as ET

for filename in os.listdir('modemuze'):
    if filename.endswith('.xml'):

        original_tree = ET.ElementTree(file='modemuze/'+filename)
        root = original_tree.getroot()

        for child in root[2]:
            if child.tag == "{http://www.openarchives.org/OAI/2.0/}record":
                new_tree = ET.ElementTree(file='test.xml')
                new_root = new_tree.getroot()
                new_root.append(child)
包含我要移动的记录的文件必须具有以下结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<OAI-PMH xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd" xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <responseDate>2018-02-14T12:15:11.241+02:00</responseDate>
    <request verb="ListRecords">147.102.11.65:9000/fashionedmfp/oai</request>
    <ListRecords>
        <record>
            <header>
                <identifier>oai:fashionedmfp:8c549bcd078e2ce84a265d318547c5f8e8bf0cd0</identifier>
                <datestamp>2016-06-27</datestamp>
            </header>
            <metadata>
                <rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:edm="http://www.europeana.eu/schemas/edm/" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:edmfp="http://www.europeanafashion.eu/edmfp/" xmlns:rdaGr2="http://rdvocab.info/ElementsGr2/" xmlns:ore="http://www.openarchives.org/ore/terms/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" xmlns:mrel="http://id.loc.gov/vocabulary/relators/" xmlns:owl="http://www.w3.org/2002/07/owl#" xmlns:wgs84="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:crm="http://www.cidoc-crm.org/rdfs/cidoc_crm_v5.0.2_english_label.rdfs#" xmlns:gr="http://www.heppnetz.de/ontologies/goodrelations/v1#" xmlns:xalan="http://xml.apache.org/xalan" xmlns:skos="http://www.w3.org/2004/02/skos/core#">
                    <edm:ProvidedCHO rdf:about="localID/europeana-fashion/10918x1y95563">
                        <dcterms:created>circa 1855-1874</dcterms:created>
                        <dcterms:issued>1875</dcterms:issued>
                        <dcterms:issued>1875</dcterms:issued>
                        <dcterms:medium>katoen</dcterms:medium>
                        <dcterms:provenance>schenking</dcterms:provenance>
                        <dcterms:provenance>1958</dcterms:provenance>
                        <dcterms:spatial>
                            <edm:Place>
                                <skos:prefLabel>Nederland</skos:prefLabel>
                            </edm:Place>
                        </dcterms:spatial>
                        <dcterms:creator>
                            <edm:Agent/>
                        </dcterms:creator>
                        <dc:creator>
                            <edm:Agent/>
                        </dc:creator>
                        <dc:description>Jurk (kind) van wit piqué, gegarneerd met broderie en soustache</dc:description>
                        <dc:identifier>0061211</dc:identifier>
                        <dc:title>jurken</dc:title>
                        <dc:title>kinderkleding</dc:title>
                        <edm:type>IMAGE</edm:type>
                        <edmfp:localType>jurken</edmfp:localType>
                        <edmfp:localType>kinderkleding</edmfp:localType>
                        <edmfp:technique>piqué-weefsel</edmfp:technique>
                    </edm:ProvidedCHO>
                    <ore:Aggregation rdf:about="localID/europeana-fashion/Aggregation_10918x1y95563">
                        <edm:aggregatedCHO rdf:resource="localID/europeana-fashion/10918x1y95563"/>
                        <edm:dataProvider>
                            <edm:Agent>
                                <skos:prefLabel>Gemeentemuseum Den Haag</skos:prefLabel>
                            </edm:Agent>
                        </edm:dataProvider>
                        <edm:hasView>
                            <edm:WebResource rdf:about="http://images.gemeentemuseum.nl/br/0061211.jpg"/>
                        </edm:hasView>
                        <edm:isShownBy>
                            <edm:WebResource rdf:about="http://images.gemeentemuseum.nl/br/0061211.jpg"/>
                        </edm:isShownBy>
                        <edm:provider>
                            <edm:Agent rdf:about="http://www.europeanafashion.eu/">
                                <skos:prefLabel>Europeana Fashion</skos:prefLabel>
                            </edm:Agent>
                        </edm:provider>
                        <edm:rights rdf:resource="http://www.europeana.eu/rights/rr-f/"/>
                    </ore:Aggregation>
                </rdf:RDF>
            </metadata>
        </record>
    </ListRecords>
</OAI-PMH>

2018-02-14T12:15:11.241+02:00
147.102.11.65:9000/fashionedmfp/oai
oai:fashionedmfp:8c549bcd078e2ce84a265d318547c5f8e8bf0cd0
2016-06-27
大约1855-1874年
1875
1875
卡托恩
申金
1958
荷兰
朱克(善良)范·维特·皮奎,格加内尔遇到了布罗德里恩·苏斯塔什
0061211
尤肯
仁慈的
形象
尤肯
仁慈的
皮奎-韦弗塞尔
海牙市立博物馆
欧洲时尚
我要将记录放入的文件具有以下结构:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ListRecords>
</ListRecords>

但是当我运行此代码时,我的test.xml文件不包含任何记录。
为什么会发生这种情况?
我对XML和python完全陌生,因此如果我遗漏了什么或不清楚什么,请让我知道,以便我可以澄清或添加它。
我正在使用python 2.7执行此操作

非常感谢所有的帮助和建议

您需要将更改写入文件。 添加到代码
new_树中。write('test.xml')

因此,代码应该如下所示

import os
import xml.etree.cElementTree as ET

for filename in os.listdir('modemuze'):
    if filename.endswith('.xml'):

        original_tree = ET.ElementTree(file='modemuze/'+filename)
        root = original_tree.getroot()

        for child in root[2]:
            if child.tag == "{http://www.openarchives.org/OAI/2.0/}record":
                new_tree = ET.ElementTree(file='test.xml')
                new_root = new_tree.getroot()
                new_root.append(child)
                new_tree.write('test.xml')