How do I get a descendant node using ElementTree when an ancestor mentions namespaces?

Rahul Iyer

I have the following xml:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
    <url>
        <loc>https://news.mycoolsite.com/city/newyork/cat-bites-dog/articleshow/12345.pms</loc>
        <news:news>
            <news:publication>
                <news:name>New York Post</news:name>
                <news:language>en</news:language>
            </news:publication>
            <news:publication_date>2017-12-27T07:23:12+03:30</news:publication_date>
            <news:title>Cat bites dog</news:title>
            <news:keywords>Cat biting,Dog,Fluffy,Pongo,Broadway,Cat attack,</news:keywords>
        </news:news>
        <lastmod>2017-12-27T10:17:04+03:30</lastmod>
        <image:image>
            <image:loc>https://news.mycoolsite.com/city/newyork/cat-bites-dog/photo/12345.pms</image:loc>
        </image:image>
    </url>
</urlset>

How do I get the 'loc' tag using ElementTree ?

I have tried the following:

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
for child in root:
    print(child.tag, child.attrib)
    tags = []
    for loc in child.iter('loc'):
        print loc.tag
        tags.append(list)
    print("Found so many tags:" + str(len(tags)))

But the problem is it doesn't seem to find any tags! What is the problem ? Does it have anything to do with the namespaces used ?

EDIT: If I delete the names spaces, then I seem to find both loc tags. So the problem seems to be I am not specifying the namespaces correctly. But the first loc tag doesn't have a namespace. So how do I specify the namespace correctly?

Rahul Iyer

You have to specify the default namespace:

import xml.etree.ElementTree as ET
tree = ET.parse('data.xml')
root = tree.getroot()
for child in root:
    print(child.tag, child.attrib)
    tags = []
    for loc in child.iter('{http://www.sitemaps.org/schemas/sitemap/0.9}loc'):
        print loc.tag
        tags.append(list)
    print("Found so many tags:" + str(len(tags)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I access the data in an XML node when using ElementTree

From Dev

get the position of a descendant node from ancestor, not necessarily a sibling

From Dev

get the position of a descendant node from ancestor, not necessarily a sibling

From Dev

How do I get node or process id when using doParallel?

From Dev

How do I get a list of MediaWiki namespaces?

From Dev

how do i combine the children of a node into one cell of a csv cell using elementtree in python?

From Dev

ADT Tree - is a node ancestor/descendant of itself?

From Dev

Find descendant elements that do not have a specific ancestor

From Dev

How do I read mentions in fb api?

From Dev

RegEx in Sitemap.xml: How do I delete node if descendant node file type is not html?

From Dev

How do I output an XML file using ElementTree in python?

From Java

How to get the ancestor node from this XML document?

From Dev

How can I click on this child node / descendant using C# Selenium?

From Dev

How do I rearrange the order of the Mentions in a Expression in VersionOne?

From Dev

How can I get 3 user mentions? - discord-js

From Dev

How do I get the environment name when using environ in Clojure?

From Dev

How do I get Promises to work when using Jest CLI?

From Dev

How do I get the path of a resource when using Maven?

From Dev

How do I get rid of the /#/ when using ui-router?

From Dev

How do i get the value of an item in tkinter when using a class?

From Dev

How do I get the path of a resource when using Maven?

From Dev

How do I get the exit status when using the sed command?

From Dev

Parsing XML with namespaces using ElementTree in Python

From Dev

How to select an element if a condition on a descendant of a common ancestor is met?

From Dev

How to get node bounds according to its specific ancestor in JavaFX 8?

From Dev

How do I get Python's ElementTree to pretty print to an XML file?

From Dev

XSLT: How do I match every descendant individually?

From Dev

How can I get size of prototype cell in descendant of UICollectionView?

From Dev

How can I get all descendant Elements for parent container?

Related Related

  1. 1

    How can I access the data in an XML node when using ElementTree

  2. 2

    get the position of a descendant node from ancestor, not necessarily a sibling

  3. 3

    get the position of a descendant node from ancestor, not necessarily a sibling

  4. 4

    How do I get node or process id when using doParallel?

  5. 5

    How do I get a list of MediaWiki namespaces?

  6. 6

    how do i combine the children of a node into one cell of a csv cell using elementtree in python?

  7. 7

    ADT Tree - is a node ancestor/descendant of itself?

  8. 8

    Find descendant elements that do not have a specific ancestor

  9. 9

    How do I read mentions in fb api?

  10. 10

    RegEx in Sitemap.xml: How do I delete node if descendant node file type is not html?

  11. 11

    How do I output an XML file using ElementTree in python?

  12. 12

    How to get the ancestor node from this XML document?

  13. 13

    How can I click on this child node / descendant using C# Selenium?

  14. 14

    How do I rearrange the order of the Mentions in a Expression in VersionOne?

  15. 15

    How can I get 3 user mentions? - discord-js

  16. 16

    How do I get the environment name when using environ in Clojure?

  17. 17

    How do I get Promises to work when using Jest CLI?

  18. 18

    How do I get the path of a resource when using Maven?

  19. 19

    How do I get rid of the /#/ when using ui-router?

  20. 20

    How do i get the value of an item in tkinter when using a class?

  21. 21

    How do I get the path of a resource when using Maven?

  22. 22

    How do I get the exit status when using the sed command?

  23. 23

    Parsing XML with namespaces using ElementTree in Python

  24. 24

    How to select an element if a condition on a descendant of a common ancestor is met?

  25. 25

    How to get node bounds according to its specific ancestor in JavaFX 8?

  26. 26

    How do I get Python's ElementTree to pretty print to an XML file?

  27. 27

    XSLT: How do I match every descendant individually?

  28. 28

    How can I get size of prototype cell in descendant of UICollectionView?

  29. 29

    How can I get all descendant Elements for parent container?

HotTag

Archive