我有以下代码循环遍历一组记录并将每条记录移动到一个新文件:
<?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>
包含我想要移动的记录的文件必须遵循以下结构:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ListRecords>
</ListRecords>
我要放置记录的文件具有以下结构:
xarray
但是当我运行此代码时,我的test.xml文件不包含任何记录..
为什么会这样?
我是XML和python的新手,所以如果我错过了什么或者某些东西不清楚请告诉我,以便我可以澄清或添加它。
我这样做是python 2.7
非常感谢所有的帮助和建议!
答案 0 :(得分:1)
您需要对文件进行更改。
添加到代码new_tree.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')