contents up previous next

wxxmldocument

this class holds xml data/document as parsed by xml parser in the root node.

wxxmldocument internally uses the expat library which comes with wxwidgets to parse the given stream.

a simple example of using xml classes is:

wxxmldocument doc;
if (!doc.load(wxt("myfile.xml")))
    return false;

// start processing the xml file
if (doc.getroot()->getname() != wxt("myroot-node"))
    return false;

wxxmlnode *child = doc.getroot()->getchildren();
while (child) {

    if (child->getname() == wxt("tag1")) {

        // process text enclosed by <tag1></tag1>
        wxstring content = child->getnodecontent();

        ...


        // process properties of <tag1>
        wxstring propvalue1 = 
            child->getpropval(wxt("prop1"), 
                              wxt("default-value"));
        wxstring propvalue2 = 
            child->getpropval(wxt("prop2"), 
                              wxt("default-value"));

        ...

    } else if (child->getname() == wxt("tag2")) {

        // process tag2 ...
    }

    child = child->getnext();
}
note: if you want to preserve the original formatting of the loaded file including whitespaces and indentation, you need to turn off whitespace-only textnode removal and automatic indentation:

wxxmldocument doc;
doc.load(wxt("myfile.xml"), wxt("utf-8"), wxxmldoc_keep_whitespace_nodes);

// myfile2.xml will be indentic to myfile.xml saving it this way:
doc.save(wxt("myfile2.xml"), wxxml_no_indentation);
using default parameters, you will get a reformatted document which in general is different from the original loaded content:

wxxmldocument doc;
doc.load(wxt("myfile.xml"));
doc.save(wxt("myfile2.xml"));  // myfile2.xml != myfile.xml
derived from

wxobject

include files

<wx/xml/xml.h>

see also

wxxmlnode, wxxmlproperty

members

wxxmldocument::wxxmldocument
wxxmldocument::~wxxmldocument
wxxmldocument::detachroot
wxxmldocument::getencoding
wxxmldocument::getfileencoding
wxxmldocument::getroot
wxxmldocument::getversion
wxxmldocument::isok
wxxmldocument::load
wxxmldocument::save
wxxmldocument::setencoding
wxxmldocument::setfileencoding
wxxmldocument::setroot
wxxmldocument::setversion
wxxmldocument::operator=


wxxmldocument::wxxmldocument

wxxmldocument()

wxxmldocument(const wxstring& filename, const wxstring& encoding = wxt("utf-8"), int flags = wxxmldoc_none)

loads the given filename using the given encoding. see load.

wxxmldocument(wxinputstream& stream, const wxstring& encoding = wxt("utf-8"), int flags = wxxmldoc_none)

loads the xml document from given stream using the given encoding. see load.

wxxmldocument(const wxxmldocument& doc)

copy constructor. deep copies all the xml tree of the given document.


wxxmldocument::~wxxmldocument

~wxxmldocument()

virtual destructor. frees the document root node.


wxxmldocument::detachroot

wxxmlnode* detachroot()

detaches the document root node and returns it. the document root node will be set to null and thus isok will return false after calling this function.

note that the caller is reponsible for deleting the returned node in order to avoid memory leaks.


wxxmldocument::getencoding

wxstring getencoding() const

returns encoding of in-memory representation of the document (same as passed to load or constructor, defaults to utf-8).

nb: this is meaningless in unicode build where data are stored as wchar_t*.


wxxmldocument::getfileencoding

wxstring getfileencoding() const

returns encoding of document (may be empty).

note: this is the encoding original file was saved in, not the encoding of in-memory representation!


wxxmldocument::getroot

wxxmlnode* getroot() const

returns the root node of the document.


wxxmldocument::getversion

wxstring getversion() const

returns the version of document. this is the value in the <?xml version="1.0"?> header of the xml document. if the version property was not explicitely given in the header, this function returns an empty string.


wxxmldocument::isok

bool isok() const

returns true if the document has been loaded successfully.


wxxmldocument::load

bool load(const wxstring& filename, const wxstring& encoding = wxt("utf-8"), int flags = wxxmldoc_none)

parses filename as an xml document and loads its data.

if flags does not contain wxxmldoc_keep_whitespace_nodes, then, while loading, all nodes of type wxxml_text_node (see wxxmlnode) are automatically skipped if they contain whitespaces only. the removal of these nodes makes the load process slightly faster and requires less memory however makes impossible to recreate exactly the loaded text with a save call later. read the initial description of this class for more info.

returns true on success, false otherwise.

bool load(wxinputstream& stream, const wxstring& encoding = wxt("utf-8"), int flags = wxxmldoc_none)

like above but takes the data from given input stream.


wxxmldocument::save

bool save(const wxstring& filename, int indentstep = 1) const

saves xml tree creating a file named with given string.

if indentstep is greater than or equal to zero, then, while saving, an automatic indentation is added with steps composed by indentstep spaces. if indentstep is wxxml_no_indentation, then, automatic indentation is turned off.

bool save(wxoutputstream& stream, int indentstep = 1) const

saves xml tree in the given output stream. see other overload for a description of indentstep.


wxxmldocument::setencoding

void setencoding(const wxstring& enc)

sets the enconding of the document.


wxxmldocument::setfileencoding

void setfileencoding(const wxstring& encoding)

sets the enconding of the file which will be used to save the document.


wxxmldocument::setroot

void setroot(wxxmlnode* node)

sets the root node of this document. deletes previous root node. use detachroot and then setroot if you want to replace the root node without deleting the old document tree.


wxxmldocument::setversion

void setversion(const wxstring& version)

sets the version of the xml file which will be used to save the document.


wxxmldocument::operator=

wxxmldocument& operator operator=(const wxxmldocument& doc)

deep copies the given document.