Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- xml_str = """<entry>
- <id>com.scur.type.string.1846</id>
- <title>Global Bobo</title>
- <type>com.string</type>
- <listType>string</listType>
- <link href="https://internalonly" rel="self"/>
- <content>
- <list version="1">
- <description>SmartL both sides of the entry</description>
- <content>
- <listEntry>
- <entry>aaaaaaaaaaaaaaaaa</entry>
- <description>description for aaaaaaaaaaaaaaaa</description>
- </listEntry>
- <listEntry>
- <entry>bbbbbbbbbbbbbbbb</entry>
- <description />
- </listEntry>
- <listEntry>
- <entry>ADDEDVIAREST.COM</entry>
- <description />
- </listEntry>
- <listEntry>
- <entry>ADDEDVIA-PYTHON-REST.COM</entry>
- <description />
- </listEntry>
- </content>
- </list>
- </content>
- </entry>"""
- # build the tree
- tree = ET.fromstring(xml_str)
- # create the file structure
- data = ET.Element('listEntry')
- entry = ET.SubElement(data, 'entry')
- description = ET.SubElement(data, 'description')
- entry.text = 'blabla.com'
- description.text = 'weehaaw'
- for row in tree.iterfind('.//content'):
- row.append(data)
- print(ET.tostring(tree))
- for row in tree.iterfind('.//content'):
- row.append(data)
- content = tree.findall('.//content')[1] # Just get the second instance.
- data = ET.SubElement(content, 'listEntry')
- entry = ET.SubElement(data, 'entry')
- description = ET.SubElement(data, 'description')
- entry.text = 'blabla.com'
- description.text = 'weehaaw'
- <entry>
- <id>com.scur.type.string.1846</id>
- <title>Global Bobo</title>
- <type>com.string</type>
- <listType>string</listType>
- <link href="https://internalonly" rel="self" />
- <content>
- <list version="1">
- <description>SmartL both sides of the entry</description>
- <content>
- <listEntry>
- <entry>aaaaaaaaaaaaaaaaa</entry>
- <description>description for aaaaaaaaaaaaaaaa</description>
- </listEntry>
- <listEntry>
- <entry>bbbbbbbbbbbbbbbb</entry>
- <description />
- </listEntry>
- <listEntry>
- <entry>ADDEDVIAREST.COM</entry>
- <description />
- </listEntry>
- <listEntry>
- <entry>ADDEDVIA-PYTHON-REST.COM</entry>
- <description />
- </listEntry>
- <listEntry>
- <entry>blabla.com</entry>
- <description>weehaaw</description>
- </listEntry>
- </content>
- </list>
- </content>
- </entry>
Add Comment
Please, Sign In to add comment