Version: 2.9.4
Public Member Functions | Static Public Member Functions
wxXmlDocument Class Reference

#include </home/zeitlin/src/wx/github/interface/wx/xml/xml.h>

Inheritance diagram for wxXmlDocument:

Detailed Description

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("myfile.xml"))
        return false;

    // start processing the XML file
    if (doc.GetRoot()->GetName() != "myroot-node")
        return false;

    // examine prologue
    wxXmlNode *prolog = doc.GetDocumentNode()->GetChildren();
    while (prolog) {

        if (prolog->GetType() == wxXML_PI_NODE && prolog->GetName() == "target") {

            // process Process Instruction contents
            wxString pi = prolog->GetContent();

            ...

        }
    }

    wxXmlNode *child = doc.GetRoot()->GetChildren();
    while (child) {

        if (child->GetName() == "tag1") {

            // process text enclosed by tag1/tag1
            wxString content = child->GetNodeContent();

            ...

            // process attributes of tag1
            wxString attrvalue1 =
                child->GetAttribute("attr1", "default-value");
            wxString attrvalue2 =
                child->GetAttribute("attr2", "default-value");

            ...

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

            // process tag2 ...
        }

        child = child->GetNext();
    }

Note that 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("myfile.xml", "UTF-8", wxXMLDOC_KEEP_WHITESPACE_NODES);

    // myfile2.xml will be identical to myfile.xml saving it this way:
    doc.Save("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("myfile.xml");
    doc.Save("myfile2.xml");  // myfile2.xml != myfile.xml

Library:  wxXML
Category:  XML
参照:
wxXmlNode, wxXmlAttribute

Public Member Functions

 wxXmlDocument ()
 Default constructor.
 wxXmlDocument (const wxXmlDocument &doc)
 Copy constructor.
 wxXmlDocument (const wxString &filename, const wxString &encoding="UTF-8"))
 Loads the given filename using the given encoding.
 wxXmlDocument (wxInputStream &stream, const wxString &encoding="UTF-8")
 Loads the XML document from given stream using the given encoding.
virtual ~wxXmlDocument ()
 Virtual destructor.
void AppendToProlog (wxXmlNode *node)
 Appends a Process Instruction or Comment node to the document prologue.
wxXmlNodeDetachDocumentNode ()
 Detaches the document node and returns it.
wxXmlNodeDetachRoot ()
 Detaches the root entity node and returns it.
wxString GetEncoding () const
 Returns encoding of in-memory representation of the document (same as passed to Load() or constructor, defaults to UTF-8).
const wxStringGetFileEncoding () const
 Returns encoding of document (may be empty).
wxXmlNodeGetDocumentNode () const
 Returns the document node of the document.
wxXmlNodeGetRoot () const
 Returns the root element node of the document.
const wxStringGetVersion () const
 Returns the version of document.
bool IsOk () const
 Returns true if the document has been loaded successfully.
virtual bool Load (const wxString &filename, const wxString &encoding="UTF-8", int flags=wxXMLDOC_NONE)
 Parses filename as an xml document and loads its data.
virtual bool Load (wxInputStream &stream, const wxString &encoding="UTF-8", int flags=wxXMLDOC_NONE)
 Like Load(const wxString&, const wxString&, int) but takes the data from given input stream.
virtual bool Save (const wxString &filename, int indentstep=2) const
 Saves XML tree creating a file named with given string.
virtual bool Save (wxOutputStream &stream, int indentstep=2) const
 Saves XML tree in the given output stream.
void SetDocumentNode (wxXmlNode *node)
 Sets the document node of this document.
void SetEncoding (const wxString &enc)
 Sets the encoding of the document.
void SetFileEncoding (const wxString &encoding)
 Sets the enconding of the file which will be used to save the document.
void SetRoot (wxXmlNode *node)
 Sets the root element node of this document.
void SetVersion (const wxString &version)
 Sets the version of the XML file which will be used to save the document.
wxXmlDocumentoperator= (const wxXmlDocument &doc)
 Deep copies the given document.

Static Public Member Functions

static wxVersionInfo GetLibraryVersionInfo ()
 Get expat library version information.

List of all members.


Constructor & Destructor Documentation

wxXmlDocument::wxXmlDocument ( )

Default constructor.

wxXmlDocument::wxXmlDocument ( const wxXmlDocument doc)

Copy constructor.

Deep copies all the XML tree of the given document.

wxXmlDocument::wxXmlDocument ( const wxString filename,
const wxString encoding = "UTF-8" 
)

Loads the given filename using the given encoding.

See Load().

wxXmlDocument::wxXmlDocument ( wxInputStream stream,
const wxString encoding = "UTF-8" 
)

Loads the XML document from given stream using the given encoding.

See Load().

virtual wxXmlDocument::~wxXmlDocument ( ) [virtual]

Virtual destructor.

Frees the document root node.


Member Function Documentation

void wxXmlDocument::AppendToProlog ( wxXmlNode node)

Appends a Process Instruction or Comment node to the document prologue.

Calling this function will create a prologue or attach the node to the end of an existing prologue.

Since:
2.9.2
wxXmlNode* wxXmlDocument::DetachDocumentNode ( )

Detaches the document node and returns it.

The document node will be set to NULL and thus IsOk() will return false after calling this function.

Note that the caller is responsible for deleting the returned node in order to avoid memory leaks.

Since:
2.9.2
wxXmlNode* wxXmlDocument::DetachRoot ( )

Detaches the root entity node and returns it.

After calling this function, the document node will remain together with any prologue nodes, but IsOk() will return false since the root entity will be missing.

Note that the caller is responsible for deleting the returned node in order to avoid memory leaks.

wxXmlNode* wxXmlDocument::GetDocumentNode ( ) const

Returns the document node of the document.

Since:
2.9.2
wxString wxXmlDocument::GetEncoding ( ) const

Returns encoding of in-memory representation of the document (same as passed to Load() or constructor, defaults to UTF-8).

注:
this is meaningless in Unicode build where data are stored as wchar_t*.
const wxString& wxXmlDocument::GetFileEncoding ( ) const

Returns encoding of document (may be empty).

注:
This is the encoding original file was saved in, not the encoding of in-memory representation!
static wxVersionInfo wxXmlDocument::GetLibraryVersionInfo ( ) [static]

Get expat library version information.

Since:
2.9.2
参照:
wxVersionInfo
wxXmlNode* wxXmlDocument::GetRoot ( ) const

Returns the root element node of the document.

const wxString& wxXmlDocument::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 attribute was not explicitly given in the header, this function returns an empty string.

bool wxXmlDocument::IsOk ( ) const

Returns true if the document has been loaded successfully.

virtual bool wxXmlDocument::Load ( const wxString filename,
const wxString encoding = "UTF-8",
int  flags = wxXMLDOC_NONE 
) [virtual]

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.

virtual bool wxXmlDocument::Load ( wxInputStream stream,
const wxString encoding = "UTF-8",
int  flags = wxXMLDOC_NONE 
) [virtual]

Like Load(const wxString&, const wxString&, int) but takes the data from given input stream.

wxXmlDocument& wxXmlDocument::operator= ( const wxXmlDocument doc)

Deep copies the given document.

virtual bool wxXmlDocument::Save ( wxOutputStream stream,
int  indentstep = 2 
) const [virtual]

Saves XML tree in the given output stream.

See Save(const wxString&, int) for a description of indentstep.

virtual bool wxXmlDocument::Save ( const wxString filename,
int  indentstep = 2 
) const [virtual]

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.

void wxXmlDocument::SetDocumentNode ( wxXmlNode node)

Sets the document node of this document.

Deletes any previous document node. Use DetachDocumentNode() and then SetDocumentNode() if you want to replace the document node without deleting the old document tree.

Since:
2.9.2
void wxXmlDocument::SetEncoding ( const wxString enc)

Sets the encoding of the document.

void wxXmlDocument::SetFileEncoding ( const wxString encoding)

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

void wxXmlDocument::SetRoot ( wxXmlNode node)

Sets the root element node of this document.

Will create the document node if necessary. Any previous root element node is deleted.

void wxXmlDocument::SetVersion ( const wxString version)

Sets the version of the XML file which will be used to save the document.

 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines