Read an xml element using python
up vote
-1
down vote
favorite
I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.
for example:
<Actions>
<ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
<ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
</Actions>
I am looking for the word :"ExtAction", and since it is inside the Actions
element I want to copy all of it. How can I do it?
python xml
add a comment |
up vote
-1
down vote
favorite
I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.
for example:
<Actions>
<ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
<ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
</Actions>
I am looking for the word :"ExtAction", and since it is inside the Actions
element I want to copy all of it. How can I do it?
python xml
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.
for example:
<Actions>
<ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
<ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
</Actions>
I am looking for the word :"ExtAction", and since it is inside the Actions
element I want to copy all of it. How can I do it?
python xml
I have an xml file. I want to search for a specific word in the file, and if i find it- i want to copy all of the xml element the word was in it.
for example:
<Actions>
<ActionGroup enabled="yes" name="viewsGroup" isExclusive="yes"/>
<ExtAction iconSet="" toolTip="" name="f5-script" text="f5-script"/>
</Actions>
I am looking for the word :"ExtAction", and since it is inside the Actions
element I want to copy all of it. How can I do it?
python xml
python xml
edited Nov 10 at 16:00
narendra-choudhary
2,19121631
2,19121631
asked Nov 10 at 14:52
ASOLOMO
123
123
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything
As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
tags = [child.tag for child in root]
file_contents =
for tag in tags:
for p in tree.iter(tag=tag):
file_contents[tag] = dict(p.items())
If you print the file contents you will get:
"'ActionGroup': 'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes', 'ExtAction': 'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'"
From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:
print(file_contents['ExtAction']['name']) # or save this as a variable if you need it
Hope this helps!
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything
As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
tags = [child.tag for child in root]
file_contents =
for tag in tags:
for p in tree.iter(tag=tag):
file_contents[tag] = dict(p.items())
If you print the file contents you will get:
"'ActionGroup': 'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes', 'ExtAction': 'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'"
From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:
print(file_contents['ExtAction']['name']) # or save this as a variable if you need it
Hope this helps!
add a comment |
up vote
0
down vote
I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything
As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
tags = [child.tag for child in root]
file_contents =
for tag in tags:
for p in tree.iter(tag=tag):
file_contents[tag] = dict(p.items())
If you print the file contents you will get:
"'ActionGroup': 'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes', 'ExtAction': 'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'"
From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:
print(file_contents['ExtAction']['name']) # or save this as a variable if you need it
Hope this helps!
add a comment |
up vote
0
down vote
up vote
0
down vote
I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything
As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
tags = [child.tag for child in root]
file_contents =
for tag in tags:
for p in tree.iter(tag=tag):
file_contents[tag] = dict(p.items())
If you print the file contents you will get:
"'ActionGroup': 'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes', 'ExtAction': 'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'"
From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:
print(file_contents['ExtAction']['name']) # or save this as a variable if you need it
Hope this helps!
I usually use ElementTree for this kind of job, as it seems the most intuitive to me. I believe this is part of the standard library, so no need to install anything
As a more general approach, the entire .xml file can be parsed as a dictionary of dictionaries, which you can then index accordingly if you so desired. This can be done like this (I just made a copy of your .xml file locally and called it "test.xml" for demonstration purposes. Of course, change this to correspond to your file if you choose this solution):
import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()
tags = [child.tag for child in root]
file_contents =
for tag in tags:
for p in tree.iter(tag=tag):
file_contents[tag] = dict(p.items())
If you print the file contents you will get:
"'ActionGroup': 'enabled': 'yes', 'name': 'viewsGroup', 'isExclusive': 'yes', 'ExtAction': 'iconSet': '', 'toolTip': '', 'name': 'f5-script', 'text': 'f5-script'"
From this it is trivial to index out the bits of information you need. For example, if you want to get the name value from the ExtAction tag, you would just do:
print(file_contents['ExtAction']['name']) # or save this as a variable if you need it
Hope this helps!
answered Nov 10 at 15:57
J.Aluko
163
163
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240127%2fread-an-xml-element-using-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown