xml2csv.py#
This converts any of the sumo xml files into a flat csv (character separated values) representation. CSV-Files are easy to parse and can be opened in LibreOffice or Microsoft Excel. Usage:
python tools/xml/xml2csv.py input.xml
With the option --separator <STRING> you can customize the field separator (default is ;), the option --quotechar <STRING> defines a quoting character such as ' which will be used to quote all fields. Furthermore you can supply an xsd schema file using --xsd <FILE> to aid the parsing and get headings also for attributes which may be not in the current file. If you give the additional --validation option the input file will be validated against the schema. This requires lxml to be installed.
If you do not use the --xsd option the script will determine the attributes to parse from the first line (or block) it parses. If your file does not contain all wanted attributes in the first occurring element they will not get parsed!
Please note that instead of an input file name you can give a number which will be interpreted as a server port where the script listens for incoming connections and then reads the input data from. The name for the output file can be given using the --output <FILE> option with the default being the path of the input file with the ".xml" suffix replaced by ".csv".
The application is limited to simple hierarchies, where the root element may contain several different child elements but all of those are limited to only one type of child elements.
Note
Excel may be able to open XML files directly. see[1].
csv2xml.py#
This is the inverse tool to xml2csv.py. Usage:
python tools/xml/csv2xml.py -x schema.xsd input.csv
The options have the same meaning as above. For some file types as nodes, edges and routes the conversion is hard coded for all the others a schema must be provided. It is not possible to automatically perform validation on the output.
xml2protobuf.py#
If the CSV representation is still too big you can compress the output even further using Google's Protocol Buffers. This will require the protoc compiler as well as the protobuf packages for python and your target language to be installed. Usage:
python tools/xml/xml2protobuf.py -x schema.xsd input.xml
The xsd schema file needs to be given the using --xsd <FILE> and the --validation option will enable XML validation. The additional --protodir <FILE> option will give a directory where the protobuf message definitions and the generated python module will be saved (default is the current working dir). The script will generate the protobuf description from the schema and then write a protomsg file containing the binary stream.
protobuf2xml.py#
This is the inverse tool to xml2protobuf.py. Usage:
python tools/xml/protobuf2xml.py -x schema.xsd input.protomsg
The options have the same meaning as above.
changeAttribute.py#
This sets/removes the specified attribute on all tags (elements) in the input file and writes the result to a new file If option --value (-v) is not given, the attribute is removed. Otherwise it is added/modified to the given value.
python tools/xml/tools/xml/changeAttribute.py -f INPUT_FILE -o OUTPUT_FILE -t TAG -a ATTRIBUTE [-v VALUE]
filterElements.py#
This removes the specified element from the input file subject to the following conditions:
- all occurrences of the element (default)
- all elements that have a specified attribute matching a value from a given list (--attribute ATTR --remove-values VAL1,VAL2)
- all elements that have a specified attribute not matching a value from a given list (--attribute ATTR --keep-values VAL1,VAL2)
Example call:
python tools/xml/tools/xml/filterElements.py -f stopoutput.xml -o filtered.xml -t stopinfo -a delay --remove-values 0