--Even after learning a little about XSLT, I didn't use it as the metadata/xls formats change so a single stylesheet based approach won't work ---
I have been trying for the last few hours to grab a csv and dump the data in each tag to a CSV but nothing has worked. I have tried elemtree, parse and regex based on a few other Q&A's in the forum.
For example works fine for his test data but it won't work on my xml (sample at end of question).
tree = ET.parse("test2.xml")
doc = tree.getroot()
thingy = doc.find('custod')
print thingy.attrib
Traceback (most recent call last): File "", line 1, in AttributeError: 'NoneType' object has no attribute 'attrib'
doc
<Element anzmeta at 801a300>
thingy = doc.find('anzmeta')
print thingy.attrib
Traceback (most recent call last): File "", line 1, in AttributeError: 'NoneType' object has no attribute 'attrib'
doc.attrib
{}
--- Try using REX
rex = re.compile(r'<custod.*?>(.*?)</custod>',re.S|re.M)
rex
<_sre.SRE_Pattern object at 0x080724A0>
match=rex.match('test2.xml')
match
text = match.groups()[0].strip()
Traceback (most recent call last): File "", line 1, in AttributeError: 'NoneType' object has no attribute 'groups'
All I need is for the system to go through my xml files and create a csv which has the complete entry of each tag in a column of the csv. It should add columns to the csv if they don't exist and then populate them accordingly.
=========== XML sample
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?>
<anzmeta>
<citeinfo>
<uniqueid />
<title><></title>
<origin>
<custod>ATGIS</custod>
<jurisdic>
<keyword thesaurus="">Tablelands Regional Council</keyword>
</jurisdic>
</origin>
</citeinfo>
<descript>
<abstract><>
</abstract>
<theme>
<keyword thesaurus="">EPSG</keyword>
</theme>
<spdom>
<keyword thesaurus="">GDA94</keyword>
<keyword thesaurus="">GRS80</keyword>
<keyword thesaurus="">Map Grid of Australia</keyword>
<keyword thesaurus="">Zone 55 (144E - 150E)</keyword>
<bounding>
<northbc />
<southbc />
<eastbc />
<westbc />
</bounding>
</spdom>
</descript>
<timeperd>
<begdate>
<date>2012</date>
</begdate>
<enddate>
<keyword thesaurus="">Completed</keyword>
</enddate>
</timeperd>
<status>
<progress>
<keyword thesaurus="">Ongoing</keyword>
<keyword thesaurus="">Completed</keyword>
</progress>
<update>
<keyword thesaurus="">As Required</keyword>
<keyword thesaurus="">As Required</keyword>
</update>
</status>
<distinfo>
<native>
<nondig>
<formname>File</formname>
</nondig>
<digform>
<formname>Type:</formname>
</digform>
</native>
<avlform>
<nondig>
<formname>Format:</formname>
</nondig>
<digform>
<formname>Size</formname>
</digform>
</avlform>
<accconst>Internal Use Only</accconst>
</distinfo>
<dataqual>
<lineage>~TBC~</lineage>
<procstep>
<procdesc Sync="TUE">Metadata imported.</procdesc>
<srcused Sync="TRUE">L:\Data_Admin\MetadataGenerator\trc_Metadata_Template.xml</srcused>
<date Sync="TRUE">20121206</date>
<time Sync="TRUE">15341400</time>
</procstep>
<posacc>~TBC~</posacc>
<attracc>~TBC~</attracc>
<logic>~TBC~</logic>
<complete>~TBC~</complete>
</dataqual>
<cntinfo>
<cntorg>Atherton Tablelands GIS</cntorg>
<cntpos>GIS Coordinator</cntpos>
<address>PO Box 1616, 8 Tolga Rd</address>
<city>Atherton</city>
<state>QLD</state>
<country>AUSTRALIA</country>
<postal>4883</postal>
<cntvoice>07 40918600</cntvoice>
<cntfax>07 40917035</cntfax>
<cntemail>[email protected]</cntemail>
</cntinfo>
<metainfo>
<metd>
<date />
</metd>
</metainfo>
</anzmeta>
--- Start of my script
import os, xml, shutil, datetime
from xml.etree import ElementTree as et
SourceDIR=os.getcwd()
outDIR=os.getcwd()+'//out'
def locatexml(SourceDIR,outDIR):
xmllist=[]
for root, dirs, files in os.walk(SourceDIR, topdown=False):
for fl in files:
currentFile=os.path.join(root, fl)
ext=fl[fl.rfind('.')+1:]
if ext=='xml':
xmllist.append(currentFile)
print currentFile
readxml(currentFile)
print "finished"
return xmllist
def readxml(currentFile):
tree=et.parse(currentFile)
print "Processing: "+str(currentFile)
locatexml(SourceDIR,outDIR)
print xmllist