Juce
Table of Contents

Reading Xml Files

Example xml file:

<CONFIG>
    <NAME>Chrys</NAME>
    <AGE>30</AGE>
</CONFIG>

File XmlDoc.h

class XmlDoc
{
    public:
        XmlDoc(const String &document);
        XmlDoc::XmlDoc(const File &file);
        void parseXml();
        ~XmlDoc(void);
 
    private:
        XmlDocument mainDocument;
        XmlElement *mainElement;
        String age;
        String name;
 
};

File XmlDoc.cpp

#include "XmlDoc.h"
 
XmlDoc::XmlDoc(const String &document) : mainDocument(document)
{
}
 
XmlDoc::XmlDoc(const File &file) : mainDocument(file)
{
}
 
XmlDoc::~XmlDoc(void)
{
}
 
void XmlDoc::parseXml()
{
 
    // parse a text-based XML document
    XmlElement *mainElement = mainDocument.getDocumentElement(); 
    //check for validity against the DTD
    if (mainElement == 0)
    {
        ALERT(mainDocument.getLastParseError());
    }
    else 
    {
        if( mainElement->hasTagName( T("CONFIG") ) )
        {
            forEachXmlChildElement(*mainElement, child)
            {
 
                if( child->hasTagName( T("NAME") ) )
                {
                    name = child->getAllSubText();
 
                }
                if( child->hasTagName(T("AGE") ) )
                {
                    age = child->getAllSubText();
                }
            }
        }
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.