From 7b8bcbf94269e69d5aab2209a4e72b66eee4ca2e Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 5 Feb 2020 10:41:22 +0530 Subject: All the basic codes added --- example.xml | 23 +++++++++++++++++++++++ new.xml | 22 ++++++++++++++++++++++ parser.py | 20 ++++++++++++++++++++ tree.py | 10 ++++++++++ 4 files changed, 75 insertions(+) create mode 100644 example.xml create mode 100644 new.xml create mode 100644 parser.py create mode 100644 tree.py diff --git a/example.xml b/example.xml new file mode 100644 index 0000000..e05f3c5 --- /dev/null +++ b/example.xml @@ -0,0 +1,23 @@ + + + + 1 + 2008 + 141100 + + + + + 4 + 2011 + 59900 + + + + 68 + 2011 + 13600 + + + + \ No newline at end of file diff --git a/new.xml b/new.xml new file mode 100644 index 0000000..bb41392 --- /dev/null +++ b/new.xml @@ -0,0 +1,22 @@ + + + 3 + 1/1/2011 + 141100 + + + + + 6 + 2011 + 59900 + + + + 70 + 2011 + 13600 + + + + \ 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 diff --git a/tree.py b/tree.py new file mode 100644 index 0000000..efac907 --- /dev/null +++ b/tree.py @@ -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 -- cgit