XmlElemt removeChildElement() usage


#1

Hi, I am trying to implement a music database search similar to the search box in iTunes and other players. So far I have parsed the iTunes library xml database into the same form as used in the juce table demo, this displays fine. Now I am trying to impliment the search by making a copy of the whole library and removing entries that don’t contain the search string.

I am using the forEachXmlChildElement and then the removeChildElement() method. The search algorithm must be working because the debug statement in the else braces returns the correct XmlElements (the ones that should be kept). However only the first child from the parent that doesn’t match the string is ever removed, not everything that doesn’t get printed.

forEachXmlChildElement(*parent, e)
{
	if (!e->getAttributeValue(columns::Artist).containsIgnoreCase(newFiltertext))
	{
            	parent->removeChildElement(e, true);
	}
	else {
		DBG(e->createDocument(""));
	}
}

Not sure what’s going on here, does the XmlElement structure of the library get messed up when elements are removed or something as it can’t seem to remove more that one element.

Any help would be much appriciated.


#2

Ok, two things:

I physically winced when I read that you were doing a search by copying an entire database and removing the elements don’t match. Honestly, don’t you think that’s just a tiny bit insane!?

And modifying a list while you’re iterating it is something you should always think about really carefully. You can never safely remove elements if you’re iterating forwards (think about it…) And in this case it’s actually a linked list, so you’re removing an element, then examining that element to find out the next one in its list, but it’s no longer even in a list! I’m actually surprised it didn’t crash.


#3

Yeh, it did seem a bit crazy and obviously not the best solution but I’m very new to this Xml stuff so needed to start somewhere. I did this more so I could just get an idea of what the Xml classes actually do and relate to rather than have a perfect solution.

I guess its back to the drawing board then as this is obviously the wrong approach.


#4

It’s not a very hard problem. Just iterate the xml and keep an array of pointers to the elements that match, rather than actually bothering to make a copy of them. As long as you don’t modify your original data while you’re using them, that’d be very fast.


#5