<chatting with a friend today>
James: I’m writing a fairly simple xml library designed mainly to be used to read configuration data (but can be used for any simple xml processing). The main usage is, most likely, you load an xml document and then query it for elements by name, etc. However, for reading configuration data, usually you just want one or two elements and don’t care about the rest of the document. For that, it’d be much more efficient to just seek into the stream itself for element tags with a particular name. I’m trying to decide what would be the best way to design the class so that you can use it for either loading method.
me: Well, first off, I’m tempted to ask why the hell you’re designing something that’s probably been written 100 times already. But, ignoring that: XML has its own built-in structure. You can’t identify things by element, you have to identify them by path or else they are meaningless. There’s a huge difference between root.person.dad.weight and root.person.mom.weight. You can’t just search for the weight tag.
James: Sorry, by name I meant to imply the path: ‘fully-qualified-name’
me: Ok. Then once you’ve loaded the data into some kind of structure, wouldn’t it always be faster to locate by path index and not by text search?
James: That’s jumping the issue, though. I’m talking about before even loading the data into a structure. Search the stream for a particular element you want.
me: You’d probably have to go with a hybrid approach. To find the element in the stream, you’d have to parse the stream anyway, at least until you got to the element you wanted. Because they don’t just sit together. I guess you could skip anything that didn’t fit. If it were me, I guess I’d make a single class / function that could operate either way. Search/discard or full parse/load. The operation is basically the same, except that in one case, you skip around based on your parameters.
James: I was thinking about templating the class so then you can use the same functions.
me: (highly amused, thinking what a bunch of fucking nonsense this would look like to anyone who isn’t nose deep in this shit like we are)
James: Yep. Don’t you just love programming? ;)