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"
24 #define WXXMLDLLEXPORT WXDLLEXPORT
26 #define WXXMLDLLEXPORT
29 class WXXMLDLLEXPORT wxXmlNode
;
30 class WXXMLDLLEXPORT wxXmlProperty
;
31 class WXXMLDLLEXPORT wxXmlDocument
;
32 class WXXMLDLLEXPORT wxXmlIOHandler
;
33 class WXDLLEXPORT wxInputStream
;
34 class WXDLLEXPORT wxOutputStream
;
37 // Represents XML node type.
40 // note: values are synchronized with xmlElementType from libxml
41 wxXML_ELEMENT_NODE
= 1,
42 wxXML_ATTRIBUTE_NODE
= 2,
44 wxXML_CDATA_SECTION_NODE
= 4,
45 wxXML_ENTITY_REF_NODE
= 5,
46 wxXML_ENTITY_NODE
= 6,
48 wxXML_COMMENT_NODE
= 8,
49 wxXML_DOCUMENT_NODE
= 9,
50 wxXML_DOCUMENT_TYPE_NODE
= 10,
51 wxXML_DOCUMENT_FRAG_NODE
= 11,
52 wxXML_NOTATION_NODE
= 12,
53 wxXML_HTML_DOCUMENT_NODE
= 13
57 // Represents node property(ies).
58 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
59 // "hello.gif" and "id" is prop. with value "3".
61 class WXXMLDLLEXPORT wxXmlProperty
64 wxXmlProperty() : m_next(NULL
) {}
65 wxXmlProperty(const wxString
& name
, const wxString
& value
,
67 : m_name(name
), m_value(value
), m_next(next
) {}
69 wxString
GetName() const { return m_name
; }
70 wxString
GetValue() const { return m_value
; }
71 wxXmlProperty
*GetNext() const { return m_next
; }
73 void SetName(const wxString
& name
) { m_name
= name
; }
74 void SetValue(const wxString
& value
) { m_value
= value
; }
75 void SetNext(wxXmlProperty
*next
) { m_next
= next
; }
80 wxXmlProperty
*m_next
;
85 // Represents node in XML document. Node has name and may have content
86 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
87 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
88 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
89 // with content="hi").
91 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
92 // (default is UTF-8).
94 class WXXMLDLLEXPORT wxXmlNode
97 wxXmlNode() : m_properties(NULL
), m_parent(NULL
),
98 m_children(NULL
), m_next(NULL
) {}
99 wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
100 const wxString
& name
, const wxString
& content
,
101 wxXmlProperty
*props
, wxXmlNode
*next
);
104 // copy ctor & operator=. Note that this does NOT copy syblings
105 // and parent pointer, i.e. m_parent and m_next will be NULL
106 // after using copy ctor and are never unmodified by operator=.
107 // On the other hand, it DOES copy children and properties.
108 wxXmlNode(const wxXmlNode
& node
);
109 wxXmlNode
& operator=(const wxXmlNode
& node
);
111 // user-friendly creation:
112 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
113 const wxString
& content
= wxEmptyString
);
114 void AddChild(wxXmlNode
*child
);
115 void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
116 bool RemoveChild(wxXmlNode
*child
);
117 void AddProperty(const wxString
& name
, const wxString
& value
);
118 bool DeleteProperty(const wxString
& name
);
121 wxXmlNodeType
GetType() const { return m_type
; }
122 wxString
GetName() const { return m_name
; }
123 wxString
GetContent() const { return m_content
; }
125 wxXmlNode
*GetParent() const { return m_parent
; }
126 wxXmlNode
*GetNext() const { return m_next
; }
127 wxXmlNode
*GetChildren() const { return m_children
; }
129 wxXmlProperty
*GetProperties() const { return m_properties
; }
130 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
131 wxString
GetPropVal(const wxString
& propName
,
132 const wxString
& defaultVal
) const;
133 bool HasProp(const wxString
& propName
) const;
135 void SetType(wxXmlNodeType type
) { m_type
= type
; }
136 void SetName(const wxString
& name
) { m_name
= name
; }
137 void SetContent(const wxString
& con
) { m_content
= con
; }
139 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
140 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
141 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
143 void SetProperties(wxXmlProperty
*prop
) { m_properties
= prop
; }
144 void AddProperty(wxXmlProperty
*prop
);
147 wxXmlNodeType m_type
;
150 wxXmlProperty
*m_properties
;
151 wxXmlNode
*m_parent
, *m_children
, *m_next
;
153 void DoCopy(const wxXmlNode
& node
);
162 // This class holds XML data/document as parsed by XML parser.
164 class WXXMLDLLEXPORT wxXmlDocument
: public wxObject
168 wxXmlDocument(const wxString
& filename
,
169 const wxString
& encoding
= wxT("UTF-8"));
170 wxXmlDocument(wxInputStream
& stream
,
171 const wxString
& encoding
= wxT("UTF-8"));
172 ~wxXmlDocument() { delete m_root
; }
174 wxXmlDocument(const wxXmlDocument
& doc
);
175 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
177 // Parses .xml file and loads data. Returns TRUE on success, FALSE
179 bool Load(const wxString
& filename
,
180 const wxString
& encoding
= wxT("UTF-8"));
181 bool Load(wxInputStream
& stream
,
182 const wxString
& encoding
= wxT("UTF-8"));
184 // Saves document as .xml file.
185 bool Save(const wxString
& filename
) const;
186 bool Save(wxOutputStream
& stream
) const;
188 bool IsOk() const { return m_root
!= NULL
; }
190 // Returns root node of the document.
191 wxXmlNode
*GetRoot() const { return m_root
; }
193 // Returns version of document (may be empty).
194 wxString
GetVersion() const { return m_version
; }
195 // Returns encoding of document (may be empty).
196 // Note: this is the encoding original file was saved in, *not* the
197 // encoding of in-memory representation!
198 wxString
GetFileEncoding() const { return m_fileEncoding
; }
200 // Write-access methods:
201 void SetRoot(wxXmlNode
*node
) { delete m_root
; m_root
= node
; }
202 void SetVersion(const wxString
& version
) { m_version
= version
; }
203 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
206 // Returns encoding of in-memory representation of the document
207 // (same as passed to Load or ctor, defaults to UTF-8).
208 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
209 wxString
GetEncoding() const { return m_encoding
; }
210 void SetEncoding(const wxString
& enc
) { m_encoding
= enc
; }
215 wxString m_fileEncoding
;
221 void DoCopy(const wxXmlDocument
& doc
);