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?

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

Case2:
ofXml doc;
ofXml data;
doc.appendChild(data);