How to get path of an XML tag containing a specific value?

Hello,

I’m collecting tweets in an XML file, using this format:

<TWEETS>
	<TWEET>
		<URL>https://twitter.com/harryb_rke/status/518395866600972288</URL>
		<STATUS>Artists working as artists</STATUS>
	</TWEET>
	<TWEET>
		<URL>https://twitter.com/harryb_rke/status/517713465591734273</URL>
		<STATUS>Literally same http://t.co/br20G4IGQO</STATUS>
		<IMAGE>image2014-10-04-09-56-08-500.png</IMAGE>
	</TWEET>
</TWEETS>

How can I find the XML path of a particular TWEET if I know its URL? It’s not clear to me from the ofXML example how to do this. I’m using an ofXML object, by the way.

Thanks!

I don’t think there’s a way of searching for a particular value (I could be wrong about this), so you’d have to iterate through the list. something like:

xml.setTo("TWEETS");
if (xml.exists("TWEET")) {
    xml.setTo("TWEET[0]");
    do {
        string url = xml.getValue<string>("URL");
        if (url == URL_TO_SEARCH_FOR) {
            string status = xml.getValue<string>("STATUS");
            string image = xml.getValue<string>("IMAGE");
        }
    }
    while (xml.setToSibling());
}

if you need it to go faster, you can run through it once and put all the entries into a map.

1 Like

Thank you @genekogan! I was hoping there was some way to do it besides linear search, but this looks viable for now – I may use map as you suggest.