get values from each itme in xml code with python -


i have xml code:

<item>     <field var="name" type="text-single"><value>jhon</value></field>     <field var="subject" type="text-single"><value>test server</value></field>     <field var="num_users" type="text-single"><value>11</value></field>     <field var="num_max_users" type="text-single"><value>25</value></field>     <field var="is_password_protected" type="boolean"><value>false</value></field>     <field var="is_member_only" type="boolean"><value>false</value></field>     <field var="language" type="text-single"><value>es</value></field>     <field var="location-type" type="list-single"><value>worldwide</value></field>     <field var="location" type="text-single"><value /></field>     <field var="jid" type="jid-single"><value>jhon@example.com</value></field> </item>  <item>     <field var="name" type="text-single"><value>jhon</value></field>     <field var="subject" type="text-single"><value>test server</value></field>     <field var="num_users" type="text-single"><value>11</value></field>     <field var="num_max_users" type="text-single"><value>25</value></field>     <field var="is_password_protected" type="boolean"><value>false</value></field>     <field var="is_member_only" type="boolean"><value>false</value></field>     <field var="language" type="text-single"><value>es</value></field>     <field var="location-type" type="list-single"><value>worldwide</value></field>     <field var="location" type="text-single"><value /></field>     <field var="jid" type="jid-single"><value>jhon@example.com</value></field> </item>  

how can values each item var each value python?


example output:

name: jhone

num_users: 11

is_password_protected: false


hope me. thank you!

please check below tutorial, https://docs.python.org/2/library/xml.etree.elementtree.html#modifying-an-xml-file

import xml.etree.elementtree et tree = et.parse('your.xml') root = tree.getroot()  item in root.findall('field'):      name = item.find('value').text 

please try above sample , let me know feedback

test.xml      <item>         <field var="name" type="text-single"><value>jhon</value></field>         <field var="subject" type="text-single"><value>test server</value></field>         <field var="num_users" type="text-single"><value>11</value></field>         <field var="num_max_users" type="text-single"><value>25</value></field>         <field var="is_password_protected" type="boolean"><value>false</value></field>         <field var="is_member_only" type="boolean"><value>false</value></field>         <field var="language" type="text-single"><value>es</value></field>         <field var="location-type" type="list-single"><value>worldwide</value></field>         <field var="location" type="text-single"><value /></field>         <field var="jid" type="jid-single"><value>jhon@example.com</value></field>     </item>          in [1]: import xml.etree.elementtree et          in [2]: tree = et.parse('test.xml')          in [3]: root = tree.getroot()          in [4]: item in root.findall('field'):             name = item.find('value').text             print name            ...:              jhon         test server         11         25         false         false         es         worldwide         none         jhon@example.com 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -