2014-04-11 30 views
5

Ngày tốt lành mọi người.Python ElementTree "không tìm thấy phần tử" ngoại lệ

Tôi đang cố đọc, phân tích cú pháp và sử dụng tệp xml bằng ElementTree. Dữ liệu sau:

<level> 
    <leveldata> 
     <level name="hh" difficulty="Easy" lenght="3600"> 
      <meteorite chance="4" speed="3" > 
       <image id="1"> 
       <image id="2"> 
       <image id="3"> 
      <meteorite /> 
      <meteorite chance="4" speed="3" > 
       <image id="4"> 
       <image id="5"> 
       <image id="6"> 
      <meteorite /> 
     <level /> 
    <leveldata /> 
    <meteorimages> 
     <meteor id="5" imagepath="res\meteorit_1.png"> 
     <meteor id="5" imagepath="res\meteorit_2.png"> 
     <meteor id="5" imagepath="res\meteorit_3.png"> 
    <meteorimages /> 
<datasheet /> 
<level /> 

Đáng buồn thay, tôi ElementTree đưa ra một ngoại lệ! Đọc các tập tin với mã sau:

import xml.etree.ElementTree as ET 
***code*** 
tree = ET.parse("res\\data.xml") 
root = tree.getroot() 

Ngoại lệ:

File "E:\blabla\core.py", line 26, in load_levelproperties 
    *tree = ET.parse("res\\data.xml")* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line 
1182, in parse 
    *tree.parse(source, parser)* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line 
657, in parse 
    *self._root = parser.close()* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line 
1654, in close 
    *self._raiseerror(v)* File "E:\Programme(x86)\Python2.7x86\lib\xml\etree\ElementTree.py", line 
1506, in _raiseerror 
    ***raise err xml.etree.ElementTree.ParseError: no element found: line 16, column 9*** 

tôi không thể tìm ra những gì là sai, tôi đã cố gắng để thay đổi data.xml bằng mọi cách có thể, tôi có thể tưởng tượng, không có Sự khác biệt. Nó luôn luôn là dòng cuối cùng của tập tin! Tôi đang làm gì sai? Cảm ơn!

Trả lời

4

thẻ của bạn không đóng đúng cách. Ví dụ: để đóng thẻ "thiên thạch", hãy sử dụng </meteorite> không <meteorite />.

+1

Có vẻ như tôi thuộc nhóm tiệc chào mừng, hôm nay! Chào mừng bạn đến với Stack Overflow @grvsmth. Chúc mừng Tiến sĩ của bạn và tôi hy vọng rằng bạn sẽ 'while (yourStay) {try {StackOverflow} catch {enjoyment}}'. Vâng, đó là một chút, nhưng, hài hước là một trong những điều cuối cùng mà con người chúng ta vẫn có thể làm tốt hơn máy tính. –

+1

Vâng THANK you. Đó là những gì tôi cần. – Nearoo

+0

Rất vui được giúp đỡ và cảm ơn sự chào đón! – grvsmth

6

Bạn XML không được định dạng đúng, ElementTree không thể phân tích cú pháp - nó thực sự trông giống như là một phần của tài liệu thực.

Dưới đây là những gì bạn nhận được nếu bạn định dạng nó:

<level> 
    <leveldata> 
     <level name="hh" difficulty="Easy" lenght="3600"> 
      <meteorite chance="4" speed="3"> 
       <image id="1"> 
        <image id="2"> 
         <image id="3"> 
          <meteorite/> 
          <meteorite chance="4" speed="3"> 
           <image id="4"> 
            <image id="5"> 
             <image id="6"> 
              <meteorite/> 
              <level/> 
              <leveldata/> 
              <meteorimages> 
               <meteor id="5" imagepath="res\meteorit_1.png"> 
                <meteor id="5" imagepath="res\meteorit_2.png"> 
                 <meteor id="5" imagepath="res\meteorit_3.png"> 
                  <meteorimages/> 
                  <datasheet/> 
                  <level/> 
+0

Ý của bạn là gì theo "định dạng"? Tôi nên làm như thế nào tốt hơn? – Nearoo

+0

@ user3424423 Tôi vừa thụt vào xml để dễ thấy rằng xml không được cấu trúc chính xác. Elementtree không thể xử lý nó. – alecxe

+0

Điều này không giúp tôi chút nào. Tôi phải làm gì hơn? – Nearoo

Các vấn đề liên quan