Phoenix Logo

phoenix_title wx.xml.XmlNode

Represents a node in an XML document.

See wx.xml.XmlDocument.

Node has a name and may have content and attributes.

Most common node types are XML_TEXT_NODE (name and attributes are irrelevant) and XML_ELEMENT_NODE .

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

The XML_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 USE_UNICODE is 0, all strings are encoded in the encoding given to wx.xml.XmlDocument.Load (default is UTF-8).


class_hierarchy Class Hierarchy

Inheritance diagram for class XmlNode:

method_summary Methods Summary

__init__ Creates this XML node and eventually insert it into an existing XML tree.
AddAttribute Appends a attribute with given name and value to the list of attributes for this node.
AddChild Adds node child as the last child of this node.
DeleteAttribute Removes the first attributes which has the given name from the list of attributes for this node.
GetAttribute Returns the value of the attribute named attrName if it does exist.
GetAttributes Return a pointer to the first attribute of this node.
GetChildren Returns the first child of this node.
GetContent Returns the content of this node.
GetDepth Returns the number of nodes which separate this node from grandparent .
GetLineNumber Returns line number of the node in the input XML file or -1 if it is unknown.
GetName Returns the name of this node.
GetNext Returns a pointer to the sibling of this node or None if there are no siblings.
GetNoConversion Returns a flag indicating whether encoding conversion is necessary when saving.
GetNodeContent Returns the content of the first child node of type XML_TEXT_NODE or XML_CDATA_SECTION_NODE .
GetParent Returns a pointer to the parent of this node or None if this node has no parent.
GetType Returns the type of this node.
HasAttribute Returns True if this node has a attribute named attrName.
InsertChild Inserts the child node immediately before followingNode in the children list.
InsertChildAfter Inserts the child node immediately after precedingNode in the children list.
IsWhitespaceOnly Returns True if the content of this node is a string containing only whitespaces (spaces, tabs, new lines, etc).
RemoveChild Removes the given node from the children list.
SetContent Sets the content of this node.
SetName Sets the name of this node.
SetNext Sets as sibling the given node.
SetNoConversion Sets a flag to indicate whether encoding conversion is necessary when saving.
SetParent Sets as parent the given node.
SetType Sets the type of this node.

api Class API



class wx.xml.XmlNode(object)

Possible constructors:

XmlNode(parent, type, name, content="", attrs=None, next=None,
        lineNo=-1)

XmlNode(type, name, content="", lineNo=-1)

XmlNode(node)

Represents a node in an XML document.


Methods



__init__(self, *args, **kw)

overload Overloaded Implementations:



__init__ (self, parent, type, name, content=””, attrs=None, next=None, lineNo=-1)

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

Parameters:
  • parent (wx.xml.XmlNode) – The parent node to which append this node instance. If this argument is None 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 (XmlNodeType) – One of the wx.xml.XmlNodeType enumeration value.
  • name (string) – The name of the node. This is the string which appears between angular brackets.
  • content (string) – The content of the node. Only meaningful when type is XML_TEXT_NODE or XML_CDATA_SECTION_NODE .
  • attrs (wx.xml.XmlAttribute) – If not None, this wx.xml.XmlAttribute object and its eventual siblings are attached to the node.
  • next (wx.xml.XmlNode) – If not None, this node and its eventual siblings are attached to the node.
  • lineNo (int) – Number of line this node was present at in input file or -1.



__init__ (self, type, name, content=””, lineNo=-1)

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

Parameters:
  • type (XmlNodeType) – One of the wx.xml.XmlNodeType enumeration value.
  • name (string) – The name of the node. This is the string which appears between angular brackets.
  • content (string) – The content of the node. Only meaningful when type is XML_TEXT_NODE or XML_CDATA_SECTION_NODE .
  • lineNo (int) – Number of line this node was present at in input file or -1.



__init__ (self, node)

Copy constructor.

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

Parameters:node (wx.xml.XmlNode) –





AddAttribute(self, *args, **kw)

overload Overloaded Implementations:



AddAttribute (self, name, value)

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

Parameters:
  • name (string) –
  • value (string) –



AddAttribute (self, attr)

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

Parameters:attr (wx.xml.XmlAttribute) –





AddChild(self, child)

Adds node child as the last child of this node.

Parameters:child (wx.xml.XmlNode) –

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.



DeleteAttribute(self, name)

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

Parameters:name (string) –
Return type:bool


GetAttribute(self, attrName, defaultVal="")

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

If it does not exist, the defaultVal is returned.

Parameters:
  • attrName (string) –
  • defaultVal (string) –
Return type:

string



GetAttributes(self)

Return a pointer to the first attribute of this node.

Return type: wx.xml.XmlAttribute


GetChildren(self)

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.

Return type: wx.xml.XmlNode


GetContent(self)

Returns the content of this node.

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

Return type:string


GetDepth(self, grandparent=None)

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 None node (which is the parent of non-linked nodes or the parent of a wx.xml.XmlDocument‘s root element node).

Parameters:grandparent (wx.xml.XmlNode) –
Return type:int


GetLineNumber(self)

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

Return type:int


GetName(self)

Returns the name of this node.

Can be an empty string (e.g. for nodes of type XML_TEXT_NODE or XML_CDATA_SECTION_NODE ).

Return type:string


GetNext(self)

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

Return type: wx.xml.XmlNode


GetNoConversion(self)

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.

Return type:bool


GetNodeContent(self)

Returns the content of the first child node of type XML_TEXT_NODE or XML_CDATA_SECTION_NODE .

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

XML_ELEMENT_NODE name="tagname", content=""
|-- XML_TEXT_NODE name="", content="tagcontent"

or eventually:

XML_ELEMENT_NODE name="tagname", content=""
|-- XML_CDATA_SECTION_NODE name="", content="tagcontent"

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

Return type:string


GetParent(self)

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

Return type: wx.xml.XmlNode


GetType(self)

Returns the type of this node.

Return type: wx.xml.XmlNodeType


HasAttribute(self, attrName)

Returns True if this node has a attribute named attrName.

Parameters:attrName (string) –
Return type:bool


InsertChild(self, child, followingNode)

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

Parameters:
Return type:

bool

Returns:

True if followingNode has been found and the child node has been inserted.

Note

For historical reasons, followingNode may be None. 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).



InsertChildAfter(self, child, precedingNode)

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

Parameters:
  • child (wx.xml.XmlNode) – The child to insert.
  • precedingNode (wx.xml.XmlNode) – The node to insert child after. As a special case, this can be None if this node has no children yet child will become this node’s only child node.
Return type:

bool

Returns:

True if precedingNode has been found and the child node has been inserted.

New in version 2.8.8.

See also

InsertChild , AddChild



IsWhitespaceOnly(self)

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.

Return type:bool


RemoveChild(self, child)

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.

Parameters:child (wx.xml.XmlNode) –
Return type:bool


SetContent(self, con)

Sets the content of this node.

Parameters:con (string) –


SetName(self, name)

Sets the name of this node.

Parameters:name (string) –


SetNext(self, next)

Sets as sibling the given node.

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

Parameters:next (wx.xml.XmlNode) –


SetNoConversion(self, 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.

Parameters:noconversion (bool) –


SetParent(self, parent)

Sets as parent the given node.

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

Parameters:parent (wx.xml.XmlNode) –


SetType(self, type)

Sets the type of this node.

Parameters:type (XmlNodeType) –

Properties



Attributes

See GetAttributes



Children

See GetChildren



Content

See GetContent and SetContent



Depth

See GetDepth



LineNumber

See GetLineNumber



Name

See GetName and SetName



Next

See GetNext and SetNext



NoConversion

See GetNoConversion and SetNoConversion



NodeContent

See GetNodeContent



Parent

See GetParent and SetParent



Type

See GetType and SetType