Adding a child using ofXML

I am trying to generate an XML data and save it as a file. Case 1 works but Case 2 doesn’t add a child. For some reason, I would like to create a child first and add it into parent later. How can I do that?

ofXml doc;
auto data = doc.appendChild(“data”);

ofXml doc;
ofXml data;