1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
19 #include "wx/string.h"
20 #include "wx/object.h"
23 #ifdef WXMAKINGDLL_XML
24 #define WXDLLIMPEXP_XML WXEXPORT
25 #elif defined(WXUSINGDLL)
26 #define WXDLLIMPEXP_XML WXIMPORT
27 #else // not making nor using DLL
28 #define WXDLLIMPEXP_XML
31 class WXDLLIMPEXP_FWD_XML wxXmlNode
;
32 class WXDLLIMPEXP_FWD_XML wxXmlAttribute
;
33 class WXDLLIMPEXP_FWD_XML wxXmlDocument
;
34 class WXDLLIMPEXP_FWD_XML wxXmlIOHandler
;
35 class WXDLLIMPEXP_FWD_BASE wxInputStream
;
36 class WXDLLIMPEXP_FWD_BASE wxOutputStream
;
39 // Represents XML node type.
42 // note: values are synchronized with xmlElementType from libxml
43 wxXML_ELEMENT_NODE
= 1,
44 wxXML_ATTRIBUTE_NODE
= 2,
46 wxXML_CDATA_SECTION_NODE
= 4,
47 wxXML_ENTITY_REF_NODE
= 5,
48 wxXML_ENTITY_NODE
= 6,
50 wxXML_COMMENT_NODE
= 8,
51 wxXML_DOCUMENT_NODE
= 9,
52 wxXML_DOCUMENT_TYPE_NODE
= 10,
53 wxXML_DOCUMENT_FRAG_NODE
= 11,
54 wxXML_NOTATION_NODE
= 12,
55 wxXML_HTML_DOCUMENT_NODE
= 13
59 // Represents node property(ies).
60 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
61 // "hello.gif" and "id" is prop. with value "3".
63 class WXDLLIMPEXP_XML wxXmlAttribute
66 wxXmlAttribute() : m_next(NULL
) {}
67 wxXmlAttribute(const wxString
& name
, const wxString
& value
,
68 wxXmlAttribute
*next
= NULL
)
69 : m_name(name
), m_value(value
), m_next(next
) {}
70 virtual ~wxXmlAttribute() {}
72 wxString
GetName() const { return m_name
; }
73 wxString
GetValue() const { return m_value
; }
74 wxXmlAttribute
*GetNext() const { return m_next
; }
76 void SetName(const wxString
& name
) { m_name
= name
; }
77 void SetValue(const wxString
& value
) { m_value
= value
; }
78 void SetNext(wxXmlAttribute
*next
) { m_next
= next
; }
83 wxXmlAttribute
*m_next
;
86 #if WXWIN_COMPATIBILITY_2_8
87 // NB: #define is used instead of typedef so that forward declarations
89 #define wxXmlProperty wxXmlAttribute
93 // Represents node in XML document. Node has name and may have content and
94 // attributes. Most common node types are wxXML_TEXT_NODE (name and attributes
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 the encoding given to Load
100 // (default is UTF-8).
102 class WXDLLIMPEXP_XML wxXmlNode
106 : m_attrs(NULL
), m_parent(NULL
), m_children(NULL
), m_next(NULL
),
111 wxXmlNode(wxXmlNode
*parent
, wxXmlNodeType type
,
112 const wxString
& name
, const wxString
& content
= wxEmptyString
,
113 wxXmlAttribute
*attrs
= NULL
, wxXmlNode
*next
= NULL
,
116 virtual ~wxXmlNode();
118 // copy ctor & operator=. Note that this does NOT copy syblings
119 // and parent pointer, i.e. m_parent and m_next will be NULL
120 // after using copy ctor and are never unmodified by operator=.
121 // On the other hand, it DOES copy children and attributes.
122 wxXmlNode(const wxXmlNode
& node
);
123 wxXmlNode
& operator=(const wxXmlNode
& node
);
125 // user-friendly creation:
126 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
127 const wxString
& content
= wxEmptyString
,
129 virtual void AddChild(wxXmlNode
*child
);
130 virtual bool InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
131 virtual bool RemoveChild(wxXmlNode
*child
);
132 virtual void AddAttribute(const wxString
& name
, const wxString
& value
);
133 virtual bool DeleteAttribute(const wxString
& name
);
136 wxXmlNodeType
GetType() const { return m_type
; }
137 const wxString
& GetName() const { return m_name
; }
138 const wxString
& GetContent() const { return m_content
; }
140 bool IsWhitespaceOnly() const;
141 int GetDepth(wxXmlNode
*grandparent
= NULL
) const;
143 // Gets node content from wxXML_ENTITY_NODE
144 // The problem is, <tag>content<tag> is represented as
145 // wxXML_ENTITY_NODE name="tag", content=""
146 // |-- wxXML_TEXT_NODE or
147 // wxXML_CDATA_SECTION_NODE name="" content="content"
148 wxString
GetNodeContent() const;
150 wxXmlNode
*GetParent() const { return m_parent
; }
151 wxXmlNode
*GetNext() const { return m_next
; }
152 wxXmlNode
*GetChildren() const { return m_children
; }
154 wxXmlAttribute
*GetAttributes() const { return m_attrs
; }
155 bool GetAttribute(const wxString
& attrName
, wxString
*value
) const;
156 wxString
GetAttribute(const wxString
& attrName
,
157 const wxString
& defaultVal
) const;
158 bool HasAttribute(const wxString
& attrName
) const;
160 int GetLineNumber() const { return m_lineNo
; }
162 void SetType(wxXmlNodeType type
) { m_type
= type
; }
163 void SetName(const wxString
& name
) { m_name
= name
; }
164 void SetContent(const wxString
& con
) { m_content
= con
; }
166 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
167 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
168 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
170 void SetAttributes(wxXmlAttribute
*attr
) { m_attrs
= attr
; }
171 virtual void AddAttribute(wxXmlAttribute
*attr
);
173 #if WXWIN_COMPATIBILITY_2_8
174 wxDEPRECATED( inline wxXmlAttribute
*GetProperties() const );
175 wxDEPRECATED( inline bool GetPropVal(const wxString
& propName
,
176 wxString
*value
) const );
177 wxDEPRECATED( inline wxString
GetPropVal(const wxString
& propName
,
178 const wxString
& defaultVal
) const );
179 wxDEPRECATED( inline bool HasProp(const wxString
& propName
) const );
181 wxDEPRECATED( inline void SetProperties(wxXmlAttribute
*prop
) );
182 #endif // WXWIN_COMPATIBILITY_2_8
184 // The following three functions are backward compatibility, but because
185 // they were virtual, we must make it possible to override them. This
186 // is done by calling e.g. AddProperty() from AddAttribute(), so we have
187 // to keep AddProperty() even if 2.8 compatibility is off. To prevent
188 // old code from compiling in that case, we make them private and
189 // non-virtual. (This can be removed when WXWIN_COMPATIBILITY_2_8 is
190 // removed, we'll have just *Attribute versions then.)
191 #if WXWIN_COMPATIBILITY_2_8
192 wxDEPRECATED_BUT_USED_INTERNALLY(
193 virtual void AddProperty(const wxString
& name
, const wxString
& value
) );
194 wxDEPRECATED_BUT_USED_INTERNALLY(
195 virtual bool DeleteProperty(const wxString
& name
) );
196 wxDEPRECATED_BUT_USED_INTERNALLY(
197 virtual void AddProperty(wxXmlAttribute
*attr
) );
200 void AddProperty(const wxString
& name
, const wxString
& value
);
201 bool DeleteProperty(const wxString
& name
);
202 void AddProperty(wxXmlAttribute
*attr
);
203 #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8
206 wxXmlNodeType m_type
;
209 wxXmlAttribute
*m_attrs
;
210 wxXmlNode
*m_parent
, *m_children
, *m_next
;
211 int m_lineNo
; // line number in original file, or -1
213 void DoCopy(const wxXmlNode
& node
);
216 #if WXWIN_COMPATIBILITY_2_8
217 inline wxXmlAttribute
*wxXmlNode::GetProperties() const
218 { return GetAttributes(); }
219 inline bool wxXmlNode::GetPropVal(const wxString
& propName
,
220 wxString
*value
) const
221 { return GetAttribute(propName
, value
); }
222 inline wxString
wxXmlNode::GetPropVal(const wxString
& propName
,
223 const wxString
& defaultVal
) const
224 { return GetAttribute(propName
, defaultVal
); }
225 inline bool wxXmlNode::HasProp(const wxString
& propName
) const
226 { return HasAttribute(propName
); }
227 inline void wxXmlNode::SetProperties(wxXmlAttribute
*prop
)
228 { SetAttributes(prop
); }
229 #endif // WXWIN_COMPATIBILITY_2_8
233 // special indentation value for wxXmlDocument::Save
234 #define wxXML_NO_INDENTATION (-1)
236 // flags for wxXmlDocument::Load
237 enum wxXmlDocumentLoadFlag
240 wxXMLDOC_KEEP_WHITESPACE_NODES
= 1
244 // This class holds XML data/document as parsed by XML parser.
246 class WXDLLIMPEXP_XML wxXmlDocument
: public wxObject
250 wxXmlDocument(const wxString
& filename
,
251 const wxString
& encoding
= wxT("UTF-8"));
252 wxXmlDocument(wxInputStream
& stream
,
253 const wxString
& encoding
= wxT("UTF-8"));
254 virtual ~wxXmlDocument() { wxDELETE(m_root
); }
256 wxXmlDocument(const wxXmlDocument
& doc
);
257 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
259 // Parses .xml file and loads data. Returns TRUE on success, FALSE
261 virtual bool Load(const wxString
& filename
,
262 const wxString
& encoding
= wxT("UTF-8"), int flags
= wxXMLDOC_NONE
);
263 virtual bool Load(wxInputStream
& stream
,
264 const wxString
& encoding
= wxT("UTF-8"), int flags
= wxXMLDOC_NONE
);
266 // Saves document as .xml file.
267 virtual bool Save(const wxString
& filename
, int indentstep
= 1) const;
268 virtual bool Save(wxOutputStream
& stream
, int indentstep
= 1) const;
270 bool IsOk() const { return m_root
!= NULL
; }
272 // Returns root node of the document.
273 wxXmlNode
*GetRoot() const { return m_root
; }
275 // Returns version of document (may be empty).
276 const wxString
& GetVersion() const { return m_version
; }
277 // Returns encoding of document (may be empty).
278 // Note: this is the encoding original file was saved in, *not* the
279 // encoding of in-memory representation!
280 const wxString
& GetFileEncoding() const { return m_fileEncoding
; }
282 // Write-access methods:
283 wxXmlNode
*DetachRoot() { wxXmlNode
*old
=m_root
; m_root
=NULL
; return old
; }
284 void SetRoot(wxXmlNode
*node
) { wxDELETE(m_root
); m_root
= node
; }
285 void SetVersion(const wxString
& version
) { m_version
= version
; }
286 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
289 // Returns encoding of in-memory representation of the document
290 // (same as passed to Load or ctor, defaults to UTF-8).
291 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
292 wxString
GetEncoding() const { return m_encoding
; }
293 void SetEncoding(const wxString
& enc
) { m_encoding
= enc
; }
298 wxString m_fileEncoding
;
304 void DoCopy(const wxXmlDocument
& doc
);
306 DECLARE_CLASS(wxXmlDocument
)