Hướng dẫn python xml check if element exists - python xml kiểm tra xem phần tử có tồn tại không

Tôi đang phân tích một tệp XML thông qua cây phần tử trong Python và ghi nội dung vào tệp CPP.

Nội dung của thẻ trẻ em sẽ là biến thể cho các thẻ khác nhau. Ví dụ, thẻ sự kiện đầu tiên có thẻ bên là con nhưng thẻ sự kiện thứ hai không có.

-> Làm thế nào tôi có thể kiểm tra xem thẻ có tồn tại hay không trước khi phân tích cú pháp?

-> Trẻ em có thuộc tính giá trị trong thẻ sự kiện thứ 1 nhưng không phải thứ hai. Làm thế nào tôi có thể kiểm tra xem một thuộc tính có tồn tại hay không trước khi lấy giá trị của nó.

-> Hiện tại mã của tôi đã gây ra lỗi cho thẻ bên không hiện có và đặt giá trị thuộc tính "Không" cho thẻ trẻ em thứ hai.

<main> <event> <party>Big</party> <children type="me" value="3"/> </event> <event> <children type="me"/> </event> </main>

Code:

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value')

Tôi muốn kiểm tra các thẻ và sau đó lấy giá trị của chúng.

Hướng dẫn dành cho người mới bắt đầu

Nhiếp ảnh tập trung nông của nhện Web Web của Robert Anasch trên unplash

Tìm hiểu cách bạn có thể phân tích, khám phá, sửa đổi và điền các tệp XML với gói Python ElementTree, cho các vòng lặp và biểu thức XPath. Là một nhà khoa học dữ liệu, bạn sẽ thấy rằng việc hiểu XML rất mạnh mẽ cho cả thực hành web và thực hành chung trong việc phân tích một tài liệu có cấu trúc

Ngôn ngữ đánh dấu có thể mở rộng (XML) là ngôn ngữ đánh dấu mã hóa các tài liệu bằng cách xác định một tập hợp các quy tắc ở cả định dạng có thể đọc được bằng máy và có thể đọc được. Mở rộng từ SGML (Ngôn ngữ đánh dấu tổng quát tiêu chuẩn), nó cho phép chúng tôi mô tả cấu trúc của tài liệu. Trong XML, chúng ta có thể xác định các thẻ tùy chỉnh. Chúng tôi cũng có thể sử dụng XML làm định dạng tiêu chuẩn để trao đổi thông tin.(XML) is a markup language which encodes documents by defining a set of rules in both machine-readable and human-readable format. Extended from SGML (Standard Generalized Markup Language), it lets us describe the structure of the document. In XML, we can define custom tags. We can also use XML as a standard format to exchange information.

  • Các tài liệu XML có các phần, được gọi là các phần tử, được xác định bởi một thẻ bắt đầu và thẻ kết thúc. Một thẻ là một cấu trúc đánh dấu bắt đầu bằng <?xml version="1.0"?>
    <collection>
    <genre category="Action">
    <decade years="1980s">
    <movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
    <format multiple="No">DVD</format>
    <year>1981</year>
    <rating>PG</rating>
    <description>
    'Archaeologist and adventurer Indiana Jones
    is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
    </description>
    </movie>
    <movie favorite="True" title="THE KARATE KID">
    <format multiple="Yes">DVD,Online</format>
    <year>1984</year>
    <rating>PG</rating>
    <description>None provided.</description>
    </movie>
    <movie favorite="False" title="Back 2 the Future">
    <format multiple="False">Blu-ray</format>
    <year>1985</year>
    <rating>PG</rating>
    <description>Marty McFly</description>
    </movie>
    </decade>
    <decade years="1990s">
    <movie favorite="False" title="X-Men">
    <format multiple="Yes">dvd, digital</format>
    <year>2000</year>
    <rating>PG-13</rating>
    <description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
    </movie>
    <movie favorite="True" title="Batman Returns">
    <format multiple="No">VHS</format>
    <year>1992</year>
    <rating>PG13</rating>
    <description>NA.</description>
    </movie>
    <movie favorite="False" title="Reservoir Dogs">
    <format multiple="No">Online</format>
    <year>1992</year>
    <rating>R</rating>
    <description>WhAtEvER I Want!!!?!</description>
    </movie>
    </decade>
    </genre>

    <genre category="Thriller">
    <decade years="1970s">
    <movie favorite="False" title="ALIEN">
    <format multiple="Yes">DVD</format>
    <year>1979</year>
    <rating>R</rating>
    <description>"""""""""</description>
    </movie>
    </decade>
    <decade years="1980s">
    <movie favorite="True" title="Ferris Bueller's Day Off">
    <format multiple="No">DVD</format>
    <year>1986</year>
    <rating>PG13</rating>
    <description>Funny movie on funny guy </description>
    </movie>
    <movie favorite="FALSE" title="American Psycho">
    <format multiple="No">blue-ray</format>
    <year>2000</year>
    <rating>Unrated</rating>
    <description>psychopathic Bateman</description>
    </movie>
    </decade>
    </genre>

    5 và kết thúc bằng <?xml version="1.0"?>
    <collection>
    <genre category="Action">
    <decade years="1980s">
    <movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
    <format multiple="No">DVD</format>
    <year>1981</year>
    <rating>PG</rating>
    <description>
    'Archaeologist and adventurer Indiana Jones
    is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
    </description>
    </movie>
    <movie favorite="True" title="THE KARATE KID">
    <format multiple="Yes">DVD,Online</format>
    <year>1984</year>
    <rating>PG</rating>
    <description>None provided.</description>
    </movie>
    <movie favorite="False" title="Back 2 the Future">
    <format multiple="False">Blu-ray</format>
    <year>1985</year>
    <rating>PG</rating>
    <description>Marty McFly</description>
    </movie>
    </decade>
    <decade years="1990s">
    <movie favorite="False" title="X-Men">
    <format multiple="Yes">dvd, digital</format>
    <year>2000</year>
    <rating>PG-13</rating>
    <description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
    </movie>
    <movie favorite="True" title="Batman Returns">
    <format multiple="No">VHS</format>
    <year>1992</year>
    <rating>PG13</rating>
    <description>NA.</description>
    </movie>
    <movie favorite="False" title="Reservoir Dogs">
    <format multiple="No">Online</format>
    <year>1992</year>
    <rating>R</rating>
    <description>WhAtEvER I Want!!!?!</description>
    </movie>
    </decade>
    </genre>

    <genre category="Thriller">
    <decade years="1970s">
    <movie favorite="False" title="ALIEN">
    <format multiple="Yes">DVD</format>
    <year>1979</year>
    <rating>R</rating>
    <description>"""""""""</description>
    </movie>
    </decade>
    <decade years="1980s">
    <movie favorite="True" title="Ferris Bueller's Day Off">
    <format multiple="No">DVD</format>
    <year>1986</year>
    <rating>PG13</rating>
    <description>Funny movie on funny guy </description>
    </movie>
    <movie favorite="FALSE" title="American Psycho">
    <format multiple="No">blue-ray</format>
    <year>2000</year>
    <rating>Unrated</rating>
    <description>psychopathic Bateman</description>
    </movie>
    </decade>
    </genre>

    6. Các ký tự giữa thẻ bắt đầu và thẻ kết thúc, nếu có bất kỳ, là nội dung của phần tử. Các yếu tố có thể chứa đánh dấu, bao gồm các yếu tố khác, được gọi là "các yếu tố con".elements, defined by a beginning and an ending tag. A tag is a markup construct that begins with <?xml version="1.0"?>
    <collection>
    <genre category="Action">
    <decade years="1980s">
    <movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
    <format multiple="No">DVD</format>
    <year>1981</year>
    <rating>PG</rating>
    <description>
    'Archaeologist and adventurer Indiana Jones
    is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
    </description>
    </movie>
    <movie favorite="True" title="THE KARATE KID">
    <format multiple="Yes">DVD,Online</format>
    <year>1984</year>
    <rating>PG</rating>
    <description>None provided.</description>
    </movie>
    <movie favorite="False" title="Back 2 the Future">
    <format multiple="False">Blu-ray</format>
    <year>1985</year>
    <rating>PG</rating>
    <description>Marty McFly</description>
    </movie>
    </decade>
    <decade years="1990s">
    <movie favorite="False" title="X-Men">
    <format multiple="Yes">dvd, digital</format>
    <year>2000</year>
    <rating>PG-13</rating>
    <description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
    </movie>
    <movie favorite="True" title="Batman Returns">
    <format multiple="No">VHS</format>
    <year>1992</year>
    <rating>PG13</rating>
    <description>NA.</description>
    </movie>
    <movie favorite="False" title="Reservoir Dogs">
    <format multiple="No">Online</format>
    <year>1992</year>
    <rating>R</rating>
    <description>WhAtEvER I Want!!!?!</description>
    </movie>
    </decade>
    </genre>

    <genre category="Thriller">
    <decade years="1970s">
    <movie favorite="False" title="ALIEN">
    <format multiple="Yes">DVD</format>
    <year>1979</year>
    <rating>R</rating>
    <description>"""""""""</description>
    </movie>
    </decade>
    <decade years="1980s">
    <movie favorite="True" title="Ferris Bueller's Day Off">
    <format multiple="No">DVD</format>
    <year>1986</year>
    <rating>PG13</rating>
    <description>Funny movie on funny guy </description>
    </movie>
    <movie favorite="FALSE" title="American Psycho">
    <format multiple="No">blue-ray</format>
    <year>2000</year>
    <rating>Unrated</rating>
    <description>psychopathic Bateman</description>
    </movie>
    </decade>
    </genre>

    5 and ends with <?xml version="1.0"?>
    <collection>
    <genre category="Action">
    <decade years="1980s">
    <movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
    <format multiple="No">DVD</format>
    <year>1981</year>
    <rating>PG</rating>
    <description>
    'Archaeologist and adventurer Indiana Jones
    is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
    </description>
    </movie>
    <movie favorite="True" title="THE KARATE KID">
    <format multiple="Yes">DVD,Online</format>
    <year>1984</year>
    <rating>PG</rating>
    <description>None provided.</description>
    </movie>
    <movie favorite="False" title="Back 2 the Future">
    <format multiple="False">Blu-ray</format>
    <year>1985</year>
    <rating>PG</rating>
    <description>Marty McFly</description>
    </movie>
    </decade>
    <decade years="1990s">
    <movie favorite="False" title="X-Men">
    <format multiple="Yes">dvd, digital</format>
    <year>2000</year>
    <rating>PG-13</rating>
    <description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
    </movie>
    <movie favorite="True" title="Batman Returns">
    <format multiple="No">VHS</format>
    <year>1992</year>
    <rating>PG13</rating>
    <description>NA.</description>
    </movie>
    <movie favorite="False" title="Reservoir Dogs">
    <format multiple="No">Online</format>
    <year>1992</year>
    <rating>R</rating>
    <description>WhAtEvER I Want!!!?!</description>
    </movie>
    </decade>
    </genre>

    <genre category="Thriller">
    <decade years="1970s">
    <movie favorite="False" title="ALIEN">
    <format multiple="Yes">DVD</format>
    <year>1979</year>
    <rating>R</rating>
    <description>"""""""""</description>
    </movie>
    </decade>
    <decade years="1980s">
    <movie favorite="True" title="Ferris Bueller's Day Off">
    <format multiple="No">DVD</format>
    <year>1986</year>
    <rating>PG13</rating>
    <description>Funny movie on funny guy </description>
    </movie>
    <movie favorite="FALSE" title="American Psycho">
    <format multiple="No">blue-ray</format>
    <year>2000</year>
    <rating>Unrated</rating>
    <description>psychopathic Bateman</description>
    </movie>
    </decade>
    </genre>

    6. The characters between the start-tag and end-tag, if there are any, are the element's content. Elements can contain markup, including other elements, which are called "child elements".
  • Phần tử cấp cao nhất, cấp cao nhất được gọi là gốc, chứa tất cả các yếu tố khác.root, which contains all other elements.
  • Các thuộc tính là cặp có giá trị tên tồn tại trong thẻ bắt đầu hoặc thẻ phần tử trống. Một thuộc tính XML chỉ có thể có một giá trị duy nhất và mỗi thuộc tính có thể xuất hiện nhiều nhất một lần trên mỗi phần tử.

Tại đây, một ảnh chụp nhanh <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

7 mà chúng tôi sẽ sử dụng cho hướng dẫn này:

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

Giới thiệu về ElementTree

Cấu trúc cây XML làm cho điều hướng, sửa đổi và loại bỏ tương đối đơn giản theo chương trình. Python có một thư viện, ElementTree tích hợp, có các chức năng để đọc và thao tác XMLS (và các tệp có cấu trúc tương tự khác).ElementTree, that has functions to read and manipulate XMLs (and other similarly structured files).

Đầu tiên, nhập <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

8. Đó là một thực tế phổ biến để sử dụng bí danh của <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

9:

import xml.etree.ElementTree as ET

Phân tích dữ liệu XML

Trong tệp XML được cung cấp, có một bộ phim cơ bản được mô tả. Vấn đề duy nhất là dữ liệu là một mớ hỗn độn! Đã có rất nhiều người phụ trách khác nhau của bộ sưu tập này và mọi người đều có cách nhập dữ liệu riêng vào tệp. Mục tiêu chính trong hướng dẫn này sẽ là đọc và hiểu tệp với Python - sau đó khắc phục các vấn đề.

Đầu tiên bạn cần đọc trong tệp với <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

8.

tree = ET.parse('movies.xml')
root = tree.getroot()

Bây giờ bạn đã khởi tạo cây, bạn nên nhìn vào XML và in ra các giá trị để hiểu cách cấu trúc cây.

root.tag'collection'

Ở cấp cao nhất, bạn thấy rằng XML này được bắt nguồn từ thẻ import xml.etree.ElementTree as ET1.

root.attrib{}

Cho các vòng lặp

Bạn có thể dễ dàng lặp lại trên các phần mềm (thường được gọi là trẻ em trẻ em) trong root bằng cách sử dụng một vòng lặp đơn giản cho vòng lặp.

for child in root:
print(child.tag, child.attrib)
genre {'category': 'Action'}
genre {'category': 'Thriller'}
genre {'category': 'Comedy'}

Bây giờ bạn biết rằng những đứa trẻ của gốc import xml.etree.ElementTree as ET1 đều là import xml.etree.ElementTree as ET3. Để chỉ định thể loại, XML sử dụng thuộc tính import xml.etree.ElementTree as ET4. Có những bộ phim hành động, phim kinh dị và hài kịch theo yếu tố import xml.etree.ElementTree as ET3.

Thông thường, rất hữu ích khi biết tất cả các yếu tố trong toàn bộ cây. Một chức năng hữu ích để làm điều đó là import xml.etree.ElementTree as ET6.

[elem.tag for elem in root.iter()]['collection',
'genre',
'decade',
'movie',
'format',
'year',
'rating',
'description',
'movie',
.
.
.
.
'movie',
'format',
'year',
'rating',
'description']

Có một cách hữu ích để xem toàn bộ tài liệu. Nếu bạn chuyển gốc vào phương thức import xml.etree.ElementTree as ET7, bạn có thể trả lại toàn bộ tài liệu. Trong ElementTree, phương pháp này có một dạng hơi lạ.

Vì ElementTree là một thư viện mạnh mẽ có thể diễn giải nhiều hơn chỉ XML, bạn phải chỉ định cả mã hóa và giải mã tài liệu bạn đang hiển thị dưới dạng chuỗi.

Bạn có thể mở rộng việc sử dụng chức năng import xml.etree.ElementTree as ET8 để giúp tìm các yếu tố cụ thể quan tâm. import xml.etree.ElementTree as ET6 sẽ liệt kê tất cả các phần phụ dưới gốc khớp với phần tử được chỉ định. Ở đây, bạn sẽ liệt kê tất cả các thuộc tính của phần tử tree = ET.parse('movies.xml')
root = tree.getroot()
0 trong cây:

for movie in root.iter('movie'):
print(movie.attrib)
{'favorite': 'True', 'title': 'Indiana Jones: The raiders of the lost Ark'}
{'favorite': 'True', 'title': 'THE KARATE KID'}
{'favorite': 'False', 'title': 'Back 2 the Future'}
{'favorite': 'False', 'title': 'X-Men'}
{'favorite': 'True', 'title': 'Batman Returns'}
{'favorite': 'False', 'title': 'Reservoir Dogs'}
{'favorite': 'False', 'title': 'ALIEN'}
{'favorite': 'True', 'title': "Ferris Bueller's Day Off"}
{'favorite': 'FALSE', 'title': 'American Psycho'}
{'favorite': 'False', 'title': 'Batman: The Movie'}
{'favorite': 'True', 'title': 'Easy A'}
{'favorite': 'True', 'title': 'Dinner for SCHMUCKS'}
{'favorite': 'False', 'title': 'Ghostbusters'}
{'favorite': 'True', 'title': 'Robin Hood: Prince of Thieves'}

Biểu thức XPath

Nhiều lần các yếu tố sẽ không có thuộc tính, chúng sẽ chỉ có nội dung văn bản. Sử dụng thuộc tính tree = ET.parse('movies.xml')
root = tree.getroot()
1, bạn có thể in nội dung này.

Bây giờ, in ra tất cả các mô tả của các bộ phim.

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 0

In XML rất hữu ích, nhưng XPath là ngôn ngữ truy vấn được sử dụng để tìm kiếm thông qua XML nhanh chóng và dễ dàng. Tuy nhiên, việc hiểu XPath là cực kỳ quan trọng đối với việc quét và điền XMLS. <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

8 có chức năng tree = ET.parse('movies.xml')
root = tree.getroot()
3 sẽ đi qua những đứa trẻ ngay lập tức của yếu tố được tham chiếu.

Tại đây, bạn sẽ tìm kiếm cây cho những bộ phim ra mắt vào năm 1992:

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 1

Hàm tree = ET.parse('movies.xml')
root = tree.getroot()
3 luôn bắt đầu tại phần tử được chỉ định. Loại chức năng này cực kỳ mạnh mẽ cho "Tìm và thay thế". Bạn thậm chí có thể tìm kiếm trên các thuộc tính!

Bây giờ, chỉ in ra những bộ phim có sẵn ở nhiều định dạng (một thuộc tính).

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 2

Động não tại sao, trong trường hợp này, câu lệnh in trả về các giá trị của vâng của tree = ET.parse('movies.xml')
root = tree.getroot()
5. Hãy suy nghĩ về cách xác định vòng lặp "cho".

Mẹo: Sử dụng tree = ET.parse('movies.xml')
root = tree.getroot()
6 Bên trong XPath để trả về phần tử cha của phần tử hiện tại.
: use tree = ET.parse('movies.xml')
root = tree.getroot()
6 inside of XPath to return the parent element of the current element.

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 3

Sửa đổi XML

Trước đó, các tựa phim là một mớ hỗn độn tuyệt đối. Bây giờ, in lại chúng:

for movie in root.iter('movie'):
print(movie.attrib)
{'favorite': 'True', 'title': 'Indiana Jones: The raiders of the lost Ark'}
{'favorite': 'True', 'title': 'THE KARATE KID'}
{'favorite': 'False', 'title': 'Back 2 the Future'}
{'favorite': 'False', 'title': 'X-Men'}
{'favorite': 'True', 'title': 'Batman Returns'}
{'favorite': 'False', 'title': 'Reservoir Dogs'}
{'favorite': 'False', 'title': 'ALIEN'}
{'favorite': 'True', 'title': "Ferris Bueller's Day Off"}
{'favorite': 'FALSE', 'title': 'American Psycho'}
{'favorite': 'False', 'title': 'Batman: The Movie'}
{'favorite': 'True', 'title': 'Easy A'}
{'favorite': 'True', 'title': 'Dinner for SCHMUCKS'}
{'favorite': 'False', 'title': 'Ghostbusters'}
{'favorite': 'True', 'title': 'Robin Hood: Prince of Thieves'}

Khắc phục ’2, trong tương lai. Tương lai. Đó nên là một vấn đề tìm thấy và thay thế. Viết mã để tìm tiêu đề ‘Back 2 The Future, và lưu nó dưới dạng biến:

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 5

Lưu ý rằng sử dụng phương thức tree = ET.parse('movies.xml')
root = tree.getroot()
7 trả về một phần tử của cây. Phần lớn thời gian, nó hữu ích hơn khi chỉnh sửa nội dung trong một phần tử.

Sửa đổi thuộc tính tree = ET.parse('movies.xml')
root = tree.getroot()
8 của Back 2 Biến phần tử trong tương lai để đọc "trở lại tương lai". Sau đó, in ra các thuộc tính của biến của bạn để xem sự thay đổi của bạn. Bạn có thể dễ dàng thực hiện điều này bằng cách truy cập thuộc tính của một phần tử và sau đó gán một giá trị mới cho nó:

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 6

Viết lại các thay đổi của bạn trở lại XML để chúng được sửa vĩnh viễn trong tài liệu. In lại các thuộc tính phim của bạn một lần nữa để đảm bảo các thay đổi của bạn hoạt động. Sử dụng phương thức tree = ET.parse('movies.xml')
root = tree.getroot()
9 để làm điều này:

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 7

Sửa các thuộc tính

Thuộc tính tree = ET.parse('movies.xml')
root = tree.getroot()
5 không chính xác ở một số nơi. Sử dụng <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

8 để sửa lỗi chỉ định dựa trên số lượng bộ phim xuất hiện. Đầu tiên, in root.tag'collection'2Attribution và văn bản để xem phần nào cần được sửa.

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 8

Có một số công việc cần phải được thực hiện trên thẻ này.

Bạn có thể sử dụng Regex để tìm dấu phẩy - điều đó sẽ cho biết liệu thuộc tính tree = ET.parse('movies.xml')
root = tree.getroot()
5 nên là "có" hoặc "không". Thêm và sửa đổi các thuộc tính có thể được thực hiện dễ dàng với root.tag'collection'4Method.

import xml.etree.ElementTree as ET tree = ET.parse('party.xml') root = tree.getroot() for event in root.findall('event'): parties = event.find('party').text children = event.get('value') 9

Các yếu tố di chuyển

Một số dữ liệu đã được đặt trong thập kỷ sai. Sử dụng những gì bạn đã học về XML và <?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

8 để tìm và sửa lỗi dữ liệu thập kỷ.

Sẽ rất hữu ích khi in ra cả thẻ root.tag'collection'6 và thẻ root.tag'collection'7 trong toàn bộ tài liệu.

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

0

Hai năm trong thập kỷ sai là những bộ phim từ những năm 2000. Tìm hiểu những bộ phim đó là gì, sử dụng biểu thức XPath.

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

1

Bạn phải thêm một thẻ thập kỷ mới, những năm 2000, vào thể loại hành động để di chuyển dữ liệu X-Men. Phương pháp root.tag'collection'8 có thể được sử dụng để thêm thẻ này vào cuối XML.

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

2

Bây giờ hãy nối bộ phim X-Men vào những năm 2000 và loại bỏ nó khỏi những năm 1990, lần lượt sử dụng root.tag'collection'9 và root.attrib{}0.

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

3

Xây dựng tài liệu XML

Tốt, vì vậy về cơ bản bạn đã có thể chuyển toàn bộ một bộ phim sang một thập kỷ mới. Lưu các thay đổi của bạn trở lại XML.

<?xml version="1.0"?>
<collection>
<genre category="Action">
<decade years="1980s">
<movie favorite="True" title="Indiana Jones: The raiders of the lost Ark">
<format multiple="No">DVD</format>
<year>1981</year>
<rating>PG</rating>
<description>
'Archaeologist and adventurer Indiana Jones
is hired by the U.S. government to find the Ark of the Covenant before the Nazis.'
</description>
</movie>
<movie favorite="True" title="THE KARATE KID">
<format multiple="Yes">DVD,Online</format>
<year>1984</year>
<rating>PG</rating>
<description>None provided.</description>
</movie>
<movie favorite="False" title="Back 2 the Future">
<format multiple="False">Blu-ray</format>
<year>1985</year>
<rating>PG</rating>
<description>Marty McFly</description>
</movie>
</decade>
<decade years="1990s">
<movie favorite="False" title="X-Men">
<format multiple="Yes">dvd, digital</format>
<year>2000</year>
<rating>PG-13</rating>
<description>Two mutants come to a private academy for their kind whose resident superhero team must oppose a terrorist organization with similar powers.</description>
</movie>
<movie favorite="True" title="Batman Returns">
<format multiple="No">VHS</format>
<year>1992</year>
<rating>PG13</rating>
<description>NA.</description>
</movie>
<movie favorite="False" title="Reservoir Dogs">
<format multiple="No">Online</format>
<year>1992</year>
<rating>R</rating>
<description>WhAtEvER I Want!!!?!</description>
</movie>
</decade>
</genre>

<genre category="Thriller">
<decade years="1970s">
<movie favorite="False" title="ALIEN">
<format multiple="Yes">DVD</format>
<year>1979</year>
<rating>R</rating>
<description>"""""""""</description>
</movie>
</decade>
<decade years="1980s">
<movie favorite="True" title="Ferris Bueller's Day Off">
<format multiple="No">DVD</format>
<year>1986</year>
<rating>PG13</rating>
<description>Funny movie on funny guy </description>
</movie>
<movie favorite="FALSE" title="American Psycho">
<format multiple="No">blue-ray</format>
<year>2000</year>
<rating>Unrated</rating>
<description>psychopathic Bateman</description>
</movie>
</decade>
</genre>

4

Sự kết luận

ElementTree là một thư viện Python quan trọng cho phép bạn phân tích và điều hướng một tài liệu XML. Sử dụng ElementTree phá vỡ tài liệu XML trong cấu trúc cây dễ hoạt động. Khi nghi ngờ, hãy in nó ra (root.attrib{}1) - Sử dụng câu lệnh in hữu ích này để xem toàn bộ tài liệu XML cùng một lúc.

Người giới thiệu

  • Bài viết gốc như được xuất bản bởi Steph Howson: DataCamp
  • Tài liệu Python 3: ElementTree
  • Wikipedia: XML

Làm cách nào để tìm một thẻ cụ thể trong XML trong Python?

Sử dụng XML ...
cây = ElementTree. Parse ("Sample.xml").
root = cây. getroot ().
Chó = Root. Findall ("Dog").
cho chó ở chó:.
In (chó. Văn bản).

Elementtree trong Python là gì?

ElementTree là một thư viện Python quan trọng cho phép bạn phân tích và điều hướng một tài liệu XML.Sử dụng ElementTree phá vỡ tài liệu XML trong cấu trúc cây dễ hoạt động.an important Python library that allows you to parse and navigate an XML document. Using ElementTree breaks down the XML document in a tree structure that is easy to work with.

Làm thế nào để bạn đi qua XML trong Python?

Để phân tích tài liệu XML, bạn cần có toàn bộ tài liệu trong bộ nhớ ...
Để phân tích tài liệu XML ..
Nhập xml.dom.minidom ..
Sử dụng chức năng parse parse để phân tích cú pháp tài liệu (doc = xml.dom.minidom.parse (tên tệp) ;.
Gọi danh sách các thẻ XML từ tài liệu XML bằng mã (= doc.getelementsByTagName (tên tên của thẻ XML thẻ).

Làm thế nào để bạn thêm thẻ XML trong Python?

Thêm chúng bằng hàm subelement () và xác định thuộc tính văn bản của nó ...
con = xml.Phần tử ("nhân viên") nm = xml.Subelement (trẻ em, "Tên") nm.Văn bản = Sinh viên.....
Nhập XML.Etree.ElementTree là Et Tree = et.ElementTree (file = 'nhân viên.xml') root = cây.....
Nhập XML.Etree.ElementTree là Et Tree = et ..

Chủ đề