Version: 3.0.2
wxXmlNode Class Reference

#include <wx/xml/xml.h>

Detailed Description

Represents a node in an XML document.

See wxXmlDocument.

Node has a name and may have content and attributes.

Most common node types are wxXML_TEXT_NODE (name and attributes are irrelevant) and wxXML_ELEMENT_NODE.

Example: in <title>hi</title> there is an element with the name title and irrelevant content and one child of type wxXML_TEXT_NODE with hi as content.

The wxXML_PI_NODE type sets the name to the PI target and the contents to the instructions. Note that whilst the PI instructions are often in the form of pseudo-attributes these do not use the nodes attribute system. It is the users responsibility to code and decode the instruction text.

If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to wxXmlDocument::Load (default is UTF-8).

Library:  wxXML
Category:  XML
See Also
wxXmlDocument, wxXmlAttribute

Public Member Functions

 wxXmlNode (wxXmlNode *parent, wxXmlNodeType type, const wxString &name, const wxString &content=wxEmptyString, wxXmlAttribute *attrs=NULL, wxXmlNode *next=NULL, int lineNo=-1)
 Creates this XML node and eventually insert it into an existing XML tree.
 
 wxXmlNode (wxXmlNodeType type, const wxString &name, const wxString &content=wxEmptyString, int lineNo=-1)
 A simplified version of the first constructor form, assuming a NULL parent.
 
 wxXmlNode (const wxXmlNode &node)
 Copy constructor.
 
virtual ~wxXmlNode ()
 The virtual destructor.
 
virtual void AddAttribute (const wxString &name, const wxString &value)
 Appends a attribute with given name and value to the list of attributes for this node.
 
virtual void AddAttribute (wxXmlAttribute *attr)
 Appends given attribute to the list of attributes for this node.
 
virtual void AddChild (wxXmlNode *child)
 Adds node child as the last child of this node.
 
virtual bool DeleteAttribute (const wxString &name)
 Removes the first attributes which has the given name from the list of attributes for this node.
 
bool GetAttribute (const wxString &attrName, wxString *value) const
 Returns true if a attribute named attrName could be found.
 
wxString GetAttribute (const wxString &attrName, const wxString &defaultVal=wxEmptyString) const
 Returns the value of the attribute named attrName if it does exist.
 
wxXmlAttributeGetAttributes () const
 Return a pointer to the first attribute of this node.
 
wxXmlNodeGetChildren () const
 Returns the first child of this node.
 
const wxStringGetContent () const
 Returns the content of this node.
 
int GetDepth (wxXmlNode *grandparent=NULL) const
 Returns the number of nodes which separate this node from grandparent.
 
bool GetNoConversion () const
 Returns a flag indicating whether encoding conversion is necessary when saving.
 
int GetLineNumber () const
 Returns line number of the node in the input XML file or -1 if it is unknown.
 
const wxStringGetName () const
 Returns the name of this node.
 
wxXmlNodeGetNext () const
 Returns a pointer to the sibling of this node or NULL if there are no siblings.
 
wxString GetNodeContent () const
 Returns the content of the first child node of type wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE.
 
wxXmlNodeGetParent () const
 Returns a pointer to the parent of this node or NULL if this node has no parent.
 
wxXmlNodeType GetType () const
 Returns the type of this node.
 
bool HasAttribute (const wxString &attrName) const
 Returns true if this node has a attribute named attrName.
 
virtual bool InsertChild (wxXmlNode *child, wxXmlNode *followingNode)
 Inserts the child node immediately before followingNode in the children list.
 
virtual bool InsertChildAfter (wxXmlNode *child, wxXmlNode *precedingNode)
 Inserts the child node immediately after precedingNode in the children list.
 
bool IsWhitespaceOnly () const
 Returns true if the content of this node is a string containing only whitespaces (spaces, tabs, new lines, etc).
 
virtual bool RemoveChild (wxXmlNode *child)
 Removes the given node from the children list.
 
void SetAttributes (wxXmlAttribute *attr)
 Sets as first attribute the given wxXmlAttribute object.
 
void SetChildren (wxXmlNode *child)
 Sets as first child the given node.
 
void SetContent (const wxString &con)
 Sets the content of this node.
 
void SetName (const wxString &name)
 Sets the name of this node.
 
void SetNext (wxXmlNode *next)
 Sets as sibling the given node.
 
void SetNoConversion (bool noconversion)
 Sets a flag to indicate whether encoding conversion is necessary when saving.
 
void SetParent (wxXmlNode *parent)
 Sets as parent the given node.
 
void SetType (wxXmlNodeType type)
 Sets the type of this node.
 
wxXmlNodeoperator= (const wxXmlNode &node)
 See the copy constructor for more info.
 

Constructor & Destructor Documentation

wxXmlNode::wxXmlNode ( wxXmlNode parent,
wxXmlNodeType  type,
const wxString name,
const wxString content = wxEmptyString,
wxXmlAttribute attrs = NULL,
wxXmlNode next = NULL,
int  lineNo = -1 
)

Creates this XML node and eventually insert it into an existing XML tree.

Parameters
parentThe parent node to which append this node instance. If this argument is NULL this new node will be floating and it can be appended later to another one using the AddChild() or InsertChild() functions. Otherwise the child is already added to the XML tree by this constructor and it shouldn't be done again.
typeOne of the wxXmlNodeType enumeration value.
nameThe name of the node. This is the string which appears between angular brackets.
contentThe content of the node. Only meaningful when type is wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE.
attrsIf not NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
nextIf not NULL, this node and its eventual siblings are attached to the node.
lineNoNumber of line this node was present at in input file or -1.
wxXmlNode::wxXmlNode ( wxXmlNodeType  type,
const wxString name,
const wxString content = wxEmptyString,
int  lineNo = -1 
)

A simplified version of the first constructor form, assuming a NULL parent.

Parameters
typeOne of the wxXmlNodeType enumeration value.
nameThe name of the node. This is the string which appears between angular brackets.
contentThe content of the node. Only meaningful when type is wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE.
lineNoNumber of line this node was present at in input file or -1.
wxXmlNode::wxXmlNode ( const wxXmlNode node)

Copy constructor.

Note that this does NOT copy siblings and parent pointer, i.e. GetParent() and GetNext() will return NULL after using copy ctor and are never unmodified by operator=(). On the other hand, it DOES copy children and attributes.

virtual wxXmlNode::~wxXmlNode ( )
virtual

The virtual destructor.

Deletes attached children and attributes.

Member Function Documentation

virtual void wxXmlNode::AddAttribute ( const wxString name,
const wxString value 
)
virtual

Appends a attribute with given name and value to the list of attributes for this node.

virtual void wxXmlNode::AddAttribute ( wxXmlAttribute attr)
virtual

Appends given attribute to the list of attributes for this node.

virtual void wxXmlNode::AddChild ( wxXmlNode child)
virtual

Adds node child as the last child of this node.

Note
Note that this function works in O(n) time where n is the number of existing children. Consequently, adding large number of child nodes using this method can be expensive, because it has O(n^2) time complexity in number of nodes to be added. Use InsertChildAfter() to populate XML tree in linear time.
See Also
InsertChild(), InsertChildAfter()
virtual bool wxXmlNode::DeleteAttribute ( const wxString name)
virtual

Removes the first attributes which has the given name from the list of attributes for this node.

bool wxXmlNode::GetAttribute ( const wxString attrName,
wxString value 
) const

Returns true if a attribute named attrName could be found.

The value of that attribute is saved in value (which must not be NULL).

wxString wxXmlNode::GetAttribute ( const wxString attrName,
const wxString defaultVal = wxEmptyString 
) const

Returns the value of the attribute named attrName if it does exist.

If it does not exist, the defaultVal is returned.

wxXmlAttribute* wxXmlNode::GetAttributes ( ) const

Return a pointer to the first attribute of this node.

wxXmlNode* wxXmlNode::GetChildren ( ) const

Returns the first child of this node.

To get a pointer to the second child of this node (if it does exist), use the GetNext() function on the returned value.

const wxString& wxXmlNode::GetContent ( ) const

Returns the content of this node.

Can be an empty string. Be aware that for nodes of type wxXML_ELEMENT_NODE (the most used node type) the content is an empty string. See GetNodeContent() for more details.

int wxXmlNode::GetDepth ( wxXmlNode grandparent = NULL) const

Returns the number of nodes which separate this node from grandparent.

This function searches only the parents of this node until it finds grandparent or the NULL node (which is the parent of non-linked nodes or the parent of a wxXmlDocument's root element node).

int wxXmlNode::GetLineNumber ( ) const

Returns line number of the node in the input XML file or -1 if it is unknown.

const wxString& wxXmlNode::GetName ( ) const

Returns the name of this node.

Can be an empty string (e.g. for nodes of type wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE).

wxXmlNode* wxXmlNode::GetNext ( ) const

Returns a pointer to the sibling of this node or NULL if there are no siblings.

bool wxXmlNode::GetNoConversion ( ) const

Returns a flag indicating whether encoding conversion is necessary when saving.

The default is false.

You can improve saving efficiency considerably by setting this value.

wxString wxXmlNode::GetNodeContent ( ) const

Returns the content of the first child node of type wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE.

This function is very useful since the XML snippet "tagnametagcontent/tagname" is represented by expat with the following tag tree:

wxXML_ELEMENT_NODE name="tagname", content=""
|-- wxXML_TEXT_NODE name="", content="tagcontent"

or eventually:

wxXML_ELEMENT_NODE name="tagname", content=""
|-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"

An empty string is returned if the node has no children of type wxXML_TEXT_NODE or wxXML_CDATA_SECTION_NODE, or if the content of the first child of such types is empty.

wxXmlNode* wxXmlNode::GetParent ( ) const

Returns a pointer to the parent of this node or NULL if this node has no parent.

wxXmlNodeType wxXmlNode::GetType ( ) const

Returns the type of this node.

bool wxXmlNode::HasAttribute ( const wxString attrName) const

Returns true if this node has a attribute named attrName.

virtual bool wxXmlNode::InsertChild ( wxXmlNode child,
wxXmlNode followingNode 
)
virtual

Inserts the child node immediately before followingNode in the children list.

Returns
true if followingNode has been found and the child node has been inserted.
Note
For historical reasons, followingNode may be NULL. In that case, then child is prepended to the list of children and becomes the first child of this node, i.e. it behaves identically to using the first children (as returned by GetChildren()) for followingNode).
See Also
AddChild(), InsertChildAfter()
virtual bool wxXmlNode::InsertChildAfter ( wxXmlNode child,
wxXmlNode precedingNode 
)
virtual

Inserts the child node immediately after precedingNode in the children list.

Returns
true if precedingNode has been found and the child node has been inserted.
Parameters
childThe child to insert.
precedingNodeThe node to insert child after. As a special case, this can be NULL if this node has no children yet – in that case, child will become this node's only child node.
Since
2.8.8
See Also
InsertChild(), AddChild()
bool wxXmlNode::IsWhitespaceOnly ( ) const

Returns true if the content of this node is a string containing only whitespaces (spaces, tabs, new lines, etc).

Note that this function is locale-independent since the parsing of XML documents must always produce the exact same tree regardless of the locale it runs under.

wxXmlNode& wxXmlNode::operator= ( const wxXmlNode node)

See the copy constructor for more info.

virtual bool wxXmlNode::RemoveChild ( wxXmlNode child)
virtual

Removes the given node from the children list.

Returns true if the node was found and removed or false if the node could not be found. Note that the caller is responsible for deleting the removed node in order to avoid memory leaks.

void wxXmlNode::SetAttributes ( wxXmlAttribute attr)

Sets as first attribute the given wxXmlAttribute object.

The caller is responsible for deleting any previously present attributes attached to this node.

void wxXmlNode::SetChildren ( wxXmlNode child)

Sets as first child the given node.

The caller is responsible for deleting any previously present children node.

void wxXmlNode::SetContent ( const wxString con)

Sets the content of this node.

void wxXmlNode::SetName ( const wxString name)

Sets the name of this node.

void wxXmlNode::SetNext ( wxXmlNode next)

Sets as sibling the given node.

The caller is responsible for deleting any previously present sibling node.

void wxXmlNode::SetNoConversion ( bool  noconversion)

Sets a flag to indicate whether encoding conversion is necessary when saving.

The default is false.

You can improve saving efficiency considerably by setting this value.

void wxXmlNode::SetParent ( wxXmlNode parent)

Sets as parent the given node.

The caller is responsible for deleting any previously present parent node.

void wxXmlNode::SetType ( wxXmlNodeType  type)

Sets the type of this node.