xml.parsers.expat.ExpatError:找不到元素:第1行,第0列

时间:2015-12-01 03:03:44

标签: python xml

我是Python的新手。我正在尝试编写一个脚本来修改我需要用于测试目的的XML文件中的一些数据。我确实看到一些人问同样的问题,但我无法从答案中找出我做错了什么。我很感激帮助。这是我要修改的示例XML。谢谢你的帮助

 <?xml version="1.0" encoding="UTF-8"?>
<Parent>
  <FlexIDResponseEx xmlns="http://WWW.YYYY.com/SsIkkk">
    <response>
      <Header>
        <Status>0</Status>
      </Header>
      <Result>
        <InputEcho>
          <Name>
            <First>John</First>
            <Last>Doe</Last>
          </Name>
          <Address>
            <StreetAddress1>XX XXX XX XXXXX</StreetAddress1>
            <City>XXXXXX</City>
            <State>XX</State>
            <Zip5>XXXXX</Zip5>
          </Address>
          <Age>0</Age>
          <SSN>XXXXXX</SSN>
          <HomePhone>XXXXXXXXX</HomePhone>
        </InputEcho>
        <UniqueId>XXXXX</UniqueId>
        <NameAddressPhone>
          <Summary>1</Summary>
          <Type>P</Type>
          <Status>C</Status>
        </NameAddressPhone>
        <VerifiedElementSummary>
          <DOB>XXXXXXXX</DOB>
          <DOBMatchLevel>8</DOBMatchLevel>
        </VerifiedElementSummary>
        <ValidElementSummary>
          <SSNValid>0</SSNValid>
          <SSNDeceased>0</SSNDeceased>
          <PassportValid>0</PassportValid>
          <AddressPOBox>0</AddressPOBox>
          <AddressCMRA>1</AddressCMRA>
          <SSNFoundForLexID>0</SSNFoundForLexID>
        </ValidElementSummary>
        <NameAddressSSNSummary>2</NameAddressSSNSummary>
        <ComprehensiveVerification>
          <ComprehensiveVerificationIndex>50</ComprehensiveVerificationIndex>
          <RiskIndicators>
            <RiskIndicator>
              <RiskCode>A</RiskCode>
              <Description>Null</Description>
              <Sequence>1</Sequence>
            </RiskIndicator>
            <RiskIndicator>
              <RiskCode>MS</RiskCode>
              <Description>XXXXXX</Description>
              <Sequence>2</Sequence>
            </RiskIndicator>
            <RiskIndicator>
              <RiskCode>25</RiskCode>
              <Description>XXXXX</Description>
              <Sequence>3</Sequence>
            </RiskIndicator>
            <RiskIndicator>
              <RiskCode>74</RiskCode>
              <Description>The input phone number is associated with a different name and address</Description>
              <Sequence>4</Sequence>
            </RiskIndicator>
            <RiskIndicator>
              <RiskCode>52</RiskCode>
              <Description>The input first name is not associated with input SSN</Description>
              <Sequence>5</Sequence>
            </RiskIndicator>
          </RiskIndicators>
        </ComprehensiveVerification>
        <CustomComprehensiveVerification>
          <ComprehensiveVerificationIndex>0</ComprehensiveVerificationIndex>
        </CustomComprehensiveVerification>
        <InstantIDVersion>1</InstantIDVersion>
      </Result>
    </response>
  </FlexIDResponseEx>
</Parent>

这是我试图编写的python脚本

import os
from xml.dom.minidom import parse

fileList = []
p = os.popen('ls *JJ*',"r")

while 1:
    line = p.readline()
    if not line: break
    fileList.append(line)

j = len(fileList)+1


for i in range(1, j):
    lnFile = open(fileList[i], "rb")
    tree = parse(lnFile)
    first = tree.find('First')
    first.text = 'YYYY'
    Last = tree.find('Last')
    Last.text = 'ZZZZ'
    tree.write(fileList[i])
    lnFile.close()

这是堆栈跟踪

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/parser/parse.py
Traceback (most recent call last):
  File "/Users/parser/pare.py", line 17, in <module>
    tree = parse(lnFile)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/minidom.py", line 1958, in parse
    return expatbuilder.parse(file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/expatbuilder.py", line 913, in parse
    result = builder.parseFile(file)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/xml/dom/expatbuilder.py", line 211, in parseFile
    parser.Parse("", True)
xml.parsers.expat.ExpatError: no element found: line 1, column 0

Process finished with exit code 1

P.S我正在使用PyCharm CE

0 个答案:

没有答案