Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the recommended structure for writing a list of integers on XML?

Tags:

python

xml

I have a list of integers [22,23,64,65,9] and what is the best way to store it over an xml file

<cls>
  <cl value="22" />
  <cl value="23" />
  <cl value="64" />
  <cl value="65" />
  <cl value="9" />
</cls>

I now have the above structure and is it advisable to use the same ? my end goal is to parse it back to python list.

like image 234
Yogi Avatar asked Jan 22 '26 00:01

Yogi


1 Answers

Your snippet is all right, but something like

<cls>22,23,64,65,9</cls>

would be OK and faster if parsed by python like this:

[int(x) for x in xml_string.split(",")]

because the xml parser has less work to iterate on the nodes. The "all in one" approach is shorter in bytes (because you don't need <cl value="" /> so the data is way smaller, particularly on a big list), and thus consumes less resources (always time various approaches).

The only issues I'm seeing:

  • you're creating a custom sub-format to xml
  • if you're trying to do the same with strings, and they contain commas, you're back to square one: in that case, leave the list to xml parser.
  • if you were doing that in C, that would require much more code like strtok to parse, and a lot of tedious string operations to create. So if you share your format with a C program, the C coder will hate you for this :)
like image 115
Jean-François Fabre Avatar answered Jan 26 '26 00:01

Jean-François Fabre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!