I'm not going to write the code for you, but I did have a similar requirement that a particular element could exist prior to me trying to insert it. So I wrote a quick "xml_child" routine that would look for a specific element, return it if it were there, but if it weren't, it would create it, then return that. You can also provide parms for setting some stuff if the element is being created, or other parms for setting stuff whether it's being created or updated.
Note that this was my first time using XML of any type, let alone XML::Twig, so be gentle with any criticisms ;-)
Example usage:
$child = xml_child(parent => $elt, child => 'built', action => 'after'
+, relto => $prev_child, def_text => '19700101 000000.000-0000');
$prev_child = $child; $child = xml_child(parent => $elt, child => 'sha
+reable', action => 'after', relto => $prev_child, override_text => 't
+rue');
Here I'm creating a series of elements, and even specifying order by the order I insert (although if it's already there out of order, then I won't correct the order). I also have some default text which won't change anything that is already present but I have some override text that says that I don't care what the XML owner says, I'm changing the shareable element to be "true".
This is probably adaptable to what you sound like you need.
If you're wondering why the lack of whitespace in the example - it's because I have about 30 of these... and I just needed to get it done - after it was done, it was more important to be able to pgup/pgdn through past these constants than to make it readable. ;-> If I were doing it again, I'd find a way to put this data somewhere else and loop through it. |