简体   繁体   中英

printing child nodes along with their xml tags in python

I have a file called m.xml which has the following content:

<volume name="sp" type="span" operation="create">
    <driver>HDD1</driver>
    <driver>HDD2</driver>
    <driver>HDD3</driver>
    <driver>HDD4</driver>
</volume>

I would like to get result as follows:

<driver>HDD1</driver>
<driver>HDD2</driver>
<driver>HDD3</driver>
<driver>HDD4</driver>

I am trying to use the following code

import xml.etree.ElementTree as ET
root = ET.parse('m.xml')
for nod in root.findall("./driver"):
    print nod.text

I am getting the following result:

HDD1
HDD2
HDD3
HDD4

How do I get the tags also and not just the textual values?

To show the element as XML text, use the ElementTree.tostring() function :

import xml.etree.ElementTree as ET
root = ET.parse('m.xml')
for nod in root.findall("./driver"):
    print ET.tostring(nod)

Demo:

>>> import xml.etree.ElementTree as ET
>>> root = ET.fromstring('''\
... <volume name="sp" type="span" operation="create">
...     <driver>HDD1</driver>
...     <driver>HDD2</driver>
...     <driver>HDD3</driver>
...     <driver>HDD4</driver>
... </volume>
... ''')
>>> for nod in root.findall("./driver"):
...     print ET.tostring(nod)
... 
<driver>HDD1</driver>

<driver>HDD2</driver>

<driver>HDD3</driver>

<driver>HDD4</driver>

Use BeautifulSoup to parse XML. It's very simple:

from bs4 import BeautifulSoup as Soup

with open("sample.xml", "r") as f:
    target_xml = f.read()

# create a `Soup` object
soup = Soup(target_xml, "xml")                                                                                                        

# loop through all <driver> returned as a list and prints all 
for d in soup.find_all("driver"):
    print(d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM