wx.xml.XmlNode¶
Represents a node in an XML
document.
See wx.xml.XmlDocument.
Each node is named and depending on the node type it may also hold content or be given attributes.
The two most common node types are XML_ELEMENT_NODE
and XML_TEXT_NODE
. XML_ELEMENT_NODE
represents a pair of XML
element tags, whilst XML_TEXT_NODE
represents the text value that can belong to the element.
A XML_ELEMENT_NODE
has a title, and optionally attributes, but does not have any content. A XML_TEXT_NODE
does not have a title or attributes but should normally have content.
For example: in the XML
fragment <title>hi</title>
there is an element node with the name title
and a single text node child with the text hi
as content.
A XML_PI_NODE
represents a Processing Instruction (PI
) node with the name parameter set as the target and the contents parameter set as the instructions. Note that whilst the PI
instructions are often in the form of pseudo-attributes, these do not use the node’s attribute member. It is the user’s responsibility to code and decode the PI
instruction text.
The XML_DOCUMENT_TYPE_NODE
is not implemented at this time. Instead, you should get and set the DOCTYPE
values using the wx.xml.XmlDocument class.
If USE_UNICODE
is 0, all strings are encoded in the encoding given to wx.xml.XmlDocument.Load
(default is UTF-8
).
Note
Once a wx.xml.XmlNode has been added to a wx.xml.XmlDocument it becomes owned by the document and this has two implications. Firstly, the wx.xml.XmlDocument takes responsibility for deleting the node so the user should not delete
it; and secondly, a wx.xml.XmlNode must always be created on the heap and never on the stack.
Class Hierarchy¶
Methods Summary¶
Creates this |
|
Appends an attribute with given name and value to the list of attributes for this node. |
|
Adds node child as the last child of this node. |
|
Removes the first attributes which has the given name from the list of attributes for this node. |
|
Returns the value of the attribute named attrName if it does exist. |
|
Return a pointer to the first attribute of this node. |
|
Returns the first child of this node. |
|
Returns the content of this node. |
|
Returns the number of nodes which separate this node from |
|
Returns line number of the node in the input |
|
Returns the name of this node. |
|
Returns a pointer to the sibling of this node or |
|
Returns a flag indicating whether encoding conversion is necessary when saving. |
|
Returns the content of the first child node of type |
|
Returns a pointer to the parent of this node or |
|
Returns the type of this node. |
|
Returns |
|
Inserts the child node immediately before followingNode in the children list. |
|
Inserts the child node immediately after precedingNode in the children list. |
|
Returns |
|
Removes the given node from the children list. |
|
Sets the content of this node. |
|
Sets the name of this node. |
|
Sets as sibling the given node. |
|
Sets a flag to indicate whether encoding conversion is necessary when saving. |
|
Sets as parent the given node. |
|
Sets the type of this node. |
Properties Summary¶
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
See |
|
Class API¶
- class wx.xml.XmlNode(object)¶
Possible constructors:
XmlNode(parent, type, name, content='', attrs=None, next=None, lineNo=-1) -> None XmlNode(type, name, content='', lineNo=-1) -> None XmlNode(node) -> None
Represents a node in an
XML
document.
Methods¶
- __init__(self, *args, **kw)¶
-
__init__ (self, parent, type, name, content=’’, attrs=None, next=None, lineNo=-1)
Creates this
XML
node and inserts it into theXML
tree as a child of the specified parent.Once added, the
XML
tree takes ownership of this object and there is no need to delete it.- 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 theAddChild
orInsertChild
functions. Otherwise the child is added to theXML
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
orXML_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.
- Return type:
None
__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
orXML_CDATA_SECTION_NODE
.lineNo (int) – Number of line this node was present at in input file or -1.
- Return type:
None
__init__ (self, node)
Copy constructor.
Note that this does NOT copy siblings and parent pointer, i.e.
GetParent
andGetNext
will returnNone
after using copy constructor and are never unmodified byoperator=
. On the other hand, it DOES copy children and attributes.- Parameters:
node (wx.xml.XmlNode)
- Return type:
None
- AddAttribute(self, *args, **kw)¶
-
AddAttribute (self, name, value)
Appends an attribute with given name and value to the list of attributes for this node.
- Parameters:
name (string)
value (string)
- Return type:
None
AddAttribute (self, attr)
Appends given attribute to the list of attributes for this node.
- Parameters:
attr (wx.xml.XmlAttribute)
- Return type:
None
- AddChild(self, child)¶
Adds node child as the last child of this node.
Once added, the
XML
tree takes ownership of this object and there is no need to delete it.- Parameters:
child (wx.xml.XmlNode)
- Return type:
None
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 populateXML
tree in linear time.See also
- 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:
str
- GetAttributes(self)¶
Return a pointer to the first attribute of this node.
- Return type:
- 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:
- 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. SeeGetNodeContent
for more details.- Return type:
str
- 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
orXML_CDATA_SECTION_NODE
).- Return type:
str
- GetNext(self)¶
Returns a pointer to the sibling of this node or
None
if there are no siblings.- Return type:
- 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
orXML_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
orXML_CDATA_SECTION_NODE
, or if the content of the first child of such types is empty.- Return type:
str
- GetParent(self)¶
Returns a pointer to the parent of this node or
None
if this node has no parent.- Return type:
- GetType(self)¶
Returns the type of this node.
- Return type:
- 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.
Once inserted, the
XML
tree takes ownership of the new child and there is no need to delete it.- Parameters:
child (wx.xml.XmlNode)
followingNode (wx.xml.XmlNode)
- 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 byGetChildren
) for followingNode).See also
- InsertChildAfter(self, child, precedingNode)¶
Inserts the child node immediately after precedingNode in the children list.
Once inserted, the
XML
tree takes ownership of the new child and there is no need to delete it.- 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 – in that case, 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.
Added in version 2.8.8.
See also
- 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 orFalse
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)
- Return type:
None
- SetName(self, name)¶
Sets the name of this node.
- Parameters:
name (string)
- Return type:
None
- 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)
- Return type:
None
- 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)
- Return type:
None
- 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)
- Return type:
None
- SetType(self, type)¶
Sets the type of this node.
- Parameters:
type (XmlNodeType)
- Return type:
None
Properties¶
- Attributes¶
See
GetAttributes
- Children¶
See
GetChildren
- Content¶
See
GetContent
andSetContent
- LineNumber¶
See
GetLineNumber
- NoConversion¶
See
GetNoConversion
andSetNoConversion
- NodeContent¶
See
GetNodeContent