XML output with specifically ordered attributes

Python:

I’ve got a tool that has to output xml documents, and the attributes in the element tags have to be, just for shits and giggles, in a specific order.

I have an object representation for the xml document, it’s a class and it’s using Minidom at the moment, but I’m not married to this. Minidom doesn’t allow specifying attribute order. As of now I’m actually considering adding something into my class’s write() method that gets a string from the toprettyxml, then takes each tag and rearranges the interior contents of the tag. This seems like an awful Idea, but I’m sort of at a loss for how explicitly order the attributes otherwise. anyone have any awesome suggestions?

-t

Is it possible for you to store the attributes and their values as child elements instead? That way the order will be consistent.

The XML standard is that attribute order doesn’t matter. As a reflection of this minidom uses a dict to represent the attributes, which is obviously unordered, and there’s no quick way to change this behavior without re-writing chunks of minidom. (And you could argue it’s not a good idea anyway, since it contravenes the XML standard.)

What Bronwen said… Having the order of attributes matter is very flaky to say to least - if you ever run that data thru a different xml read/write system it’ll most likely break in a very ugly way.

Can you work around this with having something like an index attribute in each of the attributes which are order dependent? Then query your node tree based on the values of the index?

<data index=“1” value="foo>Hello</data>
<data index=“2” value=“bar”>World</data>

SamiV.

I would (and have) brought up the point that this is not a good idea, however the engineers that wrote the pipeline stuff that uses the data is no longer with the company, and since the tool will be deprecated in the next year or two, no one wants to make a change to it.

I’ve got a currently working fix in by just creating my own write() method for the object, and enforcing an ordering based on data from an xml configs file (parses back to a dict of elements and valid/properly ordered attributes).

The way we found that it requires specific ordering was that I tried to read/write it and it broke. In a very ugly way.

-t

I think what Bronwen and Sami are saying is, ‘Fix the fucking problem.’ The tool isn’t going to be deprecated in the next year or two, you have no idea if/when it will happen, and you’ll probably spend more time working around this and solving problems with it than if you just fix it properly.

There are things that are fine bandaiding and things that need to be fixed properly. I don’t think I could ever see a bug like this and consider a bandaid acceptable.

since the tool will be deprecated in the next year or two

That sentence alone suggests you should fix the problem and work-around it.

That’s a very long time - could be it’s your successor who gets to wrangle the same problem. And usually it’s never been that upgrades / pipeline changes happen as planned - if it still works - hey why not keep using it…

I don’t know what your exact skillsets are - but even if the code reading the XML is on C++ (and you’re not familiar with it), usually XML tree parsing code is surprisingly straight forward (well come to think of it, if your XML parser depends on the attribute order, it’s most likely some very ugly hack and not a real XML reader library - so you’ll be in a world of pain fixing it anyway).

SamiV.

Sounds like your idea to brew your own XML writer functionality that can handle specifically ordered attributes is the best solution available…

But in general, I agree that requiring specifically ordered attributes sort of defeats the purpose of XML to begin with. The original implementation should have just used text file or something.

I agree that I shouldn’t have to solve this problem with a custom writer, but I doubt I’m going to get anywhere with getting the pipeline exe changed.

I don’t have any experience with C++ and I don’t know anything about the internal workings of the tool, so I don’t want to risk bringing down the pipeline by tinkering around in a system I don’t understand at all. C++ isn’t a language I feel comfortable just stepping into and making fixes if I don’t actually understand the language at all. I am looking to start learning C-based languages soon (C, C++, C#), but while I don’t have the experience, if engineering isn’t willing to fix it, then I really don’t feel comfortable doing anything more active than just reading the code.

Well, if you’re stuck with it, just make sure to document the heck out of your hack, explaining why you’re doing what you’re doing, so that anyone that has to edit the tool is aware of this landmine.

In the meantime, why don’t you make fixing this part of your goal for learning C++? It’s always easier to learn a language when you have a problem you need to solve. Gives you focus, keeps you motivated.

And maybe your team will be willing to help if you tell them you’d like to learn enough to be able to solve problems like this without having to involve them. You could end up with a C++ mentor.