Vencabot - 2007-08-24

Hey there, TinyXML. Before I say anything else, let me say that I love the TinyXML library; I tried XERCES first for my XML parsing, but it was way, way overwrought for something that I imagined should be so much simpler (my XML DOM experience comes from JavaScript). When I asked a friend if there was anything more usable available, he suggested I look you up, and I'm very pleased.

Anyway, it's my JavaScript experience that brings me to the point that I want to bring up: why shouldn't TinyXML have a getElementsByTagName() or a getElementByID() function? These commands make document navigation much simpler. I've searched the forums for similar posts, and found one where Lee suggested that the user use "GetChildElement()," but when I grep'd my tinyxml folder for "GetChildElement()," nothing at all came up.

What I want, and which seems as if it should be fairly easy to implement, is a function that searches an XML document for ALL elements of a certain type (tag name) and returns an Array that contains them in the order that they appear.

For instance, say I have:

<root>
<element1>VT</element1>
<element1>is the</element1>
<element2>
  <element3>applesauce</element3>
</element2>
<element1>God of Rock</element1>
</root>

I could use getElementsByTagName( "element1" ), which would return an array where elementsarray[0]->GetText() would return "VT," elementsarray[1]->GetText() would return "is the," etc. In JavaScript, this is a very convenient way to get the data that you need from an HTML or XML document.

As for "getElementById()," I understand that the freedom of XML means that "IDs" aren't standard, but it could be changed to something like, "getElementsByAttribute( name, value )," and that would allow someone to get ALL elements with a certain attribute and store it in an array, or, even more conveniently, say that they DO use IDs:

<root>
<element1>VT</element>
<element1>is the</element>
<element2>
  <element3
   id="myelement">applesauce</element3>
</element2>
<element1>God of Rock</element1>
</root>

We could simply getElementsByAttribute( "id", "myelement" ), and the function would make an array of all elements with that attribute, and with that value... We'd get an Array of one TiXmlElement, and we'd know elementarray[0] was the element that we we're searching for!

I don't think that either of these functions should be too difficult to implement, but I think that they'd be a huge addition to TinyXML. If no more experienced developers are up for it, I intend to give it a try myself, since I'd certainly like to make use of these functions in my programs... If I can work something out, I'll post it here -- that is, if nobody else wants to give it a shot before that.

Thanks for reading, and again, great job with TinyXML.

-David