Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中解析XML:多个相同的属性_Python_Xml - Fatal编程技术网

在Python中解析XML:多个相同的属性

在Python中解析XML:多个相同的属性,python,xml,Python,Xml,我有一个xml文件: <movie title="Enemy Behind"> <type>War, Thriller</type> <type>WW2</type> <format>DVD</format> <year>2003</year> <rating>PG</rating>

我有一个xml文件:

    <movie title="Enemy Behind">
       <type>War, Thriller</type>
       <type>WW2</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
   </movie>
但使用此代码只能打印其中一个属性,即“战争,颤栗”。另一个表示“WW2”的属性无法打印


我应该使用for循环吗?我已经尝试过了,但出现了一个错误“'Element'对象不可编辑”。

我不知道您正在使用哪个库,但您可以使用以下代码获取XML片段的值:

test.xml

   <movie title="Enemy Behind">
       <type>War, Thriller</type>
       <type>WW2</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
   </movie>
建议阅读:


这说明了什么?打印类型(movie.getElementsByTagName('type'))伙计我正在看你发给我的教程,它们看起来很不错。我正试图想出自己的方法来做这件事。将很快发布我使用的内容。@Nikhilesharma我发布的代码片段应该会对您有所帮助,但强烈建议您阅读参考资料:)如果您认为我在任何方面对您有所帮助,考虑一下接受/接受我的回答:“伙计,我正在尝试不同的东西。我可能会为它做一个新的帖子。”尼希希什沙玛没有写代码>我在最后一句话中演示了用法。如果你的意思真的不同,我建议你编辑你的问题,我可以更新我的答案:)
   <movie title="Enemy Behind">
       <type>War, Thriller</type>
       <type>WW2</type>
       <format>DVD</format>
       <year>2003</year>
       <rating>PG</rating>
       <stars>10</stars>
       <description>Talk about a US-Japan war</description>
   </movie>
import lxml.etree

# Getting the XML root tag... Movie in our case
root = lxml.etree.parse("test.xml").getroot()

# the method "get" returns the value of the XML attribute informed
# as parameter
print(root.get("title"))

# You can iterate over the children of an XML node
for child in root:
    print(child.text) # gets the text value from the children XML nodes

# Or more specifically, for each type, use the method FIND to get
# the XML child node from the current XML node.
node = root.find("name")
if node is not None:
    print(node.text)

# ..Or if you expect more than one node, as it is the case for the tag
# "type", you can use FINDALL which returns all direct children from the current XML node.
nodes = root.findall("type")
for node in nodes:
    print(node.text)