Is it possible to edit the existing XML file with TXMLEngine in ROOT?

I know that reading and creating XML files is available in ROOT with TXMLEngine. But what if I want to edit the existing XML file? Particularly, I want to add a child to the existing node. How one could reach this?

I tried the following code:

    //some code above
    XMLNodePointer_t generalInfo = findChild( "GeneralInfo", mainNode, xml ); 

    XMLNodePointer_t info = xml->NewChild( generalInfo, 0, "Info" );
    xml->NewAttr( info, 0, "name", "TrainingSignalEvents");
    xml->NewIntAttr( info, "value", 1000 );

    xml->SaveDoc( xmldoc, fileName );

    xml->FreeDoc( xmldoc );
    delete xml; 

Where findChild is function that I wrote to simplify finding of child by name. It worked in this way:

    <Info name="TMVA Release" value="4.2.1 [262657]"/>
    <Info name="ROOT Release" value="6.08/02 [395266]"/>
    <Info name="Creator" value="wolfgang"/>
    <Info name="Date" value="Fri Nov 24 23:49:39 2017"/>
    <Info name="Host" value="Linux wolfgang-IdeaPad-Z585 4.4.0-81-generic #104~14.04.1-Ubuntu SMP Wed Jun 14 12:45:52 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux"/>
    <Info name="Dir" value="/home/wolfgang/BINP/Diploma/Analysis/omega_pi0/Algorithm"/>
    <Info name="Training events" value="28324"/>
    <Info name="TrainingTime" value="1.15905461e+01"/>
    <Info name="AnalysisType" value="Classification"/>
    <Info name="TrainingSignalEvents" value="1000"/>
  </GeneralInfo>

You see that new child Info has been added. But I have two questions here. Is it possible to specify where to place new child? And is it a proper way to add new child in the existing file? I mean just SaveDoc in the end?

UPDATE: One more question. Is it possible to remove single child from the node? If yes, how?

Thanks in advance.

Hi,

I guess you asking possibility to modify existing XML file with TXMLEngine?

Yes, it is possible.
Look xmlnewfile.C and xmlreadfile.C macros in tutorials/xml/ directory.
After existing file parsed in xmlreadfile.C macro, you can insert/delete/modify any of it elements and store in new XML file.

Regards,
Sergey

Thanks for your early reply. See edit, please.

New child node always add to the end of children list.
There is possibility to move child node to the beginning of childs list with
TXMLEngine::AddChildFirst() function. You should do in your code:

XMLNodePointer_t generalInfo = findChild( "GeneralInfo", mainNode, xml ); 

XMLNodePointer_t info = xml->NewChild( generalInfo, 0, "Info" );
xml->NewAttr( info, 0, "name", "TrainingSignalEvents");
xml->NewIntAttr( info, "value", 1000 );

xml->UnlinkNode(info);
xml->AddChildFirst(generalInfo, info); 

xml->SaveDoc( xmldoc, fileName );

xml->FreeDoc( xmldoc );
delete xml; 

Unfortunately, there is no function to insert node somewhere in the middle of children list

It is necessary to call UnlinkNode(info) before SaveDoc if I do not want to place new child to the beginning of the parent child list?

UnlinkNode(info) required only if you want to move it to some other place.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.