简体   繁体   中英

using python to convert XML to Json and trigger a post API request

im lookning for a solution to convert XML to Json and use the Json as the payload for post request.

I'm aiming for the following logic:

  1. search for all root.listing.scedules.s and parse @s @d @p @c.

  2. in root.listing.programs parse @t [p.id = @p (from scedules)] ->"Prime Discussion"

3, in root.listing.channels parse @c [c.id = @c (from scedules)] -> "mychannel"

  1. once I have all the info parsed, I want to build a JSON containing all the params and send it using post request

I also look for a solution which will trigger multiple post APIs as the number of root.listing.scedules.s elements

{
"time":"{@s}",
"durartion":"{@d}",
"programID":"{@p}",
"title":"{@t}",
"channelName":"{@c}",
}

<?xml version="1.0" encoding="UTF-8"?>
<root>
<listings>
<schedules>
<s s="2019-09-26T00:00:00" d="1800" p="1569735" c="100007">
<f id="3" />
</s>
</schedules>
<programs>
<p id="1569735" t="Prime Discussion" d="Discussion on Current Affairs." rd="Discussion on Current Affairs." l="en">
<f id="2" />
<f id="21" />
<k id="6" v="20160614" />
<k id="1" v="2450548" />
<k id="18" v="12983658" />
<k id="21" v="12983658" />
<k id="10" v="Program" />
<k id="19" v="SH024505480000" />
<k id="20" v="http://tmsimg.com/assets/p12983658_b_h5_aa.jpg" />
<c id="607" />
<r o="1" r="1" n="100" />
<r o="2" r="1" n="1000" />
<r o="3" r="1" n="10000" />
</p>
</programs>
</listings>
<channels>
<c id="100007" c="mychannel" l="Prime Asia TV SD" d="Prime Asia TV SD" t="Digital" iso639="hi" />
<c id="10035" c="AETV" l="A&amp;amp;E Canada" d="A&amp;amp;E Canada" t="Digital" u="WWW.AETV.COM" iso639="en" />
</channels>
</root>

currently, i use this code to parse the scedules.s elements (part 1) and need some help with parts 2,3,4

import xml.etree.ElementTree as ET
tree = ET.parse('ChannelsProgramsTest.xml')
root = tree.getroot()
for sched in root[0][0].findall('s'):
    new = sched.get('s'),sched.get('p'),sched.get('d'),sched.get('c')
    print(new)

Below (I think it is the core solution you were looking for)

import xml.etree.ElementTree as ET

xml = '''<?xml version="1.0" encoding="UTF-8"?>
<root>
   <listings>
      <schedules>
         <s s="2019-09-26T00:00:00" d="1800" p="1569735" c="100007">
            <f id="3" />
         </s>
      </schedules>
      <programs>
         <p id="1569735" t="Prime Discussion" d="Discussion on Current Affairs." rd="Discussion on Current Affairs." l="en">
            <f id="2" />
            <f id="21" />
            <k id="6" v="20160614" />
            <k id="1" v="2450548" />
            <k id="18" v="12983658" />
            <k id="21" v="12983658" />
            <k id="10" v="Program" />
            <k id="19" v="SH024505480000" />
            <k id="20" v="http://tmsimg.com/assets/p12983658_b_h5_aa.jpg" />
            <c id="607" />
            <r o="1" r="1" n="100" />
            <r o="2" r="1" n="1000" />
            <r o="3" r="1" n="10000" />
         </p>
      </programs>
   </listings>
   <channels>
      <c id="100007" c="mychannel" l="Prime Asia TV SD" d="Prime Asia TV SD" t="Digital" iso639="hi" />
      <c id="10035" c="AETV" l="A&amp;amp;E Canada" d="A&amp;amp;E Canada" t="Digital" u="WWW.AETV.COM" iso639="en" />
   </channels>
</root>'''

tree = ET.fromstring(xml)
listings = tree.findall('.//listings')
for entry in listings:
    # This is the first requirement: find s,d,p,c under 's' element
    s = entry.find('./schedules/s')
    print(s.attrib)
    # now that we have s,d,p,c we can move on and look for the program with a specific id
    program = entry.find("./programs/p[@id='{}']".format(s.attrib['p']))
    print(program.attrib['t'])
    # find the channel
    channel = tree.find(".//channels/c[@id='{}']".format(s.attrib['c']))
    print(channel.attrib['c'])

output

{'s': '2019-09-26T00:00:00', 'd': '1800', 'p': '1569735', 'c': '100007'}
Prime Discussion
mychannel

I'm still somewhat new to Stackoverflow in usage but not years. I think this is a somewhat duplicate question but I do not know how to tag this as duplicate yet.


A very good explanation of XML to JSON via Python is in the following post by the author of the library suggested.


Converting XML to JSON using Python?


The data source may have unknown characters in it that you will need to code for if you don't use a library


ie, newlines, unicode characters, other 'stray' characters. Often libraries will have done this for you already and you don't have to re-invent the wheel.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM