0

Need to know the quickest way to parse an xml that looks similar:

<DocumentRequest>
  <RequestDateTime>2000-01-10T04:00:00</RequestDateTime>
  <Unit>CHECK</Unit>
  <Doc>YOU_PAY_ME</Doc>
  <Version>1</Version>
  <Batched>Y</Batched>
  <Counter>10559</Counter>
  <Total>1000</Total>
</DocumentRequest>

the file then repeats the pattern several times.

I'm looking for a way to import the xml from a folder and then parsing the data into a nice data table.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • That doesn't actually look like XML, but if you're interested in parsing XML in Python, take a look at these: http://docs.python-guide.org/en/latest/scenarios/xml/ – alykhank May 03 '16 at 20:59
  • ??? What is not XML about it? I just parsed it using `xml.etree.ElementTree` without a hiccup. – PaulMcG May 03 '16 at 21:26
  • @PaulMcGuire do you mind dropping me the script you used? I appreciate it. I get an error when importing the file which is: File "", line unknown ParseError: junk after document element: line 2, column 0 – user3100446 May 03 '16 at 23:10
  • Ah, but I didn't use a file, I just pasted that string into a Python string variable, made it into a StringIO so that it was a file-like object, and passed it to `xml.etree.ElementTree.parse`. I think the exception you are getting may be from this: "the file then repeats the pattern several times" - there has to be a single root element for it to be valid XML. If you can't control the content of the file, then read the file into a string, add leading and trailing root tags, and then parse it like I did, by wrapping the string in a StringIO. – PaulMcG May 04 '16 at 05:56

0 Answers0