Basic XML File Creation Help

Hey Guys,

i try to write some data in my GUI-Application to an xml File.
My target is to have a simple GUI, where is an editable label, and if i put some Text in there, that the App saves it in a XML File, and if i reopen the app that it reads from the XML File and puts the data back in the Labelfield.

I’m really new to c++ and Juce, but i guess i need to use the XMLElement, XMLDocument and XMLElement::writeToFile functions / classes, right?

May someone could give a simple step by step how they exactly work? The documentation is kind of confusing me.

Greetings,
Manuel

Here is some code I snipped out of one of my projects. I’ve left in just enough to help you see the basic write/read process.

void cParquetDeformationPlaygroundComponent::SaveParquetDeformationFile(const File& _parquetDeformationFile)
{
    XmlElement mainElement(kParquetDeformationMainTag);
    mainElement.setAttribute(kParquetDeformationVersionTag, String(kParquetDeformationVersion));
    mParquetDeformationComponent.ExportXml(mainElement);
    mainElement.writeToFile(_parquetDeformationFile, String::empty);
}

void cParquetDeformation::ExportXml(XmlElement& _parentElement)
{
    XmlElement* parquetDeformationElement = new XmlElement(kParquetDeformationMainTag);
    _parentElement.addChildElement(parquetDeformationElement);

    XmlElement* nameElement = new XmlElement(kParquetDeformationNameTag);
    nameElement->addTextElement(String(mName));
    parquetDeformationElement->addChildElement(nameElement);

	// etc
}

void cParquetDeformationPlaygroundComponent::LoadParquetDeformationFile(const File& _parquetDeformationFile)
{
	if (_parquetDeformationFile.exists())
	{
		XmlDocument xmlDoc(_parquetDeformationFile);
		ScopedPointer<XmlElement> mainElement = xmlDoc.getDocumentElement();

		if (mainElement != nullptr)
		{
			// check the main tag
			if (mainElement->getTagName() == kParquetDeformationMainTag)
			{
				// verify version
				if (mainElement->hasAttribute(kParquetDeformationVersionTag) && mainElement->getDoubleAttribute(kParquetDeformationVersionTag) == kParquetDeformationVersion)
				{
					mParquetDeformationComponent.ImportXml(*mainElement);
					
					// etc
				}
			}
		}
	}			
}

void cParquetDeformation::ImportXml(const XmlElement& _parentElement)
{
    XmlElement* parquetDeformationElement = _parentElement.getChildByName(kParquetDeformationMainTag);
    if (parquetDeformationElement != nullptr)
    {
        Clear();

        XmlElement* nameElement = parquetDeformationElement->getChildByName(kParquetDeformationNameTag);
        if (nameElement != nullptr)
            mName = nameElement->getAllSubText();
			
		// etc
	}
}
1 Like

Also, just search the juce source tree for “XmlElement” or “createNewChildElement” and you’ll find hundreds of examples.

Hey guys,

thank you for your help, i see that i need to go deeper in the basics of c++, to really understand a) your code @cpr , and b) learn how to include the functions from the juce framwork correctly.

But thanks anyways.

Greetings,
Manuel

Hey Guys,

again and back to the Problem =) .
I think i have a main Problem with the file creation.

I insert in my Project just the Example from the JUCE XMLElement Class:
/* EXAMPLE STARTS HERE
// create an outer node called "ANIMALS"
XmlElement animalsList (“ANIMALS”);
for (int i = 0; i < numAnimals; ++i)
{
// create an inner element…
XmlElement* giraffe = new XmlElement (“GIRAFFE”);
giraffe->setAttribute (“name”, “nigel”);
giraffe->setAttribute (“age”, 10);
giraffe->setAttribute (“friendly”, true);
// …and add our new element to the parent node
animalsList.addChildElement (giraffe);
}
// now we can turn the whole thing into a text document…
String myXmlDoc = animalsList.createDocument (String());
*/ EXAMPLE ENDS HERE

As i understood, this example would write me an XMLDocument in which includes the Attributes from the Giraffe.

The Code compiles, but i cant find any XML File or Document on my Mac.
Then i thought, i need to write that XMLDocument in an File.

So i thought i need to add the writeToFile function at the end, cause it says it would write the Element to an File as an XML Document.
My Idea:
animalsList.writeToFile();

but what to insert in the brackets??? And where does the programm know, where to store the file? Do i need to define a directory path before i can write something?

Greetings,
Manuel

1 Like

Yeah, you should give that a File with a full path where you want the file written. You shouldn’t do something like myxml.writeToFile(File(“mydocument.xml”),String()); because the file can end up in some unexpected place. (Just a file name by itself does NOT mean the directory where your program executable is, it means the “working directory”.)

OH YES!
damn, i was so close :slight_smile:

works! thanks mate!