2011-01-09 1 views
0

안녕하세요 여러분 XML 파일을 구문 분석하고 sqlite에 데이터를 입력하는 데 문제가 있습니다. 형식은 111, AAA, BBB 등 토큰 앞에 입력해야합니다.XML에서 데이터 구문 분석 및 Python에서 DB에 저장

이 3 foreach는 이루어집니다 .NET에서
<DOCUMENT> 
<PAGE width="544.252" height="634.961" number="1" id="p1"> 
<MEDIABOX x1="0" y1="0" x2="544.252" y2="634.961"/> 

<BLOCK id="p1_b1"> 

<TEXT width="37.7" height="74.124" id="p1_t1" x="51.1" y="20.8652"> 
<TOKEN sid="p1_s11" id="p1_w1" font-name="Verdanae" bold="yes" italic="no">111</TOKEN> 
</TEXT> 
</BLOCK> 

<BLOCK id="p1_b3"> 

<TEXT width="151.267" height="10.725" id="p1_t6" x="24.099" y="572.096"> 
<TOKEN sid="p1_s35" id="p1_w22" font-name="Verdanae" bold="yes"  italic="yes">AAA</TOKEN> 
<TOKEN sid="p1_s36" id="p1_w23" font-name="verdanae" bold="yes" italic="no">BBB</TOKEN> 
<TOKEN sid="p1_s37" id="p1_w24" font-name="verdanae" bold="yes" italic="no">CCC</TOKEN> 
</TEXT> 
</BLOCK> 

<BLOCK id="p1_b4"> 

<TEXT width="82.72" height="26" id="p1_t7" x="55.426" y="138.026"> 
<TOKEN sid="p1_s42" id="p1_w29" font-name="verdanae" bold="yes" italic="no">DDD</TOKEN> 
<TOKEN sid="p1_s43" id="p1_w30" font-name="verdanae" bold="yes" italic="no">EEE</TOKEN> 
</TEXT> 

<TEXT width="101.74" height="26" id="p1_t8" x="55.406" y="162.026"> 
<TOKEN sid="p1_s45" id="p1_w31" font-name="verdanae" bold="yes" italic="no">FFF</TOKEN> 
</TEXT> 

<TEXT width="152.96" height="26" id="p1_t9" x="55.406" y="186.026"> 
<TOKEN sid="p1_s47" id="p1_w32" font-name="verdanae" bold="yes" italic="no">GGG</TOKEN> 
<TOKEN sid="p1_s48" id="p1_w33" font-name="verdanae" bold="yes" italic="no">HHH</TOKEN> 
</TEXT> 
</BLOCK> 
</PAGE> 
</DOCUMENT> 

는 "문서/페이지/BLOCK"2. "TEXT"3. "TOKEN"1. 루프 후는 내가 어떻게 얻을 해달라고 DB에 입력됩니다

+0

모든 토큰 값을 가져와야한다는 의미입니까? '[111', 'BBB', 'EEE'] 또는 [[ '111'], [ 'BBB', 'EEE']] – virhilo

답변

1

이이 있나요? : 파이썬과 내가 lxml이 모듈을 시도하고 의미

>>> xml = """<DOCUMENT> 
... <PAGE width="544.252" height="634.961" number="1" id="p1"> 
... <MEDIABOX x1="0" y1="0" x2="544.252" y2="634.961"/> 
... 
... <BLOCK id="p1_b1"> 
... 
... <TEXT width="37.7" height="74.124" id="p1_t1" x="51.1" y="20.8652"> 
... <TOKEN sid="p1_s11" id="p1_w1" font-name="Verdanae" bold="yes" italic="no">111</TOKEN> 
... </TEXT> 
... </BLOCK> 
... 
... <BLOCK id="p1_b3"> 
... 
... <TEXT width="151.267" height="10.725" id="p1_t6" x="24.099" y="572.096"> 
... <TOKEN sid="p1_s35" id="p1_w22" font-name="Verdanae" bold="yes"  italic="yes">AAA</TOKEN> 
... <TOKEN sid="p1_s36" id="p1_w23" font-name="verdanae" bold="yes" italic="no">BBB</TOKEN> 
... <TOKEN sid="p1_s37" id="p1_w24" font-name="verdanae" bold="yes" italic="no">CCC</TOKEN> 
... </TEXT> 
... </BLOCK> 
... 
... <BLOCK id="p1_b4"> 
... 
... <TEXT width="82.72" height="26" id="p1_t7" x="55.426" y="138.026"> 
... <TOKEN sid="p1_s42" id="p1_w29" font-name="verdanae" bold="yes" italic="no">DDD</TOKEN> 
... <TOKEN sid="p1_s43" id="p1_w30" font-name="verdanae" bold="yes" italic="no">EEE</TOKEN> 
... </TEXT> 
... 
... <TEXT width="101.74" height="26" id="p1_t8" x="55.406" y="162.026"> 
... <TOKEN sid="p1_s45" id="p1_w31" font-name="verdanae" bold="yes" italic="no">FFF</TOKEN> 
... </TEXT> 
... 
... <TEXT width="152.96" height="26" id="p1_t9" x="55.406" y="186.026"> 
... <TOKEN sid="p1_s47" id="p1_w32" font-name="verdanae" bold="yes" italic="no">GGG</TOKEN> 
... <TOKEN sid="p1_s48" id="p1_w33" font-name="verdanae" bold="yes" italic="no">HHH</TOKEN> 
... </TEXT> 
... </BLOCK> 
... </PAGE> 
... </DOCUMENT>""" 
>>> from lxml import etree 
>>> parsed = etree.fromstring(xml) 
>>> tokens = parsed.xpath('//TOKEN/text()') 
>>> tokens 
['111', 'AAA', 'BBB', 'CCC', 'DDD', 'EEE', 'FFF', 'GGG', 'HHH'] 
>>> 

또는? :

>>> parsed = etree.fromstring(xml) 
>>> for block in parsed.xpath('//PAGE/BLOCK/TEXT'): 
...  print block.xpath('./TOKEN/text()') 
... 
['111'] 
['AAA', 'BBB', 'CCC'] 
['DDD', 'EEE'] 
['FFF'] 
['GGG', 'HHH'] 
>>> 
+0

같은 방법으로 시도했지만 빈 목록을 얻었습니다. . "/ TOKEN/text()"에 추가되지 않았습니다. 왜 그 점을 추가합니까? ..... 어쨌든 많은 고마워요. – Rakesh

+0

점은 'here'의 상대 경로를 의미합니다. 여기서 DOCUMENT/PAGE는 현재 위치입니다./BLOCK/TEXT 요소 '/'는 도큐멘트 루트에서 시작하여 물론 './'부분을 제거하면 동일하게됩니다.) 더 자세한 내용은 google xpath를 참조하십시오. 내 대답을 표시해주세요. 받아 들여지는 무엇을 너의 평균 :) – virhilo