2

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]

3
  • post how should look the nominal Row value Commented Oct 30, 2017 at 15:15
  • findall with a bare tag only finds elements that are direct children of the element. Try root.findall('.//Row') Commented Oct 30, 2017 at 15:22
  • after it - results is None Commented Oct 30, 2017 at 15:26

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I Fix it, its my mistake
2

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.

https://repl.it/N144/1

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)

Comments

0

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>]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.