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_XML wxXmlNode
;
32 class WXDLLIMPEXP_XML wxXmlProperty
;
33 class WXDLLIMPEXP_XML wxXmlDocument
;
34 class WXDLLIMPEXP_XML wxXmlIOHandler
;
35 class WXDLLIMPEXP_BASE wxInputStream
;
36 class WXDLLIMPEXP_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 wxXmlProperty
66 wxXmlProperty() : m_next(NULL
) {}
67 wxXmlProperty(const wxString
& name
, const wxString
& value
,
68 wxXmlProperty
*next
= NULL
)
69 : m_name(name
), m_value(value
), m_next(next
) {}
70 virtual ~wxXmlProperty() {}
72 wxString
GetName() const { return m_name
; }
73 wxString
GetValue() const { return m_value
; }
74 wxXmlProperty
*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(wxXmlProperty
*next
) { m_next
= next
; }
83 wxXmlProperty
*m_next
;
88 // Represents node in XML document. Node has name and may have content
89 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
90 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
91 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
92 // with content="hi").
94 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
95 // (default is UTF-8).
97 class WXDLLIMPEXP_XML 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
= wxEmptyString
,
104 wxXmlProperty
*props
= NULL
, wxXmlNode
*next
= NULL
);
105 virtual ~wxXmlNode();
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 virtual void AddChild(wxXmlNode
*child
);
118 virtual void InsertChild(wxXmlNode
*child
, wxXmlNode
*before_node
);
119 virtual bool RemoveChild(wxXmlNode
*child
);
120 virtual void AddProperty(const wxString
& name
, const wxString
& value
);
121 virtual 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 // Gets node content from wxXML_ENTITY_NODE
129 // The problem is, <tag>content<tag> is represented as
130 // wxXML_ENTITY_NODE name="tag", content=""
131 // |-- wxXML_TEXT_NODE or
132 // wxXML_CDATA_SECTION_NODE name="" content="content"
133 wxString
GetNodeContent() const;
135 wxXmlNode
*GetParent() const { return m_parent
; }
136 wxXmlNode
*GetNext() const { return m_next
; }
137 wxXmlNode
*GetChildren() const { return m_children
; }
139 wxXmlProperty
*GetProperties() const { return m_properties
; }
140 bool GetPropVal(const wxString
& propName
, wxString
*value
) const;
141 wxString
GetPropVal(const wxString
& propName
,
142 const wxString
& defaultVal
) const;
143 bool HasProp(const wxString
& propName
) const;
145 void SetType(wxXmlNodeType type
) { m_type
= type
; }
146 void SetName(const wxString
& name
) { m_name
= name
; }
147 void SetContent(const wxString
& con
) { m_content
= con
; }
149 void SetParent(wxXmlNode
*parent
) { m_parent
= parent
; }
150 void SetNext(wxXmlNode
*next
) { m_next
= next
; }
151 void SetChildren(wxXmlNode
*child
) { m_children
= child
; }
153 void SetProperties(wxXmlProperty
*prop
) { m_properties
= prop
; }
154 virtual void AddProperty(wxXmlProperty
*prop
);
157 wxXmlNodeType m_type
;
160 wxXmlProperty
*m_properties
;
161 wxXmlNode
*m_parent
, *m_children
, *m_next
;
163 void DoCopy(const wxXmlNode
& node
);
172 // This class holds XML data/document as parsed by XML parser.
174 class WXDLLIMPEXP_XML wxXmlDocument
: public wxObject
178 wxXmlDocument(const wxString
& filename
,
179 const wxString
& encoding
= wxT("UTF-8"));
180 wxXmlDocument(wxInputStream
& stream
,
181 const wxString
& encoding
= wxT("UTF-8"));
182 virtual ~wxXmlDocument() { wxDELETE(m_root
); }
184 wxXmlDocument(const wxXmlDocument
& doc
);
185 wxXmlDocument
& operator=(const wxXmlDocument
& doc
);
187 // Parses .xml file and loads data. Returns TRUE on success, FALSE
189 virtual bool Load(const wxString
& filename
,
190 const wxString
& encoding
= wxT("UTF-8"));
191 virtual bool Load(wxInputStream
& stream
,
192 const wxString
& encoding
= wxT("UTF-8"));
194 // Saves document as .xml file.
195 virtual bool Save(const wxString
& filename
) const;
196 virtual bool Save(wxOutputStream
& stream
) const;
198 bool IsOk() const { return m_root
!= NULL
; }
200 // Returns root node of the document.
201 wxXmlNode
*GetRoot() const { return m_root
; }
203 // Returns version of document (may be empty).
204 wxString
GetVersion() const { return m_version
; }
205 // Returns encoding of document (may be empty).
206 // Note: this is the encoding original file was saved in, *not* the
207 // encoding of in-memory representation!
208 wxString
GetFileEncoding() const { return m_fileEncoding
; }
210 // Write-access methods:
211 void SetRoot(wxXmlNode
*node
) { wxDELETE(m_root
); m_root
= node
; }
212 void SetVersion(const wxString
& version
) { m_version
= version
; }
213 void SetFileEncoding(const wxString
& encoding
) { m_fileEncoding
= encoding
; }
216 // Returns encoding of in-memory representation of the document
217 // (same as passed to Load or ctor, defaults to UTF-8).
218 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
219 wxString
GetEncoding() const { return m_encoding
; }
220 void SetEncoding(const wxString
& enc
) { m_encoding
= enc
; }
225 wxString m_fileEncoding
;
231 void DoCopy(const wxXmlDocument
& doc
);
233 DECLARE_CLASS(wxXmlDocument
)