wxWidgets: wxXmlNode Class Reference

| Version: 3.0.5

Public Member Functions | List of all members
wxXmlNode Class Reference
Class List by Category » XML

`#include </home/Hamis/wxwidgets/wxWidgets3.0/wxWidgets3.0-3.0.5.1-2.i686/src/wxWidgets-3.0.5.1/interface/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

<>< =''>:</>&;&;< =''>\ </></>

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. More...
| 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. More...
| wxXmlNode (const wxXmlNode &node)
| Copy constructor. More...
virtual | ~wxXmlNode ()
| The virtual destructor. More...
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. More...
virtual void | AddAttribute (wxXmlAttribute *attr)
| Appends given attribute to the list of attributes for this node. More...
virtual void | AddChild (wxXmlNode *child)
| Adds node child as the last child of this node. More...
virtual bool | DeleteAttribute (const wxString &name)
| Removes the first attributes which has the given name from the list of attributes for this node. More...
bool | GetAttribute (const wxString &attrName, wxString *value) const
| Returns true if a attribute named attrName could be found. More...
wxString | GetAttribute (const wxString &attrName, const wxString &defaultVal=wxEmptyString) const
| Returns the value of the attribute named attrName if it does exist. More...
wxXmlAttribute * | GetAttributes () const
| Return a pointer to the first attribute of this node. More...
wxXmlNode * | GetChildren () const
| Returns the first child of this node. More...
const wxString & | GetContent () const
| Returns the content of this node. More...
int | GetDepth (wxXmlNode *grandparent=NULL) const
| Returns the number of nodes which separate this node from `grandparent`. More...
bool | GetNoConversion () const
| Returns a flag indicating whether encoding conversion is necessary when saving. More...
int | GetLineNumber () const
| Returns line number of the node in the input XML file or `-1` if it is unknown. More...
const wxString & | GetName () const
| Returns the name of this node. More...
wxXmlNode * | GetNext () const
| Returns a pointer to the sibling of this node or NULL if there are no siblings. More...
wxString | GetNodeContent () const
| Returns the content of the first child node of type `wxXML_TEXT_NODE` or `wxXML_CDATA_SECTION_NODE`. More...
wxXmlNode * | GetParent () const
| Returns a pointer to the parent of this node or NULL if this node has no parent. More...
wxXmlNodeType | GetType () const
| Returns the type of this node. More...
bool | HasAttribute (const wxString &attrName) const
| Returns true if this node has a attribute named attrName. More...
virtual bool | InsertChild (wxXmlNode *child, wxXmlNode *followingNode)
| Inserts the child node immediately before followingNode in the children list. More...
virtual bool | InsertChildAfter (wxXmlNode *child, wxXmlNode *precedingNode)
| Inserts the child node immediately after precedingNode in the children list. More...
bool | IsWhitespaceOnly () const
| Returns true if the content of this node is a string containing only whitespaces (spaces, tabs, new lines, etc). More...
virtual bool | RemoveChild (wxXmlNode *child)
| Removes the given node from the children list. More...
void | SetAttributes (wxXmlAttribute *attr)
| Sets as first attribute the given wxXmlAttribute object. More...
void | SetChildren (wxXmlNode *child)
| Sets as first child the given node. More...
void | SetContent (const wxString &con)
| Sets the content of this node. More...
void | SetName (const wxString &name)
| Sets the name of this node. More...
void | SetNext (wxXmlNode *next)
| Sets as sibling the given node. More...
void | SetNoConversion (bool noconversion)
| Sets a flag to indicate whether encoding conversion is necessary when saving. More...
void | SetParent (wxXmlNode *parent)
| Sets as parent the given node. More...
void | SetType (wxXmlNodeType type)
| Sets the type of this node. More...
wxXmlNode & | operator= (const wxXmlNode &node)
| See the copy constructor for more info. More...

## Constructor & Destructor Documentation

## ◆ wxXmlNode() [1/3]

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

  parent | The 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.
  type | One of the wxXmlNodeType enumeration value.
  name | The name of the node. This is the string which appears between angular brackets.
  content | The content of the node. Only meaningful when type is `wxXML_TEXT_NODE` or `wxXML_CDATA_SECTION_NODE`.
  attrs | If not NULL, this wxXmlAttribute object and its eventual siblings are attached to the node.
  next | If not NULL, this node and its eventual siblings are attached to the node.
  lineNo | Number of line this node was present at in input file or -1.

## ◆ wxXmlNode() [2/3]

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

  type | One of the wxXmlNodeType enumeration value.
  name | The name of the node. This is the string which appears between angular brackets.
  content | The content of the node. Only meaningful when type is `wxXML_TEXT_NODE` or `wxXML_CDATA_SECTION_NODE`.
  lineNo | Number of line this node was present at in input file or -1.

## ◆ wxXmlNode() [3/3]

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.

## ◆ ~wxXmlNode()

virtual wxXmlNode::~wxXmlNode | ( |  | ) |
| virtual

The virtual destructor.

Deletes attached children and attributes.

## Member Function Documentation

## ◆ AddAttribute() [1/2]

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.

## ◆ AddAttribute() [2/2]

virtual void wxXmlNode::AddAttribute | ( | wxXmlAttribute * | attr | ) |
| virtual

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

## ◆ AddChild()

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()

## ◆ DeleteAttribute()

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.

## ◆ GetAttribute() [1/2]

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.

## ◆ GetAttribute() [2/2]

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).

## ◆ GetAttributes()

wxXmlAttribute* wxXmlNode::GetAttributes | ( |  | ) | const

Return a pointer to the first attribute of this node.

## ◆ GetChildren()

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.

## ◆ GetContent()

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.

## ◆ GetDepth()

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).

## ◆ GetLineNumber()

int wxXmlNode::GetLineNumber | ( |  | ) | const

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

## ◆ GetName()

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`).

## ◆ GetNext()

wxXmlNode* wxXmlNode::GetNext | ( |  | ) | const

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

## ◆ GetNoConversion()

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.

## ◆ GetNodeContent()

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"
wxXML_TEXT_NODE
@ wxXML_TEXT_NODE
Definition: xml.h:15
wxXML_ELEMENT_NODE
@ wxXML_ELEMENT_NODE
Definition: xml.h:13

or eventually:

wxXML_ELEMENT_NODE name="tagname", content=""
|-- wxXML_CDATA_SECTION_NODE name="", content="tagcontent"
wxXML_CDATA_SECTION_NODE
@ wxXML_CDATA_SECTION_NODE
Definition: xml.h:16

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.

## ◆ GetParent()

wxXmlNode* wxXmlNode::GetParent | ( |  | ) | const

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

## ◆ GetType()

wxXmlNodeType wxXmlNode::GetType | ( |  | ) | const

Returns the type of this node.

## ◆ HasAttribute()

bool wxXmlNode::HasAttribute | ( | const wxString & | attrName | ) | const

Returns true if this node has a attribute named attrName.

## ◆ InsertChild()

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()

## ◆ 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

  child | The child to insert.
  precedingNode | The 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()

## ◆ IsWhitespaceOnly()

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.

## ◆ operator=()

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

See the copy constructor for more info.

## ◆ RemoveChild()

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.

## ◆ SetAttributes()

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.

## ◆ SetChildren()

void wxXmlNode::SetChildren | ( | wxXmlNode * | child | ) |

Sets as first child the given node.

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

## ◆ SetContent()

void wxXmlNode::SetContent | ( | const wxString & | con | ) |

Sets the content of this node.

## ◆ SetName()

void wxXmlNode::SetName | ( | const wxString & | name | ) |

Sets the name of this node.

## ◆ SetNext()

void wxXmlNode::SetNext | ( | wxXmlNode * | next | ) |

Sets as sibling the given node.

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

## ◆ SetNoConversion()

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.

## ◆ SetParent()

void wxXmlNode::SetParent | ( | wxXmlNode * | parent | ) |

Sets as parent the given node.

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

## ◆ SetType()

void wxXmlNode::SetType | ( | wxXmlNodeType | type | ) |

Sets the type of this node.