Question about creating XML

I’m currently trying to learn about XML structures.
Taking an example from the JUCE demo code:

<DEMO_TABLE_DATA>
    <COLUMNS>
       
        <COLUMN columnId="2" name="Artist" width="150"/>
 
    </COLUMNS>
</DEMO_TABLE_DATA>

would this be the correct code to produce this (when converted back into a text document):

XmlElement DEMO_TABLE_DATA; // outter node  

XmlElement* columns = new XmlElement("COLUMNS");  
DEMO_TABLE_DATA.addChildElement(columns);

XmlElement* column = new XmlElement("COLUMN");  
column->setAttribute("columnId", 2);
column->setAttribute("name", "Artist");
column->setAttribute("width", 150);  

columns->addChildElement(column);

edit:
fixed variable names crept in from my actual code

No, that’s all a bit muddled, you’re not adding things to parents correctly.

Might be easier to understand if you use createNewChildElement() - you can find plenty of examples of that in the codebase if you search.

And it’s a lot quicker to print it out with createDocument() to see for yourself whether it works than to ask us whether it works!

1 Like