This class holds XML
data/document as parsed by XML
parser in the root node.
wx.xml.XmlDocument internally uses the expat library which comes with wxWidgets to parse the given stream.
A simple example of using XML
classes is:
def ScanDocument():
doc = wx.xml.XmlDocument()
if not doc.Load("myfile.xml"):
return False
# start processing the XML file
if doc.GetRoot().GetName() != "myroot-node":
return False
# examine prologue
prolog = doc.GetDocumentNode().GetChildren()
while prolog:
if prolog.GetType() == wx.xml.XML_PI_NODE and prolog.GetName() == "target":
# process Process Instruction contents
pi = prolog.GetContent()
# Other code here...
child = doc.GetRoot().GetChildren()
while child:
if child.GetName() == "tag1":
# process text enclosed by tag1/tag1
content = child.GetNodeContent()
# Other code here...
# process attributes of tag1
attrvalue1 = child.GetAttribute("attr1", "default-value")
attrvalue2 = child.GetAttribute("attr2", "default-value")
elif child.GetName() == "tag2":
# process tag2 ...
attrvalue3 = child.GetAttribute("attr3", "default-value")
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:
doc = wx.xml.XmlDocument()
doc.Load("myfile.xml", "UTF-8", wx.xml.XMLDOC_KEEP_WHITESPACE_NODES)
# myfile2.xml will be identical to myfile.xml saving it self way:
doc.Save("myfile2.xml", wx.xml.XML_NO_INDENTATION)
Using default parameters, you will get a reformatted document which in general is different from the original loaded content:
doc = wx.xml.XmlDocument()
doc.Load("myfile.xml")
doc.Save("myfile2.xml") # myfile2.xml != myfile.xml
See also
__init__ |
Default constructor. |
AppendToProlog |
Appends a Process Instruction or Comment node to the document prologue. |
DetachDocumentNode |
Detaches the document node and returns it. |
DetachRoot |
Detaches the root entity node and returns it. |
GetDocumentNode |
Returns the document node of the document. |
GetFileEncoding |
Returns encoding of document (may be empty). |
GetLibraryVersionInfo |
Get expat library version information. |
GetRoot |
Returns the root element node of the document. |
GetVersion |
Returns the version of document. |
IsOk |
Returns True if the document has been loaded successfully. |
Load |
Parses filename as an xml document and loads its data. |
Save |
Saves XML tree creating a file named with given string. |
SetDocumentNode |
Sets the document node of this document. |
SetFileEncoding |
Sets the enconding of the file which will be used to save the document. |
SetRoot |
Sets the root element node of this document. |
SetVersion |
Sets the version of the XML file which will be used to save the document. |
DocumentNode |
See GetDocumentNode and SetDocumentNode |
FileEncoding |
See GetFileEncoding and SetFileEncoding |
Root |
See GetRoot and SetRoot |
Version |
See GetVersion and SetVersion |
wx.xml.
XmlDocument
(Object)¶Possible constructors:
XmlDocument()
XmlDocument(doc)
XmlDocument(filename, encoding="UTF-8")
XmlDocument(stream, encoding="UTF-8")
This class holds XML
data/document as parsed by XML
parser in the root
node.
__init__
(self, *args, **kw)¶__init__ (self)
Default constructor.
__init__ (self, doc)
Copy constructor.
Deep copies all the XML
tree of the given document.
Parameters: | doc (wx.xml.XmlDocument) – |
---|
__init__ (self, filename, encoding=”UTF-8”)
Loads the given filename using the given encoding.
See Load
.
Parameters: |
|
---|
__init__ (self, stream, encoding=”UTF-8”)
Loads the XML
document from given stream using the given encoding.
See Load
.
Parameters: |
|
---|
AppendToProlog
(self, 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.
Parameters: | node (wx.xml.XmlNode) – |
---|
New in version 2.9.2.
DetachDocumentNode
(self)¶Detaches the document node and returns it.
The document node will be set to None
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.
Return type: | wx.xml.XmlNode |
---|
New in version 2.9.2.
DetachRoot
(self)¶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.
Return type: | wx.xml.XmlNode |
---|
GetDocumentNode
(self)¶Returns the document node of the document.
Return type: | wx.xml.XmlNode |
---|
New in version 2.9.2.
GetFileEncoding
(self)¶Returns encoding of document (may be empty).
Return type: | string |
---|
Note
This is the encoding original file was saved in, not the encoding of in-memory representation!
GetLibraryVersionInfo
()¶Get expat library version information.
Return type: | wx.VersionInfo |
---|
New in version 2.9.2.
See also
GetRoot
(self)¶Returns the root element node of the document.
Return type: | wx.xml.XmlNode |
---|
GetVersion
(self)¶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.
Return type: | string |
---|
IsOk
(self)¶Returns True
if the document has been loaded successfully.
Return type: | bool |
---|
Load
(self, *args, **kw)¶Load (self, filename, encoding=”UTF-8”, flags=XMLDOC_NONE)
Parses filename as an xml document and loads its data.
If flags does not contain wx.xml.XMLDOC_KEEP_WHITESPACE_NODES
, then, while loading, all nodes of type XML_TEXT_NODE
(see wx.xml.XmlNode) 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.
Parameters: |
|
---|---|
Return type: | bool |
Load (self, stream, encoding=”UTF-8”, flags=XMLDOC_NONE)
Like Load
but takes the data from given input stream.
Parameters: |
|
---|---|
Return type: | bool |
Save
(self, *args, **kw)¶Save (self, filename, indentstep=2)
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 XML_NO_INDENTATION
, then, automatic indentation is turned off.
Parameters: |
|
---|---|
Return type: | bool |
Save (self, stream, indentstep=2)
Saves XML
tree in the given output stream.
See Save(const String&, int) for a description of indentstep.
Parameters: |
|
---|---|
Return type: | bool |
SetDocumentNode
(self, 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.
Parameters: | node (wx.xml.XmlNode) – |
---|
New in version 2.9.2.
SetFileEncoding
(self, encoding)¶Sets the enconding of the file which will be used to save the document.
Parameters: | encoding (string) – |
---|
SetRoot
(self, node)¶Sets the root element node of this document.
Will create the document node if necessary. Any previous root element node is deleted.
Parameters: | node (wx.xml.XmlNode) – |
---|
SetVersion
(self, version)¶Sets the version of the XML
file which will be used to save the document.
Parameters: | version (string) – |
---|
DocumentNode
¶See GetDocumentNode
and SetDocumentNode
FileEncoding
¶See GetFileEncoding
and SetFileEncoding
Version
¶See GetVersion
and SetVersion