0
  <?xml version="1.0" encoding="UTF-8"?>
        <country:list version="3.0" xmlns:country="http://Some/www.home.com" xmlns:region="http://some/www.hello.com" xmlns:Location="http://Some/www.home.com">
            <country:Region111>
                <Some_child_tags>
                    <region:tag1 name="1">some contents in country:Region111 </region:tag1>

                    <tags>

                            .
                            .
                            .
                            .
                    </tags>
                    <region:tag1 name="2">Some other contents in country:Region111</region:tag1>
                </Some_child_tags>
            </country:Region111>

            <Location:Region222>
            <Some_child_tags>
                    <region:tag1 name="1">some contents in Location:Region222</region:tag1>
                    <tags>

                            .
                            .
                            .
                            .
                    </tags>
                    <region:tag1 name="2">Some other contents in Location:Region222</region:tag1>
                </Some_child_tags>
            </Location:Region222>
        </country:list>

I want to retrieve all the <region:tag1> tag contens and attribute values also that is comming under <country:Region111>...</country:Region111> not under <Location:Region222> ....</Location:Region222>. So the final output should be the following

name 1 some contents in country:Region111                                
name 2 Some other contents in country:Region111
      It should eliminate the <region:tag1> contents that is coming from <Location:Region222>.
4
  • show your efforts Commented Jun 9, 2017 at 6:34
  • 2
    don't post input xml content in comments - move it to the question Commented Jun 9, 2017 at 6:35
  • show how should look the final output considering that there are several region:tag1 tags and they have child tags (how that childs contents should be treated) and including attribute values(1,2) Commented Jun 9, 2017 at 13:55
  • Updated the code with more soecification. Commented Jun 10, 2017 at 4:51

1 Answer 1

1

Your input XML document should look as below (to be valid):

<?xml version="1.0" encoding="UTF-8"?>
<country:list version="3.0" xmlns:country="http://Some/www.home.com" xmlns:region="http://some/www.hello.com">
<country:Region111>
     <Some_child_tags>
      <region:tag1>some contents</region:tag1>
     </Some_child_tags>
</country:Region111>
</country:list>

The solution using xml.etree.ElementTree module:

import xml.etree.ElementTree as ET

tree = ET.parse("yourfile.xml")
root = tree.getroot()
tag1 = root.find('.//{http://some/www.hello.com}tag1')  # accessing tag with namespace

print(tag1.text)

The output:

some contents
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for ur valuable support.
@sudarsan, welcome, you can accept the answer if it has solved the issue
The above given solution is for fetching only the first element,How can we fetch all the contents for <region:tag1>.If the xml file is looking like this. <Some_child_tags> <region:tag1>some contents</region:tag1> . . . . <region:tag1>some other contents</region:tag1> </Some_child_tags> please help.
@sudarsan, update your xml and post the final output into the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.