diff options
-rw-r--r-- | example.xml | 23 | ||||
-rw-r--r-- | new.xml | 22 | ||||
-rw-r--r-- | parser.py | 20 | ||||
-rw-r--r-- | tree.py | 10 |
4 files changed, 75 insertions, 0 deletions
diff --git a/example.xml b/example.xml new file mode 100644 index 0000000..e05f3c5 --- /dev/null +++ b/example.xml @@ -0,0 +1,23 @@ +<?xml version="1.0"?> +<data> + <country name="Liechtenstein"> + <rank>1</rank> + <year>2008</year> + <gdppc>141100</gdppc> + <neighbor name="Austria" direction="E"/> + <neighbor name="Switzerland" direction="W"/> + </country> + <country name="Singapore"> + <rank>4</rank> + <year>2011</year> + <gdppc>59900</gdppc> + <neighbor name="Malaysia" direction="N"/> + </country> + <country name="Panama"> + <rank>68</rank> + <year>2011</year> + <gdppc>13600</gdppc> + <neighbor name="Costa Rica" direction="W"/> + <neighbor name="Colombia" direction="E"/> + </country> +</data>
\ No newline at end of file @@ -0,0 +1,22 @@ +<data> + <country name="Liechtenstein"> + <rank updated="yes">3</rank> + <year>1/1/2011</year> + <gdppc>141100</gdppc> + <neighbor direction="E" name="Austria" /> + <neighbor direction="W" name="Switzerland" /> + </country> + <country name="Singapore"> + <rank updated="yes">6</rank> + <year>2011</year> + <gdppc>59900</gdppc> + <neighbor direction="N" name="Malaysia" /> + </country> + <country name="Panama"> + <rank updated="yes">70</rank> + <year>2011</year> + <gdppc>13600</gdppc> + <neighbor direction="W" name="Costa Rica" /> + <neighbor direction="E" name="Colombia" /> + </country> +</data>
\ No newline at end of file diff --git a/parser.py b/parser.py new file mode 100644 index 0000000..361abe5 --- /dev/null +++ b/parser.py @@ -0,0 +1,20 @@ +import xml.etree.ElementTree as ET + +xmlTree = ET.parse('/home/eric/Documents/example.xml') +xmlRootTag = {elem.tag for elem in xmlTree.iter()} + + +#xmlTree = ET.parse('/home/fossee/Documents/example.xml') +#xmlRootTag = list({elem.tag for elem in xmlTree.iter()}) + +#for xmlChild in xmlRootTag: + #if xmlChild : + # print(xmlChild) + +for xmlChild in xmlRootTag: + if xmlChild == 'to': + xmlChild = 'food' + #print(xmlChild) + #else: + #print(xmlChild) +xmlTree.write('/home/eric/Documents/new.xml')
\ No newline at end of file @@ -0,0 +1,10 @@ +import xml.etree.ElementTree as ET +tree = ET.parse('/home/eric/Documents/example.xml') +root = tree.getroot() +for rank in root.iter('rank'): + new_rank=int(rank.text) + 2 + rank.text = str(new_rank) + rank.set('updated','yes') +tree.find('.//year').text = '1/1/2011' + +tree.write('/home/eric/Documents/new.xml')
\ No newline at end of file |