다음 xml 파일 (lieferungen.xml)에는 여러 불일치가 있습니다.Python SAX 파서 프로그램이 잘못된 결과를 계산합니다.
은 파일의 모든 불일치를 찾기 위해<?xml version="1.0" encoding="UTF-8"?>
<lieferungen xmlns="urn:myspace:lieferungen" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:myspace:lieferungen C:\xml\lieferungen.xsd">
<artikel id="3526">
<name>apfel</name>
<preis stueckpreis="true">8.97</preis>
<lieferant>Fa. Krause</lieferant>
</artikel>
<artikel id="7866">
<name>Kirschen</name>
<preis stueckpreis="false">10.45</preis>
<lieferant>Fa. Helbig</lieferant>
</artikel>
<artikel id="4444"> <!--DIFFERENT ID FOR apfel!! -->
<name>apfel</name>
<preis stueckpreis="true">12.67</preis>
<lieferant>Fa. Liebig</lieferant>
</artikel>
<artikel id="7866">
<name>Kirschen</name>
<preis stueckpreis="false">17.67</preis>
<lieferant>Fa. Krause</lieferant>
</artikel>
<artikel id="2345"> <!--DIFFERENT ID FOR apfel!! -->
<name>apfel</name>
<preis stueckpreis="true">9.54</preis>
<lieferant>Fa. Mertes</lieferant>
</artikel>
<artikel id="7116"> <!--DIFFERENT ID FOR Kirschen!! -->
<name>Kirschen</name>
<preis stueckpreis="false">16.45</preis>
<lieferant>Fa. Hoeller</lieferant>
</artikel>
<artikel id="7868">
<name>Kohl</name>
<preis stueckpreis="false">3.20</preis>
<lieferant>Fa. Hoeller</lieferant>
</artikel>
<artikel id="7866">
<name>Kirschen</name>
<preis stueckpreis="false">12.45</preis>
<lieferant>Fa. Richard</lieferant>
</artikel>
<artikel id="3245">
<name>Bananen</name>
<preis stueckpreis="false">15.67</preis>
<lieferant>Fa. Hoeller</lieferant>
</artikel>
<artikel id="6745"> <!--DIFFERENT ID FOR Kohl!! -->
<name>Kohl</name>
<preis stueckpreis="false">3.10</preis>
<lieferant>Fa. Reinhardt</lieferant>
</artikel>
<artikel id="7789">
<name>Ananas</name>
<preis stueckpreis="true">8.60</preis>
<lieferant>Fa. Richard</lieferant>
</artikel>
</lieferungen>
, 나는 파이썬에서 다음 색소폰 파서를 썼다 : 항목 중 일부는 하나 개 이상의 ID (예 : 항목 "apfel"3 개 개의 ID를 가지고이)가
이 파일 (하나 개 이상의 ID의 발생을 표시하는 의견 주)에서 볼 수 있듯이Inconsistencies:
There are 3 different IDs for item "Kirschen".
,이 출력은 두 가지 방법으로 잘못 다음과 같이
import xml.sax
import sys
class C_Handler(xml.sax.ContentHandler):
def __init__(self):
self.items = {}
self.items2 = {}
self.read = 0
self.id = 0
def startDocument(self):
print("Inconsistencies:\n")
def startElement(self, tag, attributes):
if tag=="name":
self.read = 1
if tag=="artikel":
self.id = attributes["id"]
def endElement(self, tag):
if tag=="name":
self.read = 0
def characters(self, content):
if self.read == 1:
item = content
#check whether the item is not yet part of the dictionaries
if item not in self.items:
#add item (e.g. "apfel") to both dictionary "items" and
#dictionary "items2". The value for the item is the id in the
#case of dictionary "items" and "0" in the case of dictionary
#"items2". The second dictionary contains the number of
#inconsistencies for each product. At the beginning, the
#number of inconsistencies for the product is zero.
self.items[item] = self.id
self.items2[item] = 0
else:
if self.items[item] == self.id:
#increase number of inconsistencies by 1:
self.items2[item] = self.items2[item] + 1
def endDocument(self):
for prod in self.items2:
if self.items2[prod]>0:
print("There are {} different IDs for item \"
{}\".".format(self.items2[prod] + 1, prod))
if (__name__ == "__main__"):
c = C_Handler()
xml.sax.parse("lieferungen.xml", c)
이 프로그램의 출력은 다음과 같습니다
- "Kirschen"항목에는 3 가지가 아닌 2 가지 ID 만 있습니다. 하나 개 이상의 ID와
- 여러 항목이 전혀 언급되지 않은 그러나, 나는 이해가 안
를 (예를 들어, 항목 "콜 것은"두 개의 서로 다른 ID를 가지고), 내 코드에서 잘못된 무슨 일이 일어나고 있는지. Kirschen
가 사용 : 내가 오해 한 않는
대단히 감사합니다! – Tommy