print the same level of xml data with a condition in python
up vote
0
down vote
favorite
I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?
asd.xml
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
asd.py
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
for report_1 in root:
for report_2 in report_1:
if result.tag == 'name':
if result.text == 'asd':
print ?
I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.
I want output
name: asd
description: example description - 1
name: asd
description: example description - 3
python xml
add a comment |
up vote
0
down vote
favorite
I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?
asd.xml
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
asd.py
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
for report_1 in root:
for report_2 in report_1:
if result.tag == 'name':
if result.text == 'asd':
print ?
I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.
I want output
name: asd
description: example description - 1
name: asd
description: example description - 3
python xml
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?
asd.xml
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
asd.py
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
for report_1 in root:
for report_2 in report_1:
if result.tag == 'name':
if result.text == 'asd':
print ?
I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.
I want output
name: asd
description: example description - 1
name: asd
description: example description - 3
python xml
I have an xml file. I didn't make this file so I don't want to make any changes. I want to print the screen by pulling the information that provides certain conditions from the file. The value I want to get is on the same level as the value I'll do with the comparison. So even if I can check the condition, I can't pull the other data. How can I do this?
asd.xml
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
asd.py
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
for report_1 in root:
for report_2 in report_1:
if result.tag == 'name':
if result.text == 'asd':
print ?
I find the ones that are equal to asd by retrieving the value from tag name "name". but I cannot access the same level decription value.
I want output
name: asd
description: example description - 1
name: asd
description: example description - 3
python xml
python xml
asked yesterday
Ali.Turkkan
226
226
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Your file produces an error:
ParseError: junk after document element: line 5, column 0
Should it look like this:
<data>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</data>
You could use the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
flag = False
for report in root:
flag = False
for row in report:
if row.tag == 'name' and row.text == 'asd':
print('name: asd')
flag = True
if flag and row.tag == 'description':
print('description: {}'.format(row.text))
That would provide you with desired result:
name: asd
description: example description - 1
name: asd
description: example description - 3
thank you. its's work fine.
– Ali.Turkkan
yesterday
add a comment |
up vote
1
down vote
What about this?
asd.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</root>
Python3
import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()
for report in root:
if report.find('name').text == "asd":
print("name: ", report.find('name').text)
print("description: ", report.find('description').text)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your file produces an error:
ParseError: junk after document element: line 5, column 0
Should it look like this:
<data>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</data>
You could use the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
flag = False
for report in root:
flag = False
for row in report:
if row.tag == 'name' and row.text == 'asd':
print('name: asd')
flag = True
if flag and row.tag == 'description':
print('description: {}'.format(row.text))
That would provide you with desired result:
name: asd
description: example description - 1
name: asd
description: example description - 3
thank you. its's work fine.
– Ali.Turkkan
yesterday
add a comment |
up vote
1
down vote
accepted
Your file produces an error:
ParseError: junk after document element: line 5, column 0
Should it look like this:
<data>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</data>
You could use the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
flag = False
for report in root:
flag = False
for row in report:
if row.tag == 'name' and row.text == 'asd':
print('name: asd')
flag = True
if flag and row.tag == 'description':
print('description: {}'.format(row.text))
That would provide you with desired result:
name: asd
description: example description - 1
name: asd
description: example description - 3
thank you. its's work fine.
– Ali.Turkkan
yesterday
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your file produces an error:
ParseError: junk after document element: line 5, column 0
Should it look like this:
<data>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</data>
You could use the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
flag = False
for report in root:
flag = False
for row in report:
if row.tag == 'name' and row.text == 'asd':
print('name: asd')
flag = True
if flag and row.tag == 'description':
print('description: {}'.format(row.text))
That would provide you with desired result:
name: asd
description: example description - 1
name: asd
description: example description - 3
Your file produces an error:
ParseError: junk after document element: line 5, column 0
Should it look like this:
<data>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</data>
You could use the following code:
import xml.etree.ElementTree as ET
tree = ET.parse('asd.xml')
root = tree.getroot()
flag = False
for report in root:
flag = False
for row in report:
if row.tag == 'name' and row.text == 'asd':
print('name: asd')
flag = True
if flag and row.tag == 'description':
print('description: {}'.format(row.text))
That would provide you with desired result:
name: asd
description: example description - 1
name: asd
description: example description - 3
answered yesterday
zipa
15.5k21334
15.5k21334
thank you. its's work fine.
– Ali.Turkkan
yesterday
add a comment |
thank you. its's work fine.
– Ali.Turkkan
yesterday
thank you. its's work fine.
– Ali.Turkkan
yesterday
thank you. its's work fine.
– Ali.Turkkan
yesterday
add a comment |
up vote
1
down vote
What about this?
asd.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</root>
Python3
import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()
for report in root:
if report.find('name').text == "asd":
print("name: ", report.find('name').text)
print("description: ", report.find('description').text)
add a comment |
up vote
1
down vote
What about this?
asd.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</root>
Python3
import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()
for report in root:
if report.find('name').text == "asd":
print("name: ", report.find('name').text)
print("description: ", report.find('description').text)
add a comment |
up vote
1
down vote
up vote
1
down vote
What about this?
asd.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</root>
Python3
import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()
for report in root:
if report.find('name').text == "asd":
print("name: ", report.find('name').text)
print("description: ", report.find('description').text)
What about this?
asd.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<report>
<name>asd</name>
<description>example description - 1</description>
</report>
<report>
<name>dsa</name>
<description>example description - 2</description>
</report>
<report>
<name>asd</name>
<description>example description - 3</description>
</report>
</root>
Python3
import xml.etree.ElementTree as ET
tree = ET.parse(r"asd.xml")
root = tree.getroot()
for report in root:
if report.find('name').text == "asd":
print("name: ", report.find('name').text)
print("description: ", report.find('description').text)
answered yesterday
Ettore Rizza
1,8502617
1,8502617
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%2f53372453%2fprint-the-same-level-of-xml-data-with-a-condition-in-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