1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 /* ************************************************************************* *
15 * The API defined in this header *WILL* change in the future and backward *
16 * compatibility will *not* be preserved. If you use these classes in your *
17 * application, it probably won't compile with future wxWindows releases. *
18 * Use on your own risk. *
20 * ************************************************************************* */
25 #if defined(__GNUG__) && !defined(__APPLE__)
26 #pragma interface "xml.h"
33 #include "wx/string.h"
34 #include "wx/object.h"
37 #ifdef WXMAKINGDLL_XML
38 #define WXDLLIMPEXP_XML WXEXPORT
39 #elif defined(WXUSINGDLL)
40 #define WXDLLIMPEXP_XML WXIMPORT
41 #else // not making nor using DLL
42 #define WXDLLIMPEXP_XML
45 class WXDLLIMPEXP_XML wxXmlNode
;
46 class WXDLLIMPEXP_XML wxXmlProperty
;
47 class WXDLLIMPEXP_XML wxXmlDocument
;
48 class WXDLLIMPEXP_XML wxXmlIOHandler
;
49 class WXDLLIMPEXP_BASE wxInputStream
;
50 class WXDLLIMPEXP_BASE wxOutputStream
;
53 // Represents XML node type.
56 // note: values are synchronized with xmlElementType from libxml
57 wxXML_ELEMENT_NODE
= 1,
58 wxXML_ATTRIBUTE_NODE
= 2,
60 wxXML_CDATA_SECTION_NODE
= 4,
61 wxXML_ENTITY_REF_NODE
= 5,
62 wxXML_ENTITY_NODE
= 6,
64 wxXML_COMMENT_NODE
= 8,
65 wxXML_DOCUMENT_NODE
= 9,
66 wxXML_DOCUMENT_TYPE_NODE
= 10,
67 wxXML_DOCUMENT_FRAG_NODE
= 11,
68 wxXML_NOTATION_NODE
= 12,
69 wxXML_HTML_DOCUMENT_NODE
= 13
73 // Represents node property(ies).
74 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
75 // "hello.gif" and "id" is prop. with value "3".
77 class WXDLLIMPEXP_XML wxXmlProperty
80 wxXmlProperty() : m_next(NULL
) {}
81 wxXmlProperty(const wxString
& name
, const wxString
& value
,
83 : m_name(name
), m_value(value
), m_next(next
) {}
85 wxString
GetName() const { return m_name
; }
86 wxString
GetValue() const { return m_value
; }
87 wxXmlProperty
*GetNext() const { return m_next
; }
89 void SetName(const wxString
& name
) { m_name
= name
; }
90 void SetValue(const wxString
& value
) { m_value
= value
; }
91 void SetNext(wxXmlProperty
*next
) { m_next
= next
; }
96 wxXmlProperty
*m_next
;
101 // Represents node in XML document. Node has name and may have content
102 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
103 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
104 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
105 // with content="hi").
107 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
108 // (default is UTF-8).
110 class WXDLLIMPEXP_XML wxXmlNode
113 wxXmlNode() : m_properties(NULL
), m_parent(NULL
),
114 m_children(NULL
), m_next(NULL
) {}
115 wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
116 const wxString
& name
, const wxString
& content
,
117 wxXmlProperty
*props
, wxXmlNode
*next
);
120 // copy ctor & operator=. Note that this does NOT copy syblings
121 // and parent pointer, i.e. m_parent and m_next will be NULL
122 // after using copy ctor and are never unmodified by operator=.
123 // On the other hand, it DOES copy children and properties.
124 wxXmlNode(const wxXmlNode
& node
);
125 wxXmlNode
& operator=(const wxXmlNode
& node
);
127 // user-friendly creation:
128 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
129 const wxString
& content
= wxEmptyString
);
130 void AddChild(wxXmlNode
*child
);
131 void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
132 bool RemoveChild(wxXmlNode
*child
);
133 void AddProperty(const wxString
& name
, const wxString
& value
);
134 bool DeleteProperty(const wxString
& name
);
137 wxXmlNodeType
GetType() const { return m_type
; }
138 wxString
GetName() const { return m_name
; }
139 wxString
GetContent() const { return m_content
; }
141 wxXmlNode
*GetParent() const { return m_parent
; }
142 wxXmlNode
*GetNext() const { return m_next
; }
143 wxXmlNode
*GetChildren() const { return m_children
; }
145 wxXmlProperty
*GetProperties() const { return m_properties
; }
146 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
147 wxString
GetPropVal(const wxString
& propName
,
148 const wxString
& defaultVal
) const;
149 bool HasProp(const wxString
& propName
) const;
151 void SetType(wxXmlNodeType type
) { m_type
= type
; }
152 void SetName(const wxString
& name
) { m_name
= name
; }
153 void SetContent(const wxString
& con
) { m_content
= con
; }
155 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
156 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
157 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
159 void SetProperties(wxXmlProperty
*prop
) { m_properties
= prop
; }
160 void AddProperty(wxXmlProperty
*prop
);
163 wxXmlNodeType m_type
;
166 wxXmlProperty
*m_properties
;
167 wxXmlNode
*m_parent
, *m_children
, *m_next
;
169 void DoCopy(const wxXmlNode
& node
);
178 // This class holds XML data/document as parsed by XML parser.
180 class WXDLLIMPEXP_XML wxXmlDocument
: public wxObject
184 wxXmlDocument(const wxString
& filename
,
185 const wxString
& encoding
= wxT("UTF-8"));
186 wxXmlDocument(wxInputStream
& stream
,
187 const wxString
& encoding
= wxT("UTF-8"));
188 ~wxXmlDocument() { delete m_root
; }
190 wxXmlDocument(const wxXmlDocument
& doc
);
191 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
193 // Parses .xml file and loads data. Returns TRUE on success, FALSE
195 bool Load(const wxString
& filename
,
196 const wxString
& encoding
= wxT("UTF-8"));
197 bool Load(wxInputStream
& stream
,
198 const wxString
& encoding
= wxT("UTF-8"));
200 // Saves document as .xml file.
201 bool Save(const wxString
& filename
) const;
202 bool Save(wxOutputStream
& stream
) 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 file was saved in, *not* the
213 // encoding of in-memory representation!
214 wxString
GetFileEncoding() const { return m_fileEncoding
; }
216 // Write-access methods:
217 void SetRoot(wxXmlNode
*node
) { delete m_root
; m_root
= node
; }
218 void SetVersion(const wxString
& version
) { m_version
= version
; }
219 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
222 // Returns encoding of in-memory representation of the document
223 // (same as passed to Load or ctor, defaults to UTF-8).
224 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
225 wxString
GetEncoding() const { return m_encoding
; }
226 void SetEncoding(const wxString
& enc
) { m_encoding
= enc
; }
231 wxString m_fileEncoding
;
237 void DoCopy(const wxXmlDocument
& doc
);