1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
14 #if defined(__GNUG__) && !defined(__APPLE__)
15 #pragma interface "xml.h"
19 #include "wx/string.h"
20 #include "wx/object.h"
23 #ifdef WXMAKINGDLL_XRC
24 #define WXDLLIMPEXP_XRC WXEXPORT
25 #elif defined(WXUSINGDLL)
26 #define WXDLLIMPEXP_XRC WXIMPORT
27 #else // not making nor using DLL
28 #define WXDLLIMPEXP_XRC
31 class WXDLLIMPEXP_XRC wxXmlNode
;
32 class WXDLLIMPEXP_XRC wxXmlProperty
;
33 class WXDLLIMPEXP_XRC wxXmlDocument
;
34 class WXDLLIMPEXP_XRC wxXmlIOHandler
;
35 class WXDLLEXPORT wxInputStream
;
36 class WXDLLEXPORT 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_XRC wxXmlProperty
66 wxXmlProperty() : m_next(NULL
) {}
67 wxXmlProperty(const wxString
& name
, const wxString
& value
,
69 : m_name(name
), m_value(value
), m_next(next
) {}
71 wxString
GetName() const { return m_name
; }
72 wxString
GetValue() const { return m_value
; }
73 wxXmlProperty
*GetNext() const { return m_next
; }
75 void SetName(const wxString
& name
) { m_name
= name
; }
76 void SetValue(const wxString
& value
) { m_value
= value
; }
77 void SetNext(wxXmlProperty
*next
) { m_next
= next
; }
82 wxXmlProperty
*m_next
;
87 // Represents node in XML document. Node has name and may have content
88 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
89 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
90 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
91 // with content="hi").
93 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
94 // (default is UTF-8).
96 class WXDLLIMPEXP_XRC wxXmlNode
99 wxXmlNode() : m_properties(NULL
), m_parent(NULL
),
100 m_children(NULL
), m_next(NULL
) {}
101 wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
102 const wxString
& name
, const wxString
& content
,
103 wxXmlProperty
*props
, wxXmlNode
*next
);
106 // copy ctor & operator=. Note that this does NOT copy syblings
107 // and parent pointer, i.e. m_parent and m_next will be NULL
108 // after using copy ctor and are never unmodified by operator=.
109 // On the other hand, it DOES copy children and properties.
110 wxXmlNode(const wxXmlNode
& node
);
111 wxXmlNode
& operator=(const wxXmlNode
& node
);
113 // user-friendly creation:
114 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
115 const wxString
& content
= wxEmptyString
);
116 void AddChild(wxXmlNode
*child
);
117 void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
118 bool RemoveChild(wxXmlNode
*child
);
119 void AddProperty(const wxString
& name
, const wxString
& value
);
120 bool DeleteProperty(const wxString
& name
);
123 wxXmlNodeType
GetType() const { return m_type
; }
124 wxString
GetName() const { return m_name
; }
125 wxString
GetContent() const { return m_content
; }
127 wxXmlNode
*GetParent() const { return m_parent
; }
128 wxXmlNode
*GetNext() const { return m_next
; }
129 wxXmlNode
*GetChildren() const { return m_children
; }
131 wxXmlProperty
*GetProperties() const { return m_properties
; }
132 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
133 wxString
GetPropVal(const wxString
& propName
,
134 const wxString
& defaultVal
) const;
135 bool HasProp(const wxString
& propName
) const;
137 void SetType(wxXmlNodeType type
) { m_type
= type
; }
138 void SetName(const wxString
& name
) { m_name
= name
; }
139 void SetContent(const wxString
& con
) { m_content
= con
; }
141 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
142 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
143 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
145 void SetProperties(wxXmlProperty
*prop
) { m_properties
= prop
; }
146 void AddProperty(wxXmlProperty
*prop
);
149 wxXmlNodeType m_type
;
152 wxXmlProperty
*m_properties
;
153 wxXmlNode
*m_parent
, *m_children
, *m_next
;
155 void DoCopy(const wxXmlNode
& node
);
164 // This class holds XML data/document as parsed by XML parser.
166 class WXDLLIMPEXP_XRC wxXmlDocument
: public wxObject
170 wxXmlDocument(const wxString
& filename
,
171 const wxString
& encoding
= wxT("UTF-8"));
172 wxXmlDocument(wxInputStream
& stream
,
173 const wxString
& encoding
= wxT("UTF-8"));
174 ~wxXmlDocument() { delete m_root
; }
176 wxXmlDocument(const wxXmlDocument
& doc
);
177 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
179 // Parses .xml file and loads data. Returns TRUE on success, FALSE
181 bool Load(const wxString
& filename
,
182 const wxString
& encoding
= wxT("UTF-8"));
183 bool Load(wxInputStream
& stream
,
184 const wxString
& encoding
= wxT("UTF-8"));
186 // Saves document as .xml file.
187 bool Save(const wxString
& filename
) const;
188 bool Save(wxOutputStream
& stream
) const;
190 bool IsOk() const { return m_root
!= NULL
; }
192 // Returns root node of the document.
193 wxXmlNode
*GetRoot() const { return m_root
; }
195 // Returns version of document (may be empty).
196 wxString
GetVersion() const { return m_version
; }
197 // Returns encoding of document (may be empty).
198 // Note: this is the encoding original file was saved in, *not* the
199 // encoding of in-memory representation!
200 wxString
GetFileEncoding() const { return m_fileEncoding
; }
202 // Write-access methods:
203 void SetRoot(wxXmlNode
*node
) { delete m_root
; m_root
= node
; }
204 void SetVersion(const wxString
& version
) { m_version
= version
; }
205 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
208 // Returns encoding of in-memory representation of the document
209 // (same as passed to Load or ctor, defaults to UTF-8).
210 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
211 wxString
GetEncoding() const { return m_encoding
; }
212 void SetEncoding(const wxString
& enc
) { m_encoding
= enc
; }
217 wxString m_fileEncoding
;
223 void DoCopy(const wxXmlDocument
& doc
);