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
Default constructor. |
|
Appends a Process Instruction or Comment node to the document prologue. |
|
Detaches the document node and returns it. |
|
Detaches the root entity node and returns it. |
|
Returns the document node of the document. |
|
Returns encoding of document (may be empty). |
|
Get expat library version information. |
|
Returns the root element node of the document. |
|
Returns the version of document. |
|
Returns |
|
Parses filename as an xml document and loads its data. |
|
Saves |
|
Sets the document node of this document. |
|
Sets the encoding of the file which will be used to save the document. |
|
Sets the root element node of this document. |
|
Sets the version of the |
See |
|
See |
|
See |
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.
doc (wx.xml.XmlDocument) –
__init__ (self, filename, encoding=”UTF-8”)
Loads the given filename using the given encoding.
See Load
.
filename (string) –
encoding (string) –
__init__ (self, stream, encoding=”UTF-8”)
Loads the XML
document from given stream using the given encoding.
See Load
.
stream (wx.InputStream) –
encoding (string) –
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.
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.
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.
GetDocumentNode
(self)¶Returns the document node of the document.
New in version 2.9.2.
GetFileEncoding
(self)¶Returns encoding of document (may be empty).
string
Note
This is the encoding original file was saved in, not the encoding of in-memory representation!
GetLibraryVersionInfo
()¶Get expat library version information.
VersionInfo
New in version 2.9.2.
See also
GetRoot
(self)¶Returns the root element node of the document.
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.
string
IsOk
(self)¶Returns True
if the document has been loaded successfully.
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.
filename (string) –
encoding (string) –
flags (int) –
bool
Load (self, stream, encoding=”UTF-8”, flags=XMLDOC_NONE)
Like Load
but takes the data from given input stream.
stream (wx.InputStream) –
encoding (string) –
flags (int) –
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.
filename (string) –
indentstep (int) –
bool
Save (self, stream, indentstep=2)
Saves XML
tree in the given output stream.
See Save(const String&, int) for a description of indentstep.
stream (wx.OutputStream) –
indentstep (int) –
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.
node (wx.xml.XmlNode) –
New in version 2.9.2.
SetFileEncoding
(self, encoding)¶Sets the encoding of the file which will be used to save the document.
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.
node (wx.xml.XmlNode) –
SetVersion
(self, version)¶Sets the version of the XML
file which will be used to save the document.
version (string) –
DocumentNode
¶See GetDocumentNode
and SetDocumentNode
FileEncoding
¶See GetFileEncoding
and SetFileEncoding
Version
¶See GetVersion
and SetVersion