1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #pragma interface "xml.h"
19 #include "wx/string.h"
20 #include "wx/object.h"
24 class WXDLLEXPORT wxXmlNode
;
25 class WXDLLEXPORT wxXmlProperty
;
26 class WXDLLEXPORT wxXmlDocument
;
27 class WXDLLEXPORT wxXmlIOHandler
;
28 class WXDLLEXPORT wxInputStream
;
29 class WXDLLEXPORT wxOutputStream
;
32 // Represents XML node type.
35 // note: values are synchronized with xmlElementType from libxml
36 wxXML_ELEMENT_NODE
= 1,
37 wxXML_ATTRIBUTE_NODE
= 2,
39 wxXML_CDATA_SECTION_NODE
= 4,
40 wxXML_ENTITY_REF_NODE
= 5,
41 wxXML_ENTITY_NODE
= 6,
43 wxXML_COMMENT_NODE
= 8,
44 wxXML_DOCUMENT_NODE
= 9,
45 wxXML_DOCUMENT_TYPE_NODE
= 10,
46 wxXML_DOCUMENT_FRAG_NODE
= 11,
47 wxXML_NOTATION_NODE
= 12,
48 wxXML_HTML_DOCUMENT_NODE
= 13
52 // Types of XML files:
56 wxXML_IO_AUTO
= 0, // detect it automatically
57 wxXML_IO_EXPAT
, // use Expat to load from text/xml document
58 wxXML_IO_TEXT_OUTPUT
, // generic saver into text/xml
59 wxXML_IO_BIN
, // save in binary uncompressed proprietary format
60 wxXML_IO_BINZ
// svae in binary zlib-compressed proprietary format
64 // Represents node property(ies).
65 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
66 // "hello.gif" and "id" is prop. with value "3".
68 class WXDLLEXPORT wxXmlProperty
71 wxXmlProperty() : m_next(NULL
) {}
72 wxXmlProperty(const wxString
& name
, const wxString
& value
,
74 : m_name(name
), m_value(value
), m_next(next
) {}
75 ~wxXmlProperty() { delete m_next
; }
77 wxString
GetName() const { return m_name
; }
78 wxString
GetValue() const { return m_value
; }
79 wxXmlProperty
*GetNext() const { return m_next
; }
81 void SetName(const wxString
& name
) { m_name
= name
; }
82 void SetValue(const wxString
& value
) { m_value
= value
; }
83 void SetNext(wxXmlProperty
*next
) { m_next
= next
; }
88 wxXmlProperty
*m_next
;
93 // Represents node in XML document. Node has name and may have content
94 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
95 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
96 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
97 // with content="hi").
99 // If wxUSE_UNICODE is 0, all strings are encoded in UTF-8 encoding (same as
100 // ASCII for characters 0-127). You can use wxMBConvUTF8 to convert then to
103 // wxCSConv myConv("iso8859-2");
104 // wxString s(cMB2WC(node->GetContent().c_str()), myConv);
106 class WXDLLEXPORT wxXmlNode
109 wxXmlNode() : m_properties(NULL
), m_parent(NULL
),
110 m_children(NULL
), m_next(NULL
) {}
111 wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
112 const wxString
& name
, const wxString
& content
,
113 wxXmlProperty
*props
, wxXmlNode
*next
);
114 ~wxXmlNode() { delete m_properties
; delete m_next
; delete m_children
; }
116 // copy ctor & operator=. Note that this does NOT copy syblings
117 // and parent pointer, i.e. m_parent and m_next will be NULL
118 // after using copy ctor and are never unmodified by operator=.
119 // On the other hand, it DOES copy children and properties.
120 wxXmlNode(const wxXmlNode
& node
);
121 wxXmlNode
& operator=(const wxXmlNode
& node
);
123 // user-friendly creation:
124 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
125 const wxString
& content
= wxEmptyString
);
126 void AddChild(wxXmlNode
*child
);
127 void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
128 bool RemoveChild(wxXmlNode
*child
);
129 void AddProperty(const wxString
& name
, const wxString
& value
);
130 bool DeleteProperty(const wxString
& name
);
133 wxXmlNodeType
GetType() const { return m_type
; }
134 wxString
GetName() const { return m_name
; }
135 wxString
GetContent() const { return m_content
; }
137 wxXmlNode
*GetParent() const { return m_parent
; }
138 wxXmlNode
*GetNext() const { return m_next
; }
139 wxXmlNode
*GetChildren() const { return m_children
; }
141 wxXmlProperty
*GetProperties() const { return m_properties
; }
142 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
143 wxString
GetPropVal(const wxString
& propName
,
144 const wxString
& defaultVal
) const;
145 bool HasProp(const wxString
& propName
) const;
147 void SetType(wxXmlNodeType type
) { m_type
= type
; }
148 void SetName(const wxString
& name
) { m_name
= name
; }
149 void SetContent(const wxString
& con
) { m_content
= con
; }
151 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
152 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
153 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
155 void SetProperties(wxXmlProperty
*prop
) { m_properties
= prop
; }
156 void AddProperty(wxXmlProperty
*prop
);
159 wxXmlNodeType m_type
;
162 wxXmlProperty
*m_properties
;
163 wxXmlNode
*m_parent
, *m_children
, *m_next
;
165 void DoCopy(const wxXmlNode
& node
);
174 // This class holds XML data/document as parsed by libxml. Note that
175 // internal representation is independant on libxml and you can use
176 // it without libxml (see Load/SaveBinary).
178 class WXDLLEXPORT wxXmlDocument
: public wxObject
181 wxXmlDocument() : wxObject(), m_version(wxT("1.0")), m_root(NULL
) {}
182 wxXmlDocument(const wxString
& filename
,
183 wxXmlIOType io_type
= wxXML_IO_AUTO
);
184 wxXmlDocument(wxInputStream
& stream
,
185 wxXmlIOType io_type
= wxXML_IO_AUTO
);
186 ~wxXmlDocument() { delete m_root
; }
188 wxXmlDocument(const wxXmlDocument
& doc
);
189 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
191 // Parses .xml file and loads data. Returns TRUE on success, FALSE
193 // NOTE: Any call to this method will result into linking against libxml
194 // and app's binary size will grow by ca. 250kB
195 bool Load(const wxString
& filename
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
196 bool Load(wxInputStream
& stream
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
198 // Saves document as .xml file.
199 bool Save(const wxString
& filename
,
200 wxXmlIOType io_type
= wxXML_IO_TEXT_OUTPUT
) const;
201 bool Save(wxOutputStream
& stream
,
202 wxXmlIOType io_type
= wxXML_IO_TEXT_OUTPUT
) const;
204 bool IsOk() const { return m_root
!= NULL
; }
206 // Returns root node of the document.
207 wxXmlNode
*GetRoot() const { return m_root
; }
209 // Returns version of document (may be empty).
210 wxString
GetVersion() const { return m_version
; }
211 // Returns encoding of document (may be empty).
212 // Note: this is the encoding original fail was saved in, *not* the
213 // encoding of in-memory representation! Data in wxXmlNode are always
214 // stored in wchar_t (in Unicode build) or UTF-8 encoded
215 // (if wxUSE_UNICODE is 0).
216 wxString
GetEncoding() const { return m_encoding
; }
218 // Write-access methods:
219 void SetRoot(wxXmlNode
*node
) { delete m_root
; m_root
= node
; }
220 void SetVersion(const wxString
& version
) { m_version
= version
; }
221 void SetEncoding(const wxString
& encoding
) { m_encoding
= encoding
; }
223 static void AddHandler(wxXmlIOHandler
*handler
);
224 static void CleanUpHandlers();
225 static void InitStandardHandlers();
228 static wxList
*sm_handlers
;
231 wxString m_version
, m_encoding
;
234 void DoCopy(const wxXmlDocument
& doc
);
239 // wxXmlIOHandler takes care of loading and/or saving XML data.
240 // see xmlio.h for available handlers
242 class WXDLLEXPORT wxXmlIOHandler
: public wxObject
247 virtual wxXmlIOType
GetType() = 0;
248 virtual bool CanLoad(wxInputStream
& stream
) = 0;
249 virtual bool CanSave() = 0;
251 virtual bool Load(wxInputStream
& stream
, wxXmlDocument
& doc
) = 0;
252 virtual bool Save(wxOutputStream
& stream
, const wxXmlDocument
& doc
) = 0;