i'm attempting to extract some data from a XML file and create a Excel with the information.
XML File:
<UniversalTransaction>
<TransactionInfo>
<DataContext>
<DataSourceCollection>
<DataSource>
<Type>AccountingInvoice</Type>
<Key>AR INV 00001006</Key>
</DataSource>
</DataSourceCollection>
<Company>
<Code>DCL</Code>
<Country>
<Code>CL</Code>
<Name>Chile</Name>
</Country>
<Name>Your Chile Corp</Name>
</Company>
...etc
Then I made this Code in python 2.7
import xml.etree.ElementTree as ET
import xlwt
from datetime import datetime
tree = ET.parse('ar.xml')
root = tree.getroot()
#extract xml
invoice = root.findall('DataSource')
arinv = root.find('Key').text
country = root.findall('Company')
ctry = root.find('Name').text
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, arinv)
ws.write(0, 1, ctry)
wb.save('example2.xls')
But I get this error:
arinv = root.find('Key').text
'NoneType' object has no attribute 'text'
And i guess it will be the same with
ctry = root.find('Name').text
Also when I change the "extract xml" part of the code to this
for ar in root.findall('DataContext'):
nro = []
ctry = []
inv = ar.find('Key').text
nro.append(inv)
country = ar.find('Name').text
ctry.append(country)
i get the following error:
ws.write(0, 0, arinv)
name 'arinv' is not defined
then again, I guess its the same with "ctry"
Windows 10, python 2.7
I'll apreciate any help, thanks.
It is better to ask shortened questions - without yours bunch of context code. Probably you find a solution yourself when you carefully try to split out exact short question.
According to the docs, Element.find
basically finds only in direct children. You need to use some XPath (look about XPath expressions in the docs) like
root.findall('.//Key')[0].text
(given with assumption the Key always exists, contains text and unique within a document; ie without validation)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.