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_LIBXML
, // use libxml2 to parse/save XML document
58 wxXML_IO_BIN
, // save in binary uncompressed proprietary format
59 wxXML_IO_BINZ
// svae in binary zlib-compressed proprietary format
63 // Represents node property(ies).
64 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
65 // "hello.gif" and "id" is prop. with value "3".
67 class WXDLLEXPORT wxXmlProperty
70 wxXmlProperty() : m_Next(NULL
) {}
71 wxXmlProperty(const wxString
& name
, const wxString
& value
, wxXmlProperty
*next
)
72 : m_Name(name
), m_Value(value
), m_Next(next
) {}
73 ~wxXmlProperty() { delete m_Next
; }
75 wxString
GetName() const { return m_Name
; }
76 wxString
GetValue() const { return m_Value
; }
77 wxXmlProperty
*GetNext() const { return m_Next
; }
79 void SetName(const wxString
& name
) { m_Name
= name
; }
80 void SetValue(const wxString
& value
) { m_Value
= value
; }
81 void SetNext(wxXmlProperty
*next
) { m_Next
= next
; }
86 wxXmlProperty
*m_Next
;
91 // Represents node in XML document. Node has name and may have content
92 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
93 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
94 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
95 // with content="hi").
97 class WXDLLEXPORT wxXmlNode
100 wxXmlNode() : m_Properties(NULL
), m_Parent(NULL
),
101 m_Children(NULL
), m_Next(NULL
) {}
102 wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
103 const wxString
& name
, const wxString
& content
,
104 wxXmlProperty
*props
, wxXmlNode
*next
);
105 ~wxXmlNode() { delete m_Properties
; delete m_Next
; delete m_Children
; }
107 // copy ctor & operator=. Note that this does NOT copy syblings
108 // and parent pointer, i.e. m_Parent and m_Next will be NULL
109 // after using copy ctor and are never unmodified by operator=.
110 // On the other hand, it DOES copy children and properties.
111 wxXmlNode(const wxXmlNode
& node
);
112 wxXmlNode
& operator=(const wxXmlNode
& node
);
114 // user-friendly creation:
115 wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
116 const wxString
& content
= wxEmptyString
);
117 void AddChild(wxXmlNode
*child
);
118 void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
119 bool RemoveChild(wxXmlNode
*child
);
120 void AddProperty(const wxString
& name
, const wxString
& value
);
121 bool DeleteProperty(const wxString
& name
);
124 wxXmlNodeType
GetType() const { return m_Type
; }
125 wxString
GetName() const { return m_Name
; }
126 wxString
GetContent() const { return m_Content
; }
128 wxXmlNode
*GetParent() const { return m_Parent
; }
129 wxXmlNode
*GetNext() const { return m_Next
; }
130 wxXmlNode
*GetChildren() const { return m_Children
; }
132 wxXmlProperty
*GetProperties() const { return m_Properties
; }
133 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
134 wxString
GetPropVal(const wxString
& propName
, 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 libxml. Note that
165 // internal representation is independant on libxml and you can use
166 // it without libxml (see Load/SaveBinary).
168 class WXDLLEXPORT wxXmlDocument
: public wxObject
171 wxXmlDocument() : wxObject(), m_Version(_T("1.0")), m_Root(NULL
) {}
172 wxXmlDocument(const wxString
& filename
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
173 wxXmlDocument(wxInputStream
& stream
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
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 // NOTE: Any call to this method will result into linking against libxml
182 // and app's binary size will grow by ca. 250kB
183 bool Load(const wxString
& filename
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
184 bool Load(wxInputStream
& stream
, wxXmlIOType io_type
= wxXML_IO_AUTO
);
186 // Saves document as .xml file.
187 bool Save(const wxString
& filename
, wxXmlIOType io_type
) const;
188 bool Save(wxOutputStream
& stream
, wxXmlIOType io_type
) const;
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 wxString
GetEncoding() const { return m_Encoding
; }
198 // Write-access methods:
199 void SetRoot(wxXmlNode
*node
) { delete m_Root
; m_Root
= node
; }
200 void SetVersion(const wxString
& version
) { m_Version
= version
; }
201 void SetEncoding(const wxString
& encoding
) { m_Encoding
= encoding
; }
203 static void AddHandler(wxXmlIOHandler
*handler
);
204 static void CleanUpHandlers();
205 static void InitStandardHandlers();
208 static wxList
*sm_Handlers
;
211 wxString m_Version
, m_Encoding
;
214 void DoCopy(const wxXmlDocument
& doc
);
219 // wxXmlIOHandler takes care of loading and/or saving XML data.
220 // see xmlio.h for available handlers
222 class WXDLLEXPORT wxXmlIOHandler
: public wxObject
227 virtual wxXmlIOType
GetType() = 0;
228 virtual bool CanLoad(wxInputStream
& stream
) = 0;
229 virtual bool CanSave() = 0;
231 virtual bool Load(wxInputStream
& stream
, wxXmlDocument
& doc
) = 0;
232 virtual bool Save(wxOutputStream
& stream
, const wxXmlDocument
& doc
) = 0;