0

I have an xml file in the following format:

<event time="0.0" type="actend" person="770885_2_Person" link="271711" actType="other"  />
    <event time="0.0" type="departure" person="770885_2_Person" link="271711" legMode="car"  />
    <event time="0.0" type="PersonEntersVehicle" person="770885_2_Person" vehicle="770885_2_Person"  />
    <event time="0.0" type="vehicle enters traffic" person="770885_2_Person" link="271711" vehicle="770885_2_Person" networkMode="car" relativePosition="1.0"  />
    <event time="1.0" type="left link" link="271711"  />
    <event time="1.0" type="entered link" link="271715" vehicle="770885_2_Person"  />
    <event time="15.0" type="left link" link="271715"  />
    <event time="15.0" type="entered link" link="535413" vehicle="770885_2_Person"  />
    <event time="17.0" type="left link" link="535413"  />
    <event time="17.0" type="entered link" link="813064" vehicle="770885_2_Person"  />
    <event time="19.0" type="left link" link="813064"  />
    <event time="19.0" type="entered link" link="813066" vehicle="770885_2_Person"  />
    <event time="22.0" type="left link" link="813066"  />
    <event time="22.0" type="entered link" link="813068" vehicle="770885_2_Person"  />
    <event time="23.0" type="actend" person="368018_2_Person" link="614790" actType="other"  />
    <event time="23.0" type="departure" person="368018_2_Person" link="614790" legMode="car"  />
    <event time="23.0" type="PersonEntersVehicle" person="368018_2_Person" vehicle="368018_2_Person"  />
    <event time="23.0" type="vehicle enters traffic" person="368018_2_Person" link="614790" vehicle="368018_2_Person" networkMode="car" relativePosition="1.0"  />
    <event time="24.0" type="left link" link="614790"  />
....

A root node is missing, thus, when I do treeEvents = et.parse(file_occup_xml) I get error related to the absence of the root (when I add a root manually the above parse works fine). So i looked here and here, however, in both cases I cannot actually do the parse() command since there is no root.

Do I miss something here? How can I then add a root on that XML file?

martineau
  • 119,623
  • 25
  • 170
  • 301
user3474218
  • 51
  • 1
  • 7
  • "when I add a root manually the above parse works fine" - so do what you did manually but do it in python then post back here if you have problems. –  May 31 '21 at 13:45
  • @JustinEzequiel by manually I mean that I simply copy-pasted the required root into the xml file and then the```treeEvents = et.parse(file_occup_xml)``` works. But, I don't want to do it "manually" I want Python to add the root on the first line (and close it at the end). – user3474218 May 31 '21 at 13:48
  • 1
    Basic file input-output operations should do it. Most tutorials should cover that topic. SO is not a replacement for tutorials. –  May 31 '21 at 14:01
  • 1
    An XML file is just a text file so [basic file operations](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files) are all you need. –  May 31 '21 at 14:07
  • 1
    See https://stackoverflow.com/q/55455486/407651 – mzjn May 31 '21 at 14:14
  • Technically, by definition XML contains a root. So, check how that root-less markup was created. Usually, non-well-formed markup occurs due to string building of XML. Be sure to always use compliant DOM libraries like `etree`, `lxml`, and other libraries in other languages. See [What's so bad about building XML with string concatenation?](https://stackoverflow.com/q/3034611/1422451) – Parfait May 31 '21 at 15:36

0 Answers0