Reputation: 73
I'm trying to write a Python script that will go through Rows and put them into my database
This is a structure of my xml:
Root>
-<SvcNf>
-<PersonNf>
-<PersonList>
-<Row>
<SysName>MI8</SysName>
<ServerDt>2016-10-28 03:00:12 +03:00</ServerDt>
<UID>9457A55E17341AA7ASDEDS057A8BFFF3</UID>
<PersID>007</PersID>
<Emp_name>James Bond</Emp_name>
<EventID>25</EventID>
<EventTXT>Drinking alcohol</EventTXT>
<CauseEventID>03</CauseEventID>
<CauseEventTXT>Martini with vodka</CauseEventTXT>
<EventBegda>2017-10-18</EventBegda>
<EventEndda>2017-10-18</EventEndda>
<AccrualsSum>171.0</AccrualsSum>
<AccrualsProz>0.0</AccrualsProz>
<AccrualsName>Chinees_</AccrualsName>
<OrderNum>P-336</OrderNum>
<Perg>0</Persg>
<Perk>15</Persk>
<Awart/>
</Row>
-<Row>
.....
</Row>
<Row/>
</PersonList>
</PersonNf>
</SvcNf>
</Root>
So, when i use this code to Parse XML:
import xml.etree.ElementTree as ET
root = ET.parse(events).getroot()
nodes = root.findall("Row")
for node in nodes:
print(node.text)
Result goes to a null
Thanks a lot!
EDIT
The nominal Row value is
['MI8', '2016-10-28 03:00:12 +03:00', '9457A55E17341AA7ASDEDS057A8BFFF3', etc]
Upvotes: 2
Views: 146
Reputation: 4967
Replace node.text
by [child.text for child in node]
. At the end you need just simply use join
function to convert the list into a string.
root = ET.fromstring("""<Root>
<SvcNf>
<PersonNf>
<PersonList>
<Row>
<SysName>MI8</SysName>
<ServerDt>2016-10-28 03:00:12 +03:00</ServerDt>
<UID>9457A55E17341AA7ASDEDS057A8BFFF3</UID>
<PersID>007</PersID>
<Emp_name>James Bond</Emp_name>
<EventID>25</EventID>
<EventTXT>Drinking alcohol</EventTXT>
<CauseEventID>03</CauseEventID>
<CauseEventTXT>Martini with vodka</CauseEventTXT>
<EventBegda>2017-10-18</EventBegda>
<EventEndda>2017-10-18</EventEndda>
<AccrualsSum>171.0</AccrualsSum>
<AccrualsProz>0.0</AccrualsProz>
<AccrualsName>Chinees_</AccrualsName>
<OrderNum>P-336</OrderNum>
<Persg>0</Persg>
<Persk>15</Persk>
<Awart/>
</Row>
<Row/>
</PersonList>
</PersonNf>
</SvcNf>
</Root>""")
nodes = root.findall(".//Row")
for node in nodes:
print node # print node object
print(node.text) # print nothing
print [child.text for child in node] # print child text (as list)
Upvotes: 2
Reputation: 3279
With the XML already sanitised and the XML selector you will have what you want:
events = b'''
<Root>
<SvcNf>
<PersonNf>
<PersonList>
<Row>
<SysName>MI8</SysName>
<ServerDt>2016-10-28 03:00:12 +03:00</ServerDt>
<UID>9457A55E17341AA7ASDEDS057A8BFFF3</UID>
<PersID>007</PersID>
<FIO>James Bond</FIO>
<EventID>25</EventID>
<EventTXT>Drinking alcohol</EventTXT>
<CauseEventID>03</CauseEventID>
<CauseEventTXT>Martini with vodka</CauseEventTXT>
<EventBegda>2017-10-18</EventBegda>
<EventEndda>2017-10-18</EventEndda>
<AccrualsSum>171.0</AccrualsSum>
<AccrualsProz>0.0</AccrualsProz>
<AccrualsName>Chinees_</AccrualsName>
<OrderNum>P-336</OrderNum>
<Perg>0</Perg>
<Perk>15</Perk>
<Awart/>
</Row>
<Row/>
</PersonList>
</PersonNf>
</SvcNf>
</Root>
'''
import xml.etree.ElementTree as ET
root = ET.fromstring(events)
nodes = root.findall(".//Row")
#[<Element 'Row' at 0x00C217E0>, <Element 'Row' at 0x00C216C0>]
Upvotes: 0
Reputation: 4967
You have an invalid XML :
<PersID>007</TabNum>
<Emp_name>James Bond</FIO>
Try to fix your XML :
<PersID>007</PersID>
<Emp_name>James Bond</Emp_name>
Upvotes: 3