Hướng dẫn how to change root element in xml using python - cách thay đổi phần tử gốc trong xml bằng python

Tôi có một tệp trông như thế này:

Show
<?xml version="1.0"etc>
<xliff version="1.2"  etc>
<file datatype="plaintext" mt="eMT-R2" original="" source-language="en-US" target-language="es">
<header/>
<body>
    <trans-unit etc>
        <source>blabla</source>
        <target>blabla</target>
        <note>blabla</note>
    </trans-unit>
</body>
</file>
</xliff>

Tôi muốn đi qua các yếu tố nguồn và mục tiêu. Mã của tôi chỉ hoạt động nếu tôi có

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
5 dưới dạng gốc. Có cách nào để bỏ qua 4 phần tử đầu tiên ở đầu tệp hay chỉ đặt gốc thành
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
5?

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)

Hướng dẫn how to change root element in xml using python - cách thay đổi phần tử gốc trong xml bằng python

Pseyfert

2.9533 huy hiệu vàng21 Huy hiệu bạc46 Huy hiệu đồng3 gold badges21 silver badges46 bronze badges

Đã hỏi ngày 21 tháng 1 năm 2017 lúc 19:13Jan 21, 2017 at 19:13

Hướng dẫn how to change root element in xml using python - cách thay đổi phần tử gốc trong xml bằng python

1

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
7 của ElementTree có chuỗi quasi-XPath. Nó không phải là một xpath có tính năng đầy đủ như có sẵn với
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
8 nhưng hoạt động cho những gì bạn cần

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)

Đã trả lời ngày 21 tháng 1 năm 2017 lúc 21:30Jan 21, 2017 at 21:30

3

OK, vì vậy nó hóa ra vấn đề không có trong mã mà là trong tệp của tôi. Đối với bất kỳ ai làm việc với các tệp XLIFF, điều này có thể hữu ích:

Vấn đề nằm trong "XMLNS" - nếu bạn xóa ít nhất một chữ cái, tệp sẽ được phân tích cú pháp chính xác. Tôi không chắc chính xác vấn đề là gì, nhưng thay đổi điều này chắc chắn giải quyết được vấn đề

Đã trả lời ngày 27 tháng 1 năm 2017 lúc 19:40Jan 27, 2017 at 19:40

Hướng dẫn how to change root element in xml using python - cách thay đổi phần tử gốc trong xml bằng python

Mã nguồn: lib/xml/etree/ElementTree.py Lib/xml/etree/ElementTree.py


Mô -đun

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
9 thực hiện API đơn giản và hiệu quả để phân tích dữ liệu XML và tạo dữ liệu XML.

Thay đổi trong phiên bản 3.3: Mô -đun này sẽ sử dụng triển khai nhanh bất cứ khi nào có sẵn.This module will use a fast implementation whenever available.

Không dùng nữa kể từ phiên bản 3.3: Mô -đun

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
0 được không dùng nữa.The
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
0 module is deprecated.

Hướng dẫn¶

Đây là một hướng dẫn ngắn để sử dụng

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
9 (ngắn gọn ____42). Mục tiêu là để chứng minh một số khối xây dựng và các khái niệm cơ bản của mô -đun.

Cây XML và các yếu tố

XML là một định dạng dữ liệu phân cấp vốn có và cách tự nhiên nhất để thể hiện nó là với một cây.

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
2 có hai lớp cho mục đích này -
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4 đại diện cho toàn bộ tài liệu XML dưới dạng cây và
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 đại diện cho một nút duy nhất trong cây này. Các tương tác với toàn bộ tài liệu (đọc và ghi vào/từ các tệp) thường được thực hiện ở cấp độ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4. Các tương tác với một phần tử XML duy nhất và các phần tử phụ của nó được thực hiện ở cấp độ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

Phân tích cú pháp xml¶

Chúng tôi sẽ sử dụng tài liệu XML sau làm dữ liệu mẫu cho phần này:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

Chúng tôi có thể nhập dữ liệu này bằng cách đọc từ một tệp:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

Hoặc trực tiếp từ một chuỗi:

root = ET.fromstring(country_data_as_string)

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
8 phân tích XML từ một chuỗi trực tiếp vào
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5, là phần tử gốc của cây được phân tích cú pháp. Các chức năng phân tích cú pháp khác có thể tạo ra
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4. Kiểm tra tài liệu để chắc chắn.

Là một

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5,
root = ET.fromstring(country_data_as_string)
2 có thẻ và từ điển thuộc tính:

>>> root.tag
'data'
>>> root.attrib
{}

Nó cũng có các nút trẻ em mà chúng ta có thể lặp lại:

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}

Trẻ em được lồng và chúng ta có thể truy cập các nút trẻ cụ thể bằng chỉ mục:

>>> root[0][1].text
'2008'

Ghi chú

Không phải tất cả các yếu tố của đầu vào XML sẽ kết thúc dưới dạng các phần tử của cây phân tích cú pháp. Hiện tại, mô -đun này bỏ qua mọi nhận xét XML, hướng dẫn xử lý và khai báo loại tài liệu trong đầu vào. Tuy nhiên, các cây được xây dựng bằng API mô -đun này thay vì phân tích cú pháp từ văn bản XML có thể có nhận xét và hướng dẫn xử lý trong đó; Chúng sẽ được bao gồm khi tạo ra đầu ra XML. Một khai báo loại tài liệu có thể được truy cập bằng cách chuyển một thể hiện tùy chỉnh ____53 cho hàm tạo

root = ET.fromstring(country_data_as_string)
4.

Kéo API cho phân tích cú pháp không chặn

Hầu hết các chức năng phân tích cú pháp được cung cấp bởi mô -đun này yêu cầu toàn bộ tài liệu được đọc cùng một lúc trước khi trả lại bất kỳ kết quả nào. Có thể sử dụng

root = ET.fromstring(country_data_as_string)
4 và dữ liệu dữ liệu vào đó, nhưng đó là API đẩy gọi các phương thức trên mục tiêu gọi lại, mức độ quá thấp và bất tiện cho hầu hết các nhu cầu. Đôi khi những gì người dùng thực sự muốn là có thể phân tích XML tăng dần, mà không chặn các hoạt động, trong khi tận hưởng sự thuận tiện của các đối tượng
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 được xây dựng đầy đủ.

Công cụ mạnh mẽ nhất để làm điều này là

root = ET.fromstring(country_data_as_string)
7. Nó không yêu cầu đọc chặn để có được dữ liệu XML và thay vào đó được cung cấp với dữ liệu tăng dần với các cuộc gọi
root = ET.fromstring(country_data_as_string)
8. Để có được các phần tử XML được phân tích cú pháp, hãy gọi
root = ET.fromstring(country_data_as_string)
9. Đây là một ví dụ:

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end

Trường hợp sử dụng rõ ràng là các ứng dụng hoạt động theo kiểu không chặn trong đó dữ liệu XML được nhận từ ổ cắm hoặc đọc tăng dần từ một số thiết bị lưu trữ. Trong những trường hợp như vậy, chặn đọc là không thể chấp nhận được.

Bởi vì nó rất linh hoạt,

root = ET.fromstring(country_data_as_string)
7 có thể bất tiện để sử dụng cho các trường hợp sử dụng đơn giản hơn. Nếu bạn không quan tâm đến việc chặn ứng dụng của mình khi đọc dữ liệu XML nhưng vẫn muốn có khả năng phân tích cú pháp gia tăng, hãy xem
>>> root.tag
'data'
>>> root.attrib
{}
1. Nó có thể hữu ích khi bạn đọc một tài liệu XML lớn và không muốn giữ nó hoàn toàn trong bộ nhớ.

Tìm các yếu tố thú vị

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 có một số phương pháp hữu ích giúp lặp lại đệ quy trên tất cả các cây con bên dưới nó (con cái, con cái của họ, v.v.). Ví dụ:
>>> root.tag
'data'
>>> root.attrib
{}
3:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
0

>>> root.tag
'data'
>>> root.attrib
{}
4 chỉ tìm thấy các yếu tố có thẻ là trẻ em trực tiếp của yếu tố hiện tại.
>>> root.tag
'data'
>>> root.attrib
{}
5 tìm thấy đứa con đầu lòng có một thẻ cụ thể và
>>> root.tag
'data'
>>> root.attrib
{}
6 truy cập vào nội dung văn bản phần tử.
>>> root.tag
'data'
>>> root.attrib
{}
7 truy cập vào các thuộc tính của phần tử:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
1

Đặc điểm kỹ thuật tinh vi hơn về các yếu tố cần tìm là có thể bằng cách sử dụng XPath.XPath.

Sửa đổi tệp XML

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4 cung cấp một cách đơn giản để xây dựng các tài liệu XML và viết chúng vào các tệp. Phương pháp
>>> root.tag
'data'
>>> root.attrib
{}
9 phục vụ mục đích này.

Sau khi được tạo, một đối tượng

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 có thể được thao tác bằng cách thay đổi trực tiếp các trường của nó (chẳng hạn như
>>> root.tag
'data'
>>> root.attrib
{}
6), thêm và sửa đổi các thuộc tính (phương thức
>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
2), cũng như thêm trẻ em mới (ví dụ với
>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
3).

Hãy nói rằng chúng tôi muốn thêm một vào cấp bậc của mỗi quốc gia và thêm thuộc tính

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
4 vào phần tử cấp bậc:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
2

XML của chúng tôi bây giờ trông như thế này:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
3

Chúng ta có thể loại bỏ các phần tử bằng

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
5. Hãy nói rằng chúng tôi muốn loại bỏ tất cả các quốc gia với thứ hạng cao hơn 50:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
4

Lưu ý rằng sửa đổi đồng thời trong khi lặp lại có thể dẫn đến các vấn đề, giống như khi lặp lại và sửa đổi danh sách python hoặc dicts. Do đó, ví dụ đầu tiên thu thập tất cả các yếu tố phù hợp với

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
6 và chỉ sau đó lặp lại trong danh sách các trận đấu.

XML của chúng tôi bây giờ trông như thế này:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
5

Chúng ta có thể loại bỏ các phần tử bằng >>> for child in root: ... print(child.tag, child.attrib) ... country {'name': 'Liechtenstein'} country {'name': 'Singapore'} country {'name': 'Panama'} 5. Hãy nói rằng chúng tôi muốn loại bỏ tất cả các quốc gia với thứ hạng cao hơn 50:

Lưu ý rằng sửa đổi đồng thời trong khi lặp lại có thể dẫn đến các vấn đề, giống như khi lặp lại và sửa đổi danh sách python hoặc dicts. Do đó, ví dụ đầu tiên thu thập tất cả các yếu tố phù hợp với

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
6 và chỉ sau đó lặp lại trong danh sách các trận đấu.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
6

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

Hàm

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
7 cũng cung cấp một cách thuận tiện để tạo các phần tử phụ mới cho một phần tử đã cho:

Phân tích cú pháp XML với không gian tên

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
7

Nếu đầu vào XML có không gian tên, thẻ và thuộc tính có tiền tố ở dạng

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
8 sẽ được mở rộng thành
>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
9 trong đó tiền tố được thay thế bằng URI đầy đủ. Ngoài ra, nếu có một không gian tên mặc định, rằng URI đầy đủ đó sẽ được chuẩn bị cho tất cả các thẻ không có lỗi.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
8

Dưới đây là một ví dụ XML kết hợp hai không gian tên, một với tiền tố hư cấu và cái còn lại là không gian tên mặc định:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
9

Một cách để tìm kiếm và khám phá ví dụ XML này là thêm thủ công URI vào mọi thẻ hoặc thuộc tính trong XPath của

>>> root[0][1].text
'2008'
0 hoặc
>>> root[0][1].text
'2008'
1:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
0

Một cách tốt hơn để tìm kiếm ví dụ XML theo tên là tạo một từ điển với các tiền tố của riêng bạn và sử dụng chúng trong các chức năng tìm kiếm:

Hai cách tiếp cận này cả hai đầu ra:

Hỗ trợ Xpath¶

Mô -đun này cung cấp hỗ trợ hạn chế cho các biểu thức XPath để định vị các phần tử trong cây. Mục tiêu là hỗ trợ một tập hợp con nhỏ của cú pháp viết tắt; Một động cơ XPath đầy đủ nằm ngoài phạm vi của mô -đun.Parsing XML section:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
1

Thí dụ¶

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
2

Ở đây, một ví dụ chứng minh một số khả năng XPath của mô -đun. Chúng tôi sẽ sử dụng tài liệu >>> root[0][1].text '2008' 2 XML từ phần phân tích cú pháp XML:

Đối với XML có không gian tên, hãy sử dụng ký hiệu

>>> root[0][1].text
'2008'
3 đủ điều kiện thông thường:

Hỗ trợ Xpath Cú pháp

>>> root[0][1].text
'2008'
4

Cú pháp

Thay đổi trong phiên bản 3.8: Hỗ trợ cho Star-Wildcards đã được thêm vào.Support for star-wildcards was added.

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end
4

Chọn tất cả các yếu tố trẻ em, bao gồm cả ý kiến ​​và hướng dẫn xử lý. Ví dụ,

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end
5 chọn tất cả các cháu có tên
>>> root[0][1].text
'2008'
8.

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end
7

Chọn nút hiện tại. Điều này chủ yếu là hữu ích ở đầu đường dẫn, để chỉ ra rằng nó là một con đường tương đối.

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end
8

Chọn tất cả các phần mềm, trên tất cả các cấp bên dưới phần tử hiện tại. Ví dụ,

>>> parser = ET.XMLPullParser(['start', 'end'])
>>> parser.feed('<mytag>sometext')
>>> list(parser.read_events())
[('start', <Element 'mytag' at 0x7fa66db2be58>)]
>>> parser.feed(' more text</mytag>')
>>> for event, elem in parser.read_events():
...     print(event)
...     print(elem.tag, 'text=', elem.text)
...
end
9 chọn tất cả các yếu tố
>>> root[0][1].text
'2008'
8 trong toàn bộ cây.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
01

Chọn phần tử cha. Trả về

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 Nếu đường dẫn cố gắng tiếp cận tổ tiên của phần tử bắt đầu (phần tử
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
03 đã được gọi).

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
04

Chọn tất cả các yếu tố có thuộc tính đã cho.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
05

Chọn tất cả các yếu tố mà thuộc tính đã cho có giá trị đã cho. Giá trị không thể chứa trích dẫn.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
06

Chọn tất cả các yếu tố mà thuộc tính đã cho không có giá trị đã cho. Giá trị không thể chứa trích dẫn.

Mới trong phiên bản 3.10.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
07

Chọn tất cả các yếu tố có một đứa trẻ tên

>>> root[0][1].text
'2008'
4. Chỉ có trẻ em ngay lập tức được hỗ trợ.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
09

Chọn tất cả các yếu tố có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, bằng với

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.

Mới trong phiên bản 3.7.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
11

Chọn tất cả các yếu tố có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, không bằng

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.

Mới trong phiên bản 3.10.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
13

Chọn tất cả các yếu tố có một đứa trẻ tên

>>> root[0][1].text
'2008'
4. Chỉ có trẻ em ngay lập tức được hỗ trợ.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
16

Chọn tất cả các yếu tố có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, bằng với

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.

Mới trong phiên bản 3.10.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
19

Chọn tất cả các yếu tố có một đứa trẻ tên

>>> root[0][1].text
'2008'
4. Chỉ có trẻ em ngay lập tức được hỗ trợ.

Chọn tất cả các yếu tố có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, bằng với

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.

Mới trong phiên bản 3.7.

Chọn tất cả các yếu tố có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, không bằng import xml.etree.ElementTree as ET tree = ET.parse('myfile.xlf') root = tree.getroot() for trans in root.findall('trans-unit'): source = trans.find('source').text target = trans.find('target').text lencomp = (len(target) - len(source))/len(source)*100.0 print(source,">>>", target) 10 đã cho.

Chọn tất cả các yếu tố có một đứa trẻ tên
>>> root[0][1].text
'2008'
4 có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, bằng với
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.(xml_data=None, *, out=None, from_file=None, **options)

Chọn tất cả các yếu tố có một đứa trẻ tên

>>> root[0][1].text
'2008'
4 có nội dung văn bản hoàn chỉnh, bao gồm cả hậu duệ, không bằng
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
10 đã cho.

Chọn tất cả các yếu tố được đặt tại vị trí đã cho. Vị trí có thể là một số nguyên (1 là vị trí đầu tiên), biểu thức

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
20 (cho vị trí cuối cùng) hoặc một vị trí so với vị trí cuối cùng (ví dụ:
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
21).

Các vị từ (biểu thức trong dấu ngoặc vuông) phải được đi trước bằng tên thẻ, dấu hoa thị hoặc vị ngữ khác.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
22 Các vị từ phải được đi trước bằng một tên thẻ.

Tài liệu tham khảo¶

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
3

Chức năng¶

  • ________ 123 ________ 124 (xml_data = none, *, out = none, from_file = none, ** Tùy chọn) ¶

  • Hàm chuyển đổi C14N 2.0.

    Canonicalization là một cách để bình thường hóa đầu ra XML theo cách cho phép so sánh byte-byte và chữ ký số. Nó làm giảm sự tự do mà các serializer XML có và thay vào đó tạo ra một biểu diễn XML bị ràng buộc hơn. Các hạn chế chính liên quan đến việc sắp xếp các khai báo không gian tên, thứ tự các thuộc tính và khoảng trắng không thể tin được.

  • Hàm này lấy chuỗi dữ liệu XML (xml_data) hoặc đường dẫn tệp hoặc đối tượng giống như tệp (từ_file) làm đầu vào, chuyển đổi nó thành dạng chính tắc và viết nó ra bằng đối tượng tệp (giống như), nếu được cung cấp hoặc Trả về nó dưới dạng chuỗi văn bản nếu không. Tệp đầu ra nhận văn bản, không phải byte. Do đó, nó nên được mở trong chế độ văn bản với mã hóa
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    25.

    Canonicalization là một cách để bình thường hóa đầu ra XML theo cách cho phép so sánh byte-byte và chữ ký số. Nó làm giảm sự tự do mà các serializer XML có và thay vào đó tạo ra một biểu diễn XML bị ràng buộc hơn. Các hạn chế chính liên quan đến việc sắp xếp các khai báo không gian tên, thứ tự các thuộc tính và khoảng trắng không thể tin được.

  • Hàm này lấy chuỗi dữ liệu XML (xml_data) hoặc đường dẫn tệp hoặc đối tượng giống như tệp (từ_file) làm đầu vào, chuyển đổi nó thành dạng chính tắc và viết nó ra bằng đối tượng tệp (giống như), nếu được cung cấp hoặc Trả về nó dưới dạng chuỗi văn bản nếu không. Tệp đầu ra nhận văn bản, không phải byte. Do đó, nó nên được mở trong chế độ văn bản với mã hóa
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    25.

    Sử dụng điển hình:

  • Các tùy chọn cấu hình như sau:

    Sử dụng điển hình:

  • Các tùy chọn cấu hình như sau:

  • with_comments: được đặt thành true để bao gồm các nhận xét (mặc định: false)

Strip_Text: Đặt thành True thành Dải khoảng trắng trước và sau khi nội dung văn bản

(Mặc định: Sai)

viết lại_prefixes: Đặt thành TRUE để thay thế các tiền tố không gian tên bằng cách N {number},

qname_aware_tags: một tập hợp các tên thẻ nhận biết qName trong đó tiền tố

nên được thay thế trong nội dung văn bản (mặc định: trống)(elem)

Qname_Aware_attrs: Một tập hợp các tên thuộc tính Aware Qname trong đó các tiền tố

loại trừ_attrs: một tập hợp các tên thuộc tính không nên tuần tự hóa

Elem là một cây phần tử hoặc một phần tử riêng lẻ.

Đã thay đổi trong phiên bản 3.8: Hàm

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
31 hiện bảo tồn thứ tự thuộc tính được chỉ định bởi người dùng.The
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
31 function now preserves the attribute order specified by the user.

________ 123 ________ 133 (văn bản, trình phân tích cú pháp = không) ¶(text, parser=None)

Phân tích phần XML từ hằng số chuỗi. Giống như

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
34. Văn bản là một chuỗi chứa dữ liệu XML. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp
root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về một ví dụ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 123 ________ 138 (trình tự, trình phân tích cú pháp = không) ¶(sequence, parser=None)

Phân tích một tài liệu XML từ một chuỗi các đoạn chuỗi. Trình tự là một danh sách hoặc chuỗi khác chứa các đoạn dữ liệu XML. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp

root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về một ví dụ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

Mới trong phiên bản 3.2.

________ 123 ________ 142 (cây, không gian = '', cấp = 0) ¶(tree, space=' ', level=0)

Lối nhiều khoảng trắng vào cây con để thụt vào cây một cách trực quan. Điều này có thể được sử dụng để tạo ra đầu ra XML được in khá đẹp. cây có thể là một yếu tố hoặc phần tử. Không gian là chuỗi khoảng trắng sẽ được chèn cho từng cấp độ thụt, hai ký tự không gian theo mặc định. Để thụt lề một phần bên trong của một cây đã thụt vào, vượt qua mức thụt đầu tiên dưới dạng cấp độ.

Mới trong phiên bản 3.9.

________ 123 ________ 144 (phần tử) ¶(element)

Kiểm tra xem một đối tượng dường như là một đối tượng phần tử hợp lệ. Phần tử là một thể hiện yếu tố. Trả về

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 Nếu đây là một đối tượng phần tử.

________ 123 ________ 147 (Nguồn, Sự kiện = Không, trình phân tích cú pháp = Không) ¶(source, events=None, parser=None)

Phân tích phần XML vào một cây phần tử tăng dần và báo cáo những gì mà người dùng sẽ tiếp tục. Nguồn là tên tệp hoặc đối tượng tệp chứa dữ liệu XML. Các sự kiện là một chuỗi các sự kiện để báo cáo lại. Các sự kiện được hỗ trợ là các chuỗi

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
48,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
50,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
51,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 và
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
53 (các sự kiện của NS NS được sử dụng để có thông tin không gian tên chi tiết). Nếu các sự kiện bị bỏ qua, chỉ có
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49 các sự kiện được báo cáo. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp
root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trình phân tích cú pháp phải là một lớp con của
root = ET.fromstring(country_data_as_string)
4 và chỉ có thể sử dụng
root = ET.fromstring(country_data_as_string)
3 mặc định làm mục tiêu. Trả về một iterator cung cấp các cặp
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
58.file object containing XML data. events is a sequence of events to report back. The supported events are the strings
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
48,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
50,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
51,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 and
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
53 (the “ns” events are used to get detailed namespace information). If events is omitted, only
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49 events are reported. parser is an optional parser instance. If not given, the standard
root = ET.fromstring(country_data_as_string)
4 parser is used. parser must be a subclass of
root = ET.fromstring(country_data_as_string)
4 and can only use the default
root = ET.fromstring(country_data_as_string)
3 as a target. Returns an iterator providing
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
58 pairs.

Lưu ý rằng trong khi

>>> root.tag
'data'
>>> root.attrib
{}
1 xây dựng cây dần dần, nó có sự cố chặn đọc trên nguồn (hoặc tên tệp nó). Do đó, nó không phù hợp với các ứng dụng trong đó việc chặn các lần đọc có thể được thực hiện. Để phân tích hoàn toàn không chặn, xem
root = ET.fromstring(country_data_as_string)
7.

Ghi chú

>>> root.tag
'data'
>>> root.attrib
{}
1 chỉ đảm bảo rằng nó đã nhìn thấy ký tự>> của một thẻ bắt đầu khi nó phát ra sự kiện bắt đầu, vì vậy các thuộc tính được xác định, nhưng nội dung của các thuộc tính văn bản và đuôi không được xác định tại điểm đó. Điều tương tự áp dụng cho trẻ em yếu tố; Họ có thể hoặc không thể có mặt.

Nếu bạn cần một yếu tố điền kinh hoàn toàn, thay vào đó, hãy tìm các sự kiện về kết thúc.

Không dùng nữa kể từ phiên bản 3.4: Đối số phân tích cú pháp.The parser argument.

Thay đổi trong phiên bản 3.8: Các sự kiện

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62 và
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
63 đã được thêm vào.The
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62 and
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
63 events were added.

________ 123 ________ 165 (Nguồn, trình phân tích cú pháp = Không) ¶(source, parser=None)

Phân tích phần XML vào một cây phần tử. Nguồn là tên tệp hoặc đối tượng tệp chứa dữ liệu XML. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp

root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về một ví dụ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4.

________ 123 ________ 169 (Target, Text = none) ¶(target, text=None)

Nhà máy nguyên tố PI. Chức năng nhà máy này tạo ra một yếu tố đặc biệt sẽ được tuần tự hóa dưới dạng hướng dẫn xử lý XML. Mục tiêu là một chuỗi chứa mục tiêu PI. Văn bản là một chuỗi chứa nội dung PI, nếu được đưa ra. Trả về một thể hiện phần tử, đại diện cho một lệnh xử lý.

Lưu ý rằng

root = ET.fromstring(country_data_as_string)
4 bỏ qua các hướng dẫn xử lý trong đầu vào thay vì tạo các đối tượng nhận xét cho chúng. Một
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
4 sẽ chỉ chứa các nút lệnh xử lý nếu chúng được đưa vào cây bằng một trong các phương thức
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 123 ________ 174 (tiền tố, uri) ¶(prefix, uri)

Đăng ký một tiền tố không gian tên. Cơ quan đăng ký là toàn cầu và bất kỳ ánh xạ hiện có nào cho tiền tố đã cho hoặc URI không gian tên sẽ bị xóa. Tiền tố là tiền tố không gian tên. URI là một không gian tên Uri. Các thẻ và các thuộc tính trong không gian tên này sẽ được tuần tự hóa với tiền tố đã cho, nếu có thể.

Mới trong phiên bản 3.2.

________ 123 ________ 142 (cây, không gian = '', cấp = 0) ¶(parent, tag, attrib={}, **extra)

Lối nhiều khoảng trắng vào cây con để thụt vào cây một cách trực quan. Điều này có thể được sử dụng để tạo ra đầu ra XML được in khá đẹp. cây có thể là một yếu tố hoặc phần tử. Không gian là chuỗi khoảng trắng sẽ được chèn cho từng cấp độ thụt, hai ký tự không gian theo mặc định. Để thụt lề một phần bên trong của một cây đã thụt vào, vượt qua mức thụt đầu tiên dưới dạng cấp độ.

Mới trong phiên bản 3.9.

________ 123 ________ 178 (phần tử, mã hóa = 'us-ascii', phương thức = 'xml', *, xml_declaration = none(element, encoding='us-ascii', method='xml', *, xml_declaration=None, default_namespace=None, short_empty_elements=True)

Tạo một biểu diễn chuỗi của một phần tử XML, bao gồm tất cả các phần mềm. Phần tử là một ví dụ

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5. Mã hóa 1 là mã hóa đầu ra (mặc định là US-ASCII). Sử dụng
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
80 để tạo chuỗi unicode (nếu không, một bytestring được tạo ra). Phương thức là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
82 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
83 (mặc định là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81). xml_declaration, default_namespace và short_empty_elements có cùng ý nghĩa như trong
>>> root.tag
'data'
>>> root.attrib
{}
9. Trả về một chuỗi được mã hóa (tùy chọn) chứa dữ liệu XML.

Mới trong phiên bản 3.4: Tham số Short_empty_elements.The short_empty_elements parameter.

Mới trong phiên bản 3.8: Các tham số XML_Declaration và Default_namespace.The xml_declaration and default_namespace parameters.

Đã thay đổi trong phiên bản 3.8: Hàm

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
86 hiện bảo tồn thứ tự thuộc tính được chỉ định bởi người dùng.The
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
86 function now preserves the attribute order specified by the user.

________ 123 ________ 188 (phần tử, mã hóa = 'us-ascii', phương thức = 'xml', *, xml_declaration = none(element, encoding='us-ascii', method='xml', *, xml_declaration=None, default_namespace=None, short_empty_elements=True)

Tạo một biểu diễn chuỗi của một phần tử XML, bao gồm tất cả các phần mềm. Phần tử là một ví dụ

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5. Mã hóa 1 là mã hóa đầu ra (mặc định là US-ASCII). Sử dụng
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
80 để tạo chuỗi unicode (nếu không, một bytestring được tạo ra). Phương thức là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
82 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
83 (mặc định là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81). xml_declaration, default_namespace và short_empty_elements có cùng ý nghĩa như trong
>>> root.tag
'data'
>>> root.attrib
{}
9. Trả về một danh sách các chuỗi được mã hóa (tùy chọn) chứa dữ liệu XML. Nó không đảm bảo bất kỳ chuỗi cụ thể nào, ngoại trừ
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
96.

Mới trong phiên bản 3.2.

Mới trong phiên bản 3.4: Tham số Short_empty_elements.The short_empty_elements parameter.

Mới trong phiên bản 3.8: Các tham số XML_Declaration và Default_namespace.The xml_declaration and default_namespace parameters.

Đã thay đổi trong phiên bản 3.8: Hàm

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
86 hiện bảo tồn thứ tự thuộc tính được chỉ định bởi người dùng.The
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
97 function now preserves the attribute order specified by the user.

________ 123 ________ 188 (phần tử, mã hóa = 'us-ascii', phương thức = 'xml', *, xml_declaration = none(text, parser=None)

Tạo một biểu diễn chuỗi của một phần tử XML, bao gồm tất cả các phần mềm. Phần tử là một ví dụ

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5. Mã hóa 1 là mã hóa đầu ra (mặc định là US-ASCII). Sử dụng
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
80 để tạo chuỗi unicode (nếu không, một bytestring được tạo ra). Phương thức là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
82 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
83 (mặc định là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81). xml_declaration, default_namespace và short_empty_elements có cùng ý nghĩa như trong
>>> root.tag
'data'
>>> root.attrib
{}
9. Trả về một danh sách các chuỗi được mã hóa (tùy chọn) chứa dữ liệu XML. Nó không đảm bảo bất kỳ chuỗi cụ thể nào, ngoại trừ
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
96.

Mới trong phiên bản 3.2.(text, parser=None)

Đã thay đổi trong phiên bản 3.8: Hàm

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
97 hiện bảo tồn thứ tự thuộc tính được chỉ định bởi người dùng.

________ 123 ________ 199 (văn bản, trình phân tích cú pháp = không) ¶

Phân tích phần XML từ hằng số chuỗi. Chức năng này có thể được sử dụng để nhúng các loại văn bản XML XML trong mã Python. Văn bản là một chuỗi chứa dữ liệu XML. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp

root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về một ví dụ
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 123 ________ 203 (văn bản, trình phân tích cú pháp = không) ¶

Phân tích phần XML từ hằng số chuỗi và cũng trả về một từ điển ánh xạ từ phần tử ID: S sang các phần tử. Văn bản là một chuỗi chứa dữ liệu XML. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp

root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về một tuple chứa một thể hiện
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 và từ điển.parse attribute to
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81, and use the href attribute to specify the document to include.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
4

Hỗ trợ Xincludehref attribute is treated as a file name. You can use custom loaders to override this behaviour. Also note that the standard helper does not support XPointer syntax.

Mô -đun này cung cấp hỗ trợ hạn chế cho các chỉ thị xinclude, thông qua mô -đun trợ giúp

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
06. Mô -đun này có thể được sử dụng để chèn các cây con và chuỗi văn bản vào các cây phần tử, dựa trên thông tin trong cây.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
5

Thí dụ¶source.xml document. The result might look something like this:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
6

Ở đây, một ví dụ chứng minh việc sử dụng mô -đun xinclude. Để bao gồm một tài liệu XML trong tài liệu hiện tại, hãy sử dụng phần tử

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
07 và đặt thuộc tính phân tích cú pháp thành
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81 và sử dụng thuộc tính HREF để chỉ định tài liệu để bao gồm.parse attribute is omitted, it defaults to “xml”. The href attribute is required.

Theo mặc định, thuộc tính HREF được coi là tên tệp. Bạn có thể sử dụng trình tải tùy chỉnh để ghi đè hành vi này. Cũng lưu ý rằng người trợ giúp tiêu chuẩn không hỗ trợ cú pháp XPOUNIN.parse attribute to “text”:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
7

Để xử lý tệp này, hãy tải nó như bình thường và chuyển phần tử gốc đến mô -đun

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
9:

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
8

Mô -đun ElementInClude thay thế phần tử import xml.etree.ElementTree as ET tree = ET.parse('myfile.xlf') for trans in tree.findall('file/body/trans-unit'): source = trans.find('source').text target = trans.find('target').text lencomp = (len(target) - len(source))/len(source)*100.0 print(source,">>>", target) 07 bằng phần tử gốc từ tài liệu Source.xml. Kết quả có thể trông giống như thế này:

Nếu thuộc tính phân tích cú pháp bị bỏ qua, nó mặc định là XML XML. Thuộc tính HREF là bắt buộc.

Để bao gồm một tài liệu văn bản, hãy sử dụng phần tử
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
07 và đặt thuộc tính phân tích cú pháp thành văn bản trực tuyến:(href, parse, encoding=None)

Kết quả có thể trông giống như:

Tài liệu tham khảo¶(elem, loader=None, base_url=None, max_depth=6)

Chức năng¶

Trả về tài nguyên mở rộng. Nếu chế độ phân tích cú pháp là

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81, đây là một thể hiện ElementTree. Nếu chế độ phân tích cú pháp là văn bản, thì đây là một chuỗi unicode. Nếu trình tải không thành công, nó có thể trả về không hoặc tăng ngoại lệ.

Mới trong phiên bản 3.9: Các tham số Base_URL và MAX_DEPTH.The base_url and max_depth parameters.

Đối tượng phần tử

Lớp ________ 123 ________ 222 (TAG, Attrib(tag, attrib={}, **extra)

Lớp phần tử. Lớp này xác định giao diện phần tử và cung cấp triển khai tham chiếu của giao diện này.

Tên phần tử, tên thuộc tính và giá trị thuộc tính có thể là chuỗi bytestrings hoặc unicode. Tag là tên phần tử. Attrible là một từ điển tùy chọn, chứa các thuộc tính phần tử. Thêm chứa các thuộc tính bổ sung, được đưa ra dưới dạng đối số từ khóa.

________ 223¶

Một chuỗi xác định loại dữ liệu mà phần tử này biểu diễn (loại phần tử, nói cách khác).

________ 224¶ ________ 225¶

Các thuộc tính này có thể được sử dụng để chứa dữ liệu bổ sung được liên kết với phần tử. Giá trị của chúng thường là chuỗi nhưng có thể là bất kỳ đối tượng dành riêng cho ứng dụng. Nếu phần tử được tạo từ tệp XML, thuộc tính văn bản sẽ giữ văn bản giữa thẻ bắt đầu của phần tử và thẻ con đầu tiên hoặc đầu , hoặc

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02. Đối với dữ liệu XML

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
9

Phần tử A có

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 cho cả thuộc tính văn bản và đuôi, phần tử B có văn bản
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
29 và đuôi
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
30, phần tử C có văn bản
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
31 và đuôi
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02, và phần tử D có văn bản
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 và đuôi
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
34.

Để thu thập văn bản bên trong của một phần tử, xem

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
35, ví dụ
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
36.

Các ứng dụng có thể lưu trữ các đối tượng tùy ý trong các thuộc tính này.

________ 237¶

Một từ điển chứa các thuộc tính của phần tử. Lưu ý rằng trong khi giá trị thuộc tính luôn là một từ điển Python có thể thay đổi thực sự, nhưng việc triển khai ElementTree có thể chọn sử dụng một biểu diễn nội bộ khác và chỉ tạo từ điển nếu ai đó yêu cầu nó. Để tận dụng các triển khai như vậy, hãy sử dụng các phương thức từ điển dưới đây bất cứ khi nào có thể.

Các phương thức giống như từ điển hoạt động trên các thuộc tính phần tử.

________ 238 ()()

Đặt lại một yếu tố. Hàm này loại bỏ tất cả các phần mềm, xóa tất cả các thuộc tính và đặt các thuộc tính văn bản và đuôi thành

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02.

________ 240 (khóa, mặc định = Không) ¶(key, default=None)

Nhận thuộc tính phần tử có tên là khóa.

Trả về giá trị thuộc tính hoặc mặc định nếu không tìm thấy thuộc tính.

________ 241 ()()

Trả về các thuộc tính phần tử như một chuỗi của các cặp (tên, giá trị). Các thuộc tính được trả về theo thứ tự tùy ý.

________ 242 ()()

Trả về tên thuộc tính phần tử làm danh sách. Các tên được trả lại theo thứ tự tùy ý.

________ 243 (khóa, giá trị) ¶(key, value)

Đặt phím thuộc tính trên phần tử thành giá trị.

Các phương pháp sau đây hoạt động trên các yếu tố trẻ em (phụ).

________ 244 (Subelement) ¶(subelement)

Thêm phần tử SubSement vào cuối danh sách nội bộ của phần tử này. Tăng

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 nếu Subelement không phải là
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 247 (SubErements) ¶(subelements)

Nối các phần mềm từ một đối tượng chuỗi bằng 0 hoặc nhiều phần tử. Tăng

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 nếu một Subelement không phải là
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

Mới trong phiên bản 3.2.

________ 250 (khớp, không gian tên = không) ¶(match, namespaces=None)

Tìm các trận đấu phù hợp đầu tiên của Subelement. khớp có thể là một tên thẻ hoặc một đường dẫn. Trả về một thể hiện phần tử hoặc

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02. Không gian tên là một ánh xạ tùy chọn từ tiền tố không gian tên đến tên đầy đủ. Vượt qua
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 làm tiền tố để di chuyển tất cả các tên thẻ chưa được chia trong biểu thức vào không gian tên đã cho.path. Returns an element instance or
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02. namespaces is an optional mapping from namespace prefix to full name. Pass
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 as prefix to move all unprefixed tag names in the expression into the given namespace.

________ 37 (khớp, không gian tên = không) ¶(match, namespaces=None)

Tìm tất cả các phần mềm phù hợp, theo tên thẻ hoặc đường dẫn. Trả về một danh sách chứa tất cả các yếu tố phù hợp theo thứ tự tài liệu. Không gian tên là một ánh xạ tùy chọn từ tiền tố không gian tên đến tên đầy đủ. Vượt qua

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 làm tiền tố để di chuyển tất cả các tên thẻ chưa được chia trong biểu thức vào không gian tên đã cho.path. Returns a list containing all matching elements in document order. namespaces is an optional mapping from namespace prefix to full name. Pass
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 as prefix to move all unprefixed tag names in the expression into the given namespace.

________ 255 (khớp, mặc định = không, không gian tên = không) ¶(match, default=None, namespaces=None)

Tìm văn bản cho trận đấu phù hợp đầu tiên của Subelement. khớp có thể là một tên thẻ hoặc một đường dẫn. Trả về nội dung văn bản của phần tử khớp đầu tiên hoặc mặc định nếu không tìm thấy phần tử nào. Lưu ý rằng nếu phần tử phù hợp không có nội dung văn bản, một chuỗi trống được trả về. Không gian tên là một ánh xạ tùy chọn từ tiền tố không gian tên đến tên đầy đủ. Vượt qua

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 làm tiền tố để di chuyển tất cả các tên thẻ chưa được chia trong biểu thức vào không gian tên đã cho.path. Returns the text content of the first matching element, or default if no element was found. Note that if the matching element has no text content an empty string is returned. namespaces is an optional mapping from namespace prefix to full name. Pass
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 as prefix to move all unprefixed tag names in the expression into the given namespace.

________ 257 (INDEX, SUPEuity) ¶(index, subelement)

Chèn SubLement tại vị trí đã cho trong yếu tố này. Tăng

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 nếu Subelement không phải là
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 260 (tag = none) ¶(tag=None)

Tạo một bộ lặp cây với phần tử hiện tại là gốc. Trình lặp lặp lại trên phần tử này và tất cả các phần tử bên dưới nó, theo thứ tự tài liệu (độ sâu đầu tiên). Nếu thẻ không phải là

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62, chỉ các phần tử có thẻ bằng thẻ được trả về từ trình lặp. Nếu cấu trúc cây được sửa đổi trong quá trình lặp, kết quả không được xác định.iterator with the current element as the root. The iterator iterates over this element and all elements below it, in document (depth first) order. If tag is not
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 or
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62, only elements whose tag equals tag are returned from the iterator. If the tree structure is modified during iteration, the result is undefined.

Mới trong phiên bản 3.2.

________ 263 (khớp, không gian tên = không) ¶(match, namespaces=None)

Tìm tất cả các phần mềm phù hợp, theo tên thẻ hoặc đường dẫn. Trả về một yếu tố phù hợp với tất cả các yếu tố phù hợp theo thứ tự tài liệu. Không gian tên là một ánh xạ tùy chọn từ tiền tố không gian tên đến tên đầy đủ.path. Returns an iterable yielding all matching elements in document order. namespaces is an optional mapping from namespace prefix to full name.

Mới trong phiên bản 3.2.

________ 264 ()()

Tạo một trình lặp văn bản. Các vòng lặp lặp qua phần tử này và tất cả các phần phụ, theo thứ tự tài liệu và trả về tất cả văn bản bên trong.

Mới trong phiên bản 3.2.

________ 265 (Tag, Attrib)(tag, attrib)

Tạo một đối tượng phần tử mới cùng loại với phần tử này. Không gọi phương thức này, hãy sử dụng chức năng nhà máy

>>> for child in root:
...     print(child.tag, child.attrib)
...
country {'name': 'Liechtenstein'}
country {'name': 'Singapore'}
country {'name': 'Panama'}
7 thay thế.

________ 267 (Subelement) ¶(subelement)

Loại bỏ Subelement khỏi phần tử. Không giống như các phương thức tìm* Phương thức này so sánh các phần tử dựa trên nhận dạng thể hiện, không phải trên giá trị thẻ hoặc nội dung.

Các đối tượng

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 cũng hỗ trợ các phương thức loại trình tự sau để làm việc với các phần phụ:
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
69,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
70,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
71,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
72.

THẬN TRỌNG: Các yếu tố không có phụ sẽ kiểm tra là

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
73. Hành vi này sẽ thay đổi trong các phiên bản trong tương lai. Thay vào đó, hãy sử dụng thử nghiệm
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
74 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
75 cụ thể.

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
0

Trước Python 3.8, thứ tự tuần tự hóa của các thuộc tính XML của các phần tử đã được dự đoán một cách nhân tạo bằng cách sắp xếp các thuộc tính theo tên của chúng. Dựa trên thứ tự được bảo đảm của các dicts, việc sắp xếp lại tùy ý này đã được xóa trong Python 3.8 để bảo tồn thứ tự mà các thuộc tính ban đầu được phân tích cú pháp hoặc được tạo bởi mã người dùng.

Nói chung, mã người dùng nên cố gắng không phụ thuộc vào một thứ tự cụ thể của các thuộc tính, cho rằng thông tin XML đã loại trừ rõ ràng thứ tự thuộc tính khỏi thông tin truyền đạt. Mã nên được chuẩn bị để đối phó với bất kỳ đơn đặt hàng trên đầu vào. Trong trường hợp yêu cầu đầu ra XML xác định, ví dụ: Đối với việc ký kết mật mã hoặc tập dữ liệu thử nghiệm, tuần tự hóa chính tắc có sẵn với hàm

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
76.

Trong trường hợp đầu ra chính tắc không được áp dụng nhưng một thứ tự thuộc tính cụ thể vẫn được mong muốn trên đầu ra, mã sẽ nhằm mục đích tạo các thuộc tính trực tiếp theo thứ tự mong muốn, để tránh sự không phù hợp về nhận thức cho người đọc mã. Trong trường hợp điều này khó đạt được, một công thức như sau đây có thể được áp dụng trước khi tuần tự hóa để thực thi một đơn đặt hàng độc lập với việc tạo yếu tố:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
1

Đối tượng ElementTree

Lớp ________ 123 ________ 278 (phần tử = none, file = none) ¶(element=None, file=None)

Lớp trình bao bọc ElementTree. Lớp này đại diện cho toàn bộ hệ thống phân cấp phần tử và thêm một số hỗ trợ bổ sung cho việc tuần tự hóa đến và từ XML tiêu chuẩn.

Phần tử là phần tử gốc. Cây được khởi tạo với nội dung của tệp XML nếu được đưa ra.

________ 279 (yếu tố) ¶(element)

Thay thế phần tử gốc cho cây này. Điều này loại bỏ các nội dung hiện tại của cây và thay thế nó bằng phần tử đã cho. Sử dụng một cách cẩn thận. Phần tử là một thể hiện yếu tố.

________ 250 (khớp, không gian tên = không) ¶(match, namespaces=None)

Giống như

>>> root.tag
'data'
>>> root.attrib
{}
5, bắt đầu từ gốc của cây.

________ 37 (khớp, không gian tên = không) ¶(match, namespaces=None)

Giống như

>>> root.tag
'data'
>>> root.attrib
{}
4, bắt đầu từ gốc của cây.

________ 255 (khớp, mặc định = không, không gian tên = không) ¶(match, default=None, namespaces=None)

Giống như

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
85, bắt đầu từ gốc của cây.

________ 286 ()()

Trả về phần tử gốc cho cây này.

________ 260 (tag = none) ¶(tag=None)

Tạo và trả về một bộ lặp cây cho phần tử gốc. Các vòng lặp lặp trên tất cả các yếu tố trong cây này, theo thứ tự phần. Tag là thẻ để tìm kiếm (mặc định là trả về tất cả các yếu tố).

________ 263 (khớp, không gian tên = không) ¶(match, namespaces=None)

Giống như

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
89, bắt đầu từ gốc của cây.

Mới trong phiên bản 3.2.

________ 165 (Nguồn, trình phân tích cú pháp = Không) ¶(source, parser=None)

Tải phần XML bên ngoài vào cây phần tử này. Nguồn là tên tệp hoặc đối tượng tệp. Trình phân tích cú pháp là một thể hiện trình phân tích cú pháp tùy chọn. Nếu không được đưa ra, trình phân tích cú pháp

root = ET.fromstring(country_data_as_string)
4 tiêu chuẩn được sử dụng. Trả về phần phần tử gốc.file object. parser is an optional parser instance. If not given, the standard
root = ET.fromstring(country_data_as_string)
4 parser is used. Returns the section root element.

A(file, encoding='us-ascii', xml_declaration=None, default_namespace=None, method='xml', *, short_empty_elements=True)

Viết cây phần tử vào một tệp, như XML. Tệp là một tên tệp hoặc một đối tượng tệp được mở để viết. Mã hóa 1 là mã hóa đầu ra (mặc định là US-ASCII). XML_Declaration điều khiển nếu sẽ được thêm khai báo XML vào tệp. Sử dụng

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
73 cho không bao giờ,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 luôn luôn,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 chỉ nếu không phải là US-ASCII hoặc UTF-8 hoặc Unicode (mặc định là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02). Default_namespace đặt không gian tên XML mặc định (đối với các loại XMLNS). Phương thức là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
82 hoặc
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
83 (mặc định là
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81). Tham số chỉ từ khóa chỉ ngắn hạn_empty_elements kiểm soát định dạng của các phần tử không chứa nội dung. Nếu
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 (mặc định), chúng được phát ra dưới dạng một thẻ tự đóng, nếu không chúng được phát ra dưới dạng một cặp thẻ bắt đầu/kết thúc.file object opened for writing. encoding 1 is the output encoding (default is US-ASCII). xml_declaration controls if an XML declaration should be added to the file. Use
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')

for trans in tree.findall('file/body/trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
73 for never,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 for always,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 for only if not US-ASCII or UTF-8 or Unicode (default is
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02). default_namespace sets the default XML namespace (for “xmlns”). method is either
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
82 or
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
83 (default is
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
81). The keyword-only short_empty_elements parameter controls the formatting of elements that contain no content. If
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
45 (the default), they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags.

Đầu ra là một chuỗi (

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
02) hoặc nhị phân (
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
03). Điều này được kiểm soát bởi đối số mã hóa. Nếu mã hóa là
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
04, đầu ra là một chuỗi; Nếu không, nó nhị phân. Lưu ý rằng điều này có thể mâu thuẫn với loại tệp nếu nó là một đối tượng tệp mở; Hãy chắc chắn rằng bạn không cố gắng viết một chuỗi vào luồng nhị phân và ngược lại.file object; make sure you do not try to write a string to a binary stream and vice versa.

Mới trong phiên bản 3.4: Tham số Short_empty_elements.The short_empty_elements parameter.

Đã thay đổi trong phiên bản 3.8: Phương thức

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
05 hiện bảo tồn thứ tự thuộc tính được chỉ định bởi người dùng.The
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
05 method now preserves the attribute order specified by the user.

Đây là tệp XML sẽ bị thao túng:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
2

Ví dụ về việc thay đổi thuộc tính Target Target của mỗi liên kết trong đoạn đầu tiên:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
3

Đối tượng Qname

Lớp ________ 123 ________ 307 (text_or_uri, tag = none) ¶(text_or_uri, tag=None)

QNAME WRAPPER. Điều này có thể được sử dụng để bọc giá trị thuộc tính Qname, để có được xử lý không gian tên thích hợp trên đầu ra. Text_or_uri là một chuỗi chứa giá trị qname, ở dạng {uri} cục bộ, hoặc, nếu đối số thẻ được đưa ra, phần URI của Qname. Nếu TAG được đưa ra, đối số đầu tiên được hiểu là một URI và đối số này được hiểu là một tên địa phương.

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
08 Các trường hợp mờ đục.

Đối tượng TreeBuilder

Lớp ________ 123 ________ 310 (Element_Factory = none, *, bình luận_factory = none, pi_factory = none, insert_comments = false, insert_pis = false)(element_factory=None, *, comment_factory=None, pi_factory=None, insert_comments=False, insert_pis=False)

Trình tạo cấu trúc phần tử chung. Nhà xây dựng này chuyển đổi một chuỗi bắt đầu, dữ liệu, kết thúc, nhận xét và phương thức PI gọi đến một cấu trúc phần tử được hình thành tốt. Bạn có thể sử dụng lớp này để xây dựng cấu trúc phần tử bằng trình phân tích cú pháp XML tùy chỉnh hoặc trình phân tích cú pháp cho một số định dạng giống như XML khác.

Element_Factory, khi được đưa ra, phải là một người có thể gọi là có thể chấp nhận hai đối số vị trí: một thẻ và một phương pháp của các thuộc tính. Dự kiến ​​sẽ trả về một thể hiện yếu tố mới.

Các chức năng bình luận_factory và pi_factory, khi được đưa ra, nên hoạt động như các chức năng

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
11 và
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
12 để tạo nhận xét và hướng dẫn xử lý. Khi không được đưa ra, các nhà máy mặc định sẽ được sử dụng. Khi insert_comments và/hoặc insert_pis là đúng, nhận xét/pis sẽ được chèn vào cây nếu chúng xuất hiện trong phần tử gốc (nhưng không nằm ngoài nó).

________ 313 ()()

Xóa các bộ đệm xây dựng và trả về yếu tố tài liệu Toplevel. Trả về một ví dụ

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5.

________ 315 (dữ liệu) ¶(data)

Thêm văn bản vào phần tử hiện tại. Dữ liệu là một chuỗi. Điều này phải là một bytestring hoặc chuỗi unicode.

________ 316 (Tag)(tag)

Đóng phần tử hiện tại. Tag là tên phần tử. Trả về phần tử đóng.

________ 317 (Tag, attrs) ¶(tag, attrs)

Mở một yếu tố mới. Tag là tên phần tử. Attrs là một từ điển chứa các thuộc tính phần tử. Trả về phần tử đã mở.

Tạo một nhận xét với văn bản đã cho. Nếu

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
18 là đúng, điều này cũng sẽ thêm nó vào cây.

Mới trong phiên bản 3.8.

________ 319 (mục tiêu, văn bản) ¶(target, text)

Tạo một nhận xét với tên và văn bản mục tiêu đã cho. Nếu

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
20 là đúng, điều này cũng sẽ thêm nó vào cây.

Mới trong phiên bản 3.8.

________ 319 (mục tiêu, văn bản) ¶

Tạo một nhận xét với tên và văn bản mục tiêu đã cho. Nếu
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
20 là đúng, điều này cũng sẽ thêm nó vào cây.(name, pubid, system)

Ngoài ra, một đối tượng

root = ET.fromstring(country_data_as_string)
3 tùy chỉnh có thể cung cấp các phương thức sau:

________ 322 (Tên, PubID, System) ¶

Xử lý một tuyên bố doctype. Tên là tên doctype. PubID là định danh công khai. Hệ thống là định danh hệ thống. Phương pháp này không tồn tại trên lớp
root = ET.fromstring(country_data_as_string)
3 mặc định.(prefix, uri)

Mới trong phiên bản 3.2.

Mới trong phiên bản 3.8.

________ 319 (mục tiêu, văn bản) ¶(prefix)

Tạo một nhận xét với tên và văn bản mục tiêu đã cho. Nếu

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
20 là đúng, điều này cũng sẽ thêm nó vào cây.

Mới trong phiên bản 3.8.

________ 319 (mục tiêu, văn bản) ¶(write, *, with_comments=False, strip_text=False, rewrite_prefixes=False, qname_aware_tags=None, qname_aware_attrs=None, exclude_attrs=None, exclude_tags=None)

Tạo một nhận xét với tên và văn bản mục tiêu đã cho. Nếu

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
20 là đúng, điều này cũng sẽ thêm nó vào cây.

Mới trong phiên bản 3.8.

________ 319 (mục tiêu, văn bản) ¶

Tạo một nhận xét với tên và văn bản mục tiêu đã cho. Nếu
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
20 là đúng, điều này cũng sẽ thêm nó vào cây.(*, target=None, encoding=None)

Ngoài ra, một đối tượng

root = ET.fromstring(country_data_as_string)
3 tùy chỉnh có thể cung cấp các phương thức sau:

Đã thay đổi trong phiên bản 3.8: Các tham số hiện chỉ dành cho từ khóa. Đối số HTML không còn hỗ trợ.Parameters are now keyword-only. The html argument no longer supported.

________ 313 ()()

Kết thúc dữ liệu cho ăn cho trình phân tích cú pháp. Trả về kết quả của việc gọi phương thức

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
38 của mục tiêu được thông qua trong quá trình xây dựng; Theo mặc định, đây là yếu tố tài liệu Toplevel.

________ 339 (dữ liệu) ¶(data)

Nguồn dữ liệu cho trình phân tích cú pháp. Dữ liệu được mã hóa dữ liệu.

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
40 gọi phương thức
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
41 của Target cho mỗi thẻ mở, phương thức
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
42 của nó cho mỗi thẻ đóng và dữ liệu được xử lý theo phương thức
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
43. Để biết thêm các phương thức gọi lại được hỗ trợ, hãy xem lớp
root = ET.fromstring(country_data_as_string)
3.
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
45 gọi phương thức của mục tiêu
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
38.
root = ET.fromstring(country_data_as_string)
4 có thể được sử dụng không chỉ để xây dựng cấu trúc cây. Đây là một ví dụ về việc đếm độ sâu tối đa của tệp XML:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
4

XMLPullPARSER Đối tượng

Lớp ________ 123 ________ 349 (Sự kiện = Không) ¶(events=None)

Một trình phân tích cú pháp kéo phù hợp cho các ứng dụng không chặn. API phía đầu vào của nó tương tự như

root = ET.fromstring(country_data_as_string)
4, nhưng thay vì đẩy các cuộc gọi đến mục tiêu gọi lại,
root = ET.fromstring(country_data_as_string)
7 thu thập một danh sách nội bộ các sự kiện phân tích cú pháp và cho phép người dùng đọc từ nó. Các sự kiện là một chuỗi các sự kiện để báo cáo lại. Các sự kiện được hỗ trợ là các chuỗi
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
48,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
50,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
51,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
52 và
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
53 (các sự kiện của NS NS được sử dụng để có thông tin không gian tên chi tiết). Nếu các sự kiện bị bỏ qua, chỉ có
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49 các sự kiện được báo cáo.

________ 339 (dữ liệu) ¶(data)

Nguồn dữ liệu cho trình phân tích cú pháp. Dữ liệu được mã hóa dữ liệu.

________ 313 ()()

Kết thúc dữ liệu cho ăn cho trình phân tích cú pháp. Trả về kết quả của việc gọi phương thức

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
38 của mục tiêu được thông qua trong quá trình xây dựng; Theo mặc định, đây là yếu tố tài liệu Toplevel.

________ 339 (dữ liệu) ¶()

Nguồn dữ liệu cho trình phân tích cú pháp. Dữ liệu được mã hóa dữ liệu.

  • <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    40 gọi phương thức
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    41 của Target cho mỗi thẻ mở, phương thức
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    42 của nó cho mỗi thẻ đóng và dữ liệu được xử lý theo phương thức
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    43. Để biết thêm các phương thức gọi lại được hỗ trợ, hãy xem lớp
    root = ET.fromstring(country_data_as_string)
    
    3.
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    45 gọi phương thức của mục tiêu
    <?xml version="1.0"?>
    <data>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
            <gdppc>141100</gdppc>
            <neighbor name="Austria" direction="E"/>
            <neighbor name="Switzerland" direction="W"/>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
            <gdppc>59900</gdppc>
            <neighbor name="Malaysia" direction="N"/>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
            <gdppc>13600</gdppc>
            <neighbor name="Costa Rica" direction="W"/>
            <neighbor name="Colombia" direction="E"/>
        </country>
    </data>
    
    38.
    root = ET.fromstring(country_data_as_string)
    
    4 có thể được sử dụng không chỉ để xây dựng cấu trúc cây. Đây là một ví dụ về việc đếm độ sâu tối đa của tệp XML:

  • XMLPullPARSER Đối tượng

  • Lớp ________ 123 ________ 349 (Sự kiện = Không) ¶

  • Một trình phân tích cú pháp kéo phù hợp cho các ứng dụng không chặn. API phía đầu vào của nó tương tự như

    root = ET.fromstring(country_data_as_string)
    
    4, nhưng thay vì đẩy các cuộc gọi đến mục tiêu gọi lại,
    root = ET.fromstring(country_data_as_string)
    
    7 thu thập một danh sách nội bộ các sự kiện phân tích cú pháp và cho phép người dùng đọc từ nó. Các sự kiện là một chuỗi các sự kiện để báo cáo lại. Các sự kiện được hỗ trợ là các chuỗi
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    48,
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    49,
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    50,
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    51,
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    52 và
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    53 (các sự kiện của NS NS được sử dụng để có thông tin không gian tên chi tiết). Nếu các sự kiện bị bỏ qua, chỉ có
    import xml.etree.ElementTree as ET
    
    tree = ET.parse('myfile.xlf')
    root = tree.getroot()
    
    for trans in root.findall('trans-unit'):
        source = trans.find('source').text
        target = trans.find('target').text
        lencomp = (len(target) - len(source))/len(source)*100.0
        print(source,">>>", target)
    
    49 các sự kiện được báo cáo.

Lấy dữ liệu byte đã cho cho trình phân tích cú pháp.

Báo hiệu cho trình phân tích cú pháp rằng luồng dữ liệu bị chấm dứt. Không giống như

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
45, phương pháp này luôn trả về
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02. Bất kỳ sự kiện nào chưa được truy xuất khi trình phân tích cú pháp vẫn có thể được đọc với
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
63.

________ 364 ()

Trả về một trình lặp lại về các sự kiện đã gặp phải trong dữ liệu được cung cấp cho trình phân tích cú pháp. Trình lặp mang lại các cặp

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
58, trong đó sự kiện là một chuỗi đại diện cho loại sự kiện (ví dụ:
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
49) và ELEM là đối tượng
import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
5 gặp phải hoặc giá trị bối cảnh khác như sau.

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
68,
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
69: Phần tử hiện tại.

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62,
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
63: Hướng dẫn nhận xét / xử lý hiện tạiThe
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62 and
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
63 events were added.

<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> 72: Một tuple <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data> 73 đặt tên cho ánh xạ không gian tên được tuyên bố.

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
74:
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
02 (điều này có thể thay đổi trong phiên bản tương lai)

Các sự kiện được cung cấp trong một cuộc gọi trước đó đến

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
63 sẽ không được mang lại. Các sự kiện chỉ được tiêu thụ từ hàng đợi nội bộ khi chúng được lấy từ trình lặp, do đó, nhiều độc giả lặp lại song song trên các trình lặp thu được từ
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank>1</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank>4</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank>68</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
63 sẽ có kết quả không thể đoán trước.

Ghi chú

root = ET.fromstring(country_data_as_string)
7 chỉ đảm bảo rằng nó đã nhìn thấy ký tự>> của một thẻ bắt đầu khi nó phát ra sự kiện bắt đầu, vì vậy các thuộc tính được xác định, nhưng nội dung của các thuộc tính văn bản và đuôi không được xác định tại điểm đó. Điều tương tự áp dụng cho trẻ em yếu tố; Họ có thể hoặc không thể có mặt.

Nếu bạn cần một yếu tố điền kinh hoàn toàn, thay vào đó, hãy tìm các sự kiện về kết thúc.

Mới trong phiên bản 3.4.

Thay đổi trong phiên bản 3.8: Các sự kiện

import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
62 và
import xml.etree.ElementTree as ET

tree = ET.parse('myfile.xlf')
root = tree.getroot()

for trans in root.findall('trans-unit'):
    source = trans.find('source').text
    target = trans.find('target').text
    lencomp = (len(target) - len(source))/len(source)*100.0
    print(source,">>>", target)
63 đã được thêm vào.

1(1,2,3,4)(1,2,3,4)

Ngoại lệ ha

Làm thế nào để bạn thay đổi dữ liệu XML trong Python?

Yếu tố.Đặt ('attrname', 'value') - sửa đổi các thuộc tính phần tử ..
Yếu tố.Subelement (cha mẹ, new_childtag) -tạo ra một thẻ con mới dưới cha mẹ ..
Yếu tố.Viết ('Tên tệp. ....
Yếu tố.pop () -delete một thuộc tính cụ thể ..
Yếu tố.Xóa () -để xóa một thẻ hoàn chỉnh ..

Làm thế nào để bạn xác định một phần tử gốc trong XML?

Mỗi tài liệu XML có chính xác một phần tử gốc.Nó bao quanh tất cả các yếu tố khác và do đó, phần tử cha duy nhất cho tất cả các yếu tố khác.Các yếu tố gốc cũng được gọi là các yếu tố tài liệu.Trong HTML, phần tử gốc là phần tử. element.

XML Etree ElementTree trong Python là gì?

Mô -đun xml.etree.elementtree thực hiện API đơn giản và hiệu quả để phân tích và tạo dữ liệu XML.Thay đổi trong phiên bản 3.3: Mô -đun này sẽ sử dụng triển khai nhanh bất cứ khi nào có sẵn.implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.

Là phần tử gốc bắt buộc trong XML?

Tài liệu XML phải có một phần tử gốc..