user2831586
user2831586

Reputation: 119

parsing xml file in python - no element found

I'm a python beginner.

I want to be able to pick values of certain elements in an xml sheet. Below is what my xml sheet looks like:

  <TempFolder>D:\Mooniology\DiSecTemp\160708_M02091_0202_000000000-APC99</TempFolder>
  <AnalysisFolder>D:\Mooniology\MiSeqAnalysis\160708_M0209831_0202_000000000-APC99</AnalysisFolder>
  <RunStartDate>160708</RunStartDate>
  <MostRecentWashType>PostRun</MostRecentWashType>
  <RecipeFolder>D:\Mooniology\MiSeq Control Software\CustomRecipe</RecipeFolder>
  <ILMNOnlyRecipeFolder>C:\Mooniology\MiSeq Control Software\Recipe</ILMNOnlyRecipeFolder>
  <SampleSheetName>20160708 ALK Amplicon NGS cDNA synthesis kit comparison</SampleSheetName>
  <SampleSheetFolder>Q:\GNO MiSeq\Jaya</SampleSheetFolder>
  <ManifestFolder>Q:\GNO MiSeq</ManifestFolder>
  <OutputFolder>\\rpbns4-lab\vol10\RMSdisect\160708_M02091_0202_000000000-APC99</OutputFolder>
  <FocusMethod>AutoFocus</FocusMethod>
  <SurfaceToScan>Both</SurfaceToScan>
  <SaveFocusImages>true</SaveFocusImages>
  <SaveScanImages>true</SaveScanImages>

And by "picking values", suppose I want the value of the element called TempFolder. I want the script spit out D:\Mooniology\DiSecTemp\160708_M02091_0202_000000000-APC99 Below is the code I'm using to attempt to scan it:

#!/usr/bin/python2.7

import xml.etree.ElementTree as ET
tree = ET.parse('online.xml')
root = tree.getroot()
for child in root:
    print(child.tag, child.attrib)

Every time i run this code, no matter how i modify it (from researching google), the end result is always the following error:

Traceback (most recent call last):
  File "./mindo.py", line 5, in <module>
    tree = ET.parse('online.xml')
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1182, in parse
    tree.parse(source, parser)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 657, in parse
    self._root = parser.close()
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1654, in close
    self._raiseerror(v)
  File "/usr/lib64/python2.7/xml/etree/ElementTree.py", line 1506, in _raiseerror
    raise err
xml.etree.ElementTree.ParseError: no element found: line 75, column 0

I suspected that the issue could be the xml file I'm using. But since I'm new to python, i have to presume its my code.

Upvotes: 0

Views: 4625

Answers (1)

chenchuk
chenchuk

Reputation: 5742

This is because the XML is not well formatted and therefore is not parsable:

In [4]: tree = ET.parse('online.xml')
   ...: 
  File "<string>", line unknown
ParseError: junk after document element: line 2, column 2

the xml need to have root element ie :

  <params>
    <TempFolder>D:\Mooniology\DiSecTemp\160708_M02091_0202_000000000-APC99</TempFolder>
    <AnalysisFolder>D:\Mooniology\MiSeqAnalysis\160708_M0209831_0202_000000000-APC99</AnalysisFolder>
    <RunStartDate>160708</RunStartDate>
    <MostRecentWashType>PostRun</MostRecentWashType>
    ...
    ...
    ...
  </params>

Upvotes: 1

Related Questions