1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xml/xml.cpp
3 // Purpose: wxXmlDocument - XML parser & data holder class
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 #include "wx/xml/xml.h"
28 #include "wx/wfstream.h"
29 #include "wx/datstrm.h"
30 #include "wx/zstream.h"
31 #include "wx/strconv.h"
32 #include "wx/scopedptr.h"
33 #include "wx/versioninfo.h"
35 #include "expat.h" // from Expat
37 // DLL options compatibility check:
38 WX_CHECK_BUILD_OPTIONS("wxXML")
41 IMPLEMENT_CLASS(wxXmlDocument
, wxObject
)
44 // a private utility used by wxXML
45 static bool wxIsWhiteOnly(const wxString
& buf
);
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 wxXmlNode::wxXmlNode(wxXmlNode
*parent
,wxXmlNodeType type
,
53 const wxString
& name
, const wxString
& content
,
54 wxXmlAttribute
*attrs
, wxXmlNode
*next
, int lineNo
)
55 : m_type(type
), m_name(name
), m_content(content
),
56 m_attrs(attrs
), m_parent(parent
),
57 m_children(NULL
), m_next(next
),
61 wxASSERT_MSG ( type
!= wxXML_ELEMENT_NODE
|| content
.empty(), "element nodes can't have content" );
65 if (m_parent
->m_children
)
67 m_next
= m_parent
->m_children
;
68 m_parent
->m_children
= this;
71 m_parent
->m_children
= this;
75 wxXmlNode::wxXmlNode(wxXmlNodeType type
, const wxString
& name
,
76 const wxString
& content
,
78 : m_type(type
), m_name(name
), m_content(content
),
79 m_attrs(NULL
), m_parent(NULL
),
80 m_children(NULL
), m_next(NULL
),
81 m_lineNo(lineNo
), m_noConversion(false)
83 wxASSERT_MSG ( type
!= wxXML_ELEMENT_NODE
|| content
.empty(), "element nodes can't have content" );
86 wxXmlNode::wxXmlNode(const wxXmlNode
& node
)
93 wxXmlNode::~wxXmlNode()
98 wxXmlNode
& wxXmlNode::operator=(const wxXmlNode
& node
)
109 void wxXmlNode::DoFree()
112 for (c
= m_children
; c
; c
= c2
)
118 wxXmlAttribute
*p
, *p2
;
119 for (p
= m_attrs
; p
; p
= p2
)
126 void wxXmlNode::DoCopy(const wxXmlNode
& node
)
128 m_type
= node
.m_type
;
129 m_name
= node
.m_name
;
130 m_content
= node
.m_content
;
131 m_lineNo
= node
.m_lineNo
;
132 m_noConversion
= node
.m_noConversion
;
135 wxXmlNode
*n
= node
.m_children
;
138 AddChild(new wxXmlNode(*n
));
143 wxXmlAttribute
*p
= node
.m_attrs
;
146 AddAttribute(p
->GetName(), p
->GetValue());
151 bool wxXmlNode::HasAttribute(const wxString
& attrName
) const
153 wxXmlAttribute
*attr
= GetAttributes();
157 if (attr
->GetName() == attrName
) return true;
158 attr
= attr
->GetNext();
164 bool wxXmlNode::GetAttribute(const wxString
& attrName
, wxString
*value
) const
166 wxCHECK_MSG( value
, false, "value argument must not be NULL" );
168 wxXmlAttribute
*attr
= GetAttributes();
172 if (attr
->GetName() == attrName
)
174 *value
= attr
->GetValue();
177 attr
= attr
->GetNext();
183 wxString
wxXmlNode::GetAttribute(const wxString
& attrName
, const wxString
& defaultVal
) const
186 if (GetAttribute(attrName
, &tmp
))
192 void wxXmlNode::AddChild(wxXmlNode
*child
)
194 if (m_children
== NULL
)
198 wxXmlNode
*ch
= m_children
;
199 while (ch
->m_next
) ch
= ch
->m_next
;
202 child
->m_next
= NULL
;
203 child
->m_parent
= this;
206 // inserts a new node in front of 'followingNode'
207 bool wxXmlNode::InsertChild(wxXmlNode
*child
, wxXmlNode
*followingNode
)
209 wxCHECK_MSG( child
, false, "cannot insert a NULL node!" );
210 wxCHECK_MSG( child
->m_parent
== NULL
, false, "node already has a parent" );
211 wxCHECK_MSG( child
->m_next
== NULL
, false, "node already has m_next" );
212 wxCHECK_MSG( followingNode
== NULL
|| followingNode
->GetParent() == this,
214 "wxXmlNode::InsertChild - followingNode has incorrect parent" );
216 // this is for backward compatibility, NULL was allowed here thanks to
217 // the confusion about followingNode's meaning
218 if ( followingNode
== NULL
)
219 followingNode
= m_children
;
221 if ( m_children
== followingNode
)
223 child
->m_next
= m_children
;
228 wxXmlNode
*ch
= m_children
;
229 while ( ch
&& ch
->m_next
!= followingNode
)
233 wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" );
237 child
->m_next
= followingNode
;
241 child
->m_parent
= this;
245 // inserts a new node right after 'precedingNode'
246 bool wxXmlNode::InsertChildAfter(wxXmlNode
*child
, wxXmlNode
*precedingNode
)
248 wxCHECK_MSG( child
, false, "cannot insert a NULL node!" );
249 wxCHECK_MSG( child
->m_parent
== NULL
, false, "node already has a parent" );
250 wxCHECK_MSG( child
->m_next
== NULL
, false, "node already has m_next" );
251 wxCHECK_MSG( precedingNode
== NULL
|| precedingNode
->m_parent
== this, false,
252 "precedingNode has wrong parent" );
256 child
->m_next
= precedingNode
->m_next
;
257 precedingNode
->m_next
= child
;
259 else // precedingNode == NULL
261 wxCHECK_MSG( m_children
== NULL
, false,
262 "NULL precedingNode only makes sense when there are no children" );
264 child
->m_next
= m_children
;
268 child
->m_parent
= this;
272 bool wxXmlNode::RemoveChild(wxXmlNode
*child
)
274 if (m_children
== NULL
)
276 else if (m_children
== child
)
278 m_children
= child
->m_next
;
279 child
->m_parent
= NULL
;
280 child
->m_next
= NULL
;
285 wxXmlNode
*ch
= m_children
;
288 if (ch
->m_next
== child
)
290 ch
->m_next
= child
->m_next
;
291 child
->m_parent
= NULL
;
292 child
->m_next
= NULL
;
301 void wxXmlNode::AddAttribute(const wxString
& name
, const wxString
& value
)
303 AddProperty(name
, value
);
306 void wxXmlNode::AddAttribute(wxXmlAttribute
*attr
)
311 bool wxXmlNode::DeleteAttribute(const wxString
& name
)
313 return DeleteProperty(name
);
316 void wxXmlNode::AddProperty(const wxString
& name
, const wxString
& value
)
318 AddProperty(new wxXmlAttribute(name
, value
, NULL
));
321 void wxXmlNode::AddProperty(wxXmlAttribute
*attr
)
327 wxXmlAttribute
*p
= m_attrs
;
328 while (p
->GetNext()) p
= p
->GetNext();
333 bool wxXmlNode::DeleteProperty(const wxString
& name
)
335 wxXmlAttribute
*attr
;
340 else if (m_attrs
->GetName() == name
)
343 m_attrs
= attr
->GetNext();
351 wxXmlAttribute
*p
= m_attrs
;
354 if (p
->GetNext()->GetName() == name
)
357 p
->SetNext(attr
->GetNext());
368 wxString
wxXmlNode::GetNodeContent() const
370 wxXmlNode
*n
= GetChildren();
374 if (n
->GetType() == wxXML_TEXT_NODE
||
375 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
376 return n
->GetContent();
379 return wxEmptyString
;
382 int wxXmlNode::GetDepth(wxXmlNode
*grandparent
) const
384 const wxXmlNode
*n
= this;
391 if (n
== grandparent
)
399 bool wxXmlNode::IsWhitespaceOnly() const
401 return wxIsWhiteOnly(m_content
);
406 //-----------------------------------------------------------------------------
408 //-----------------------------------------------------------------------------
410 wxXmlDocument::wxXmlDocument()
411 : m_version(wxS("1.0")), m_fileEncoding(wxS("UTF-8")), m_docNode(NULL
)
414 m_encoding
= wxS("UTF-8");
418 wxXmlDocument::wxXmlDocument(const wxString
& filename
, const wxString
& encoding
)
419 :wxObject(), m_docNode(NULL
)
421 if ( !Load(filename
, encoding
) )
427 wxXmlDocument::wxXmlDocument(wxInputStream
& stream
, const wxString
& encoding
)
428 :wxObject(), m_docNode(NULL
)
430 if ( !Load(stream
, encoding
) )
436 wxXmlDocument::wxXmlDocument(const wxXmlDocument
& doc
)
442 wxXmlDocument
& wxXmlDocument::operator=(const wxXmlDocument
& doc
)
449 void wxXmlDocument::DoCopy(const wxXmlDocument
& doc
)
451 m_version
= doc
.m_version
;
453 m_encoding
= doc
.m_encoding
;
455 m_fileEncoding
= doc
.m_fileEncoding
;
458 m_docNode
= new wxXmlNode(*doc
.m_docNode
);
463 bool wxXmlDocument::Load(const wxString
& filename
, const wxString
& encoding
, int flags
)
465 wxFileInputStream
stream(filename
);
468 return Load(stream
, encoding
, flags
);
471 bool wxXmlDocument::Save(const wxString
& filename
, int indentstep
) const
473 wxFileOutputStream
stream(filename
);
476 return Save(stream
, indentstep
);
479 wxXmlNode
*wxXmlDocument::GetRoot() const
481 wxXmlNode
*node
= m_docNode
;
484 node
= m_docNode
->GetChildren();
485 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
486 node
= node
->GetNext();
491 wxXmlNode
*wxXmlDocument::DetachRoot()
493 wxXmlNode
*node
= m_docNode
;
496 node
= m_docNode
->GetChildren();
497 wxXmlNode
*prev
= NULL
;
498 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
501 node
= node
->GetNext();
505 if (node
== m_docNode
->GetChildren())
506 m_docNode
->SetChildren(node
->GetNext());
509 prev
->SetNext(node
->GetNext());
511 node
->SetParent(NULL
);
518 void wxXmlDocument::SetRoot(wxXmlNode
*root
)
522 wxASSERT_MSG( root
->GetType() == wxXML_ELEMENT_NODE
,
523 "Can only set an element type node as root" );
526 wxXmlNode
*node
= m_docNode
;
529 node
= m_docNode
->GetChildren();
530 wxXmlNode
*prev
= NULL
;
531 while (node
!= NULL
&& node
->GetType() != wxXML_ELEMENT_NODE
)
534 node
= node
->GetNext();
538 root
->SetNext( node
->GetNext() );
544 m_docNode
->SetChildren(root
);
548 m_docNode
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
549 m_docNode
->SetChildren(root
);
552 root
->SetParent(m_docNode
);
555 void wxXmlDocument::AppendToProlog(wxXmlNode
*node
)
558 m_docNode
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
560 m_docNode
->InsertChild( node
, GetRoot() );
562 m_docNode
->AddChild( node
);
565 //-----------------------------------------------------------------------------
566 // wxXmlDocument loading routines
567 //-----------------------------------------------------------------------------
569 // converts Expat-produced string in UTF-8 into wxString using the specified
570 // conv or keep in UTF-8 if conv is NULL
571 static wxString
CharToString(wxMBConv
*conv
,
572 const char *s
, size_t len
= wxString::npos
)
577 // there can be no embedded NULs in this string so we don't need the
578 // output length, it will be NUL-terminated
579 const wxWCharBuffer
wbuf(
580 wxConvUTF8
.cMB2WC(s
, len
== wxString::npos
? wxNO_LEN
: len
, NULL
));
582 return wxString(wbuf
, *conv
);
584 // else: the string is wanted in UTF-8
585 #endif // !wxUSE_UNICODE
588 return wxString::FromUTF8Unchecked(s
, len
);
591 // returns true if the given string contains only whitespaces
592 bool wxIsWhiteOnly(const wxString
& buf
)
594 for ( wxString::const_iterator i
= buf
.begin(); i
!= buf
.end(); ++i
)
597 if ( c
!= wxS(' ') && c
!= wxS('\t') && c
!= wxS('\n') && c
!= wxS('\r'))
604 struct wxXmlParsingContext
606 wxXmlParsingContext()
611 removeWhiteOnlyNodes(false)
616 wxXmlNode
*node
; // the node being parsed
617 wxXmlNode
*lastChild
; // the last child of "node"
618 wxXmlNode
*lastAsText
; // the last _text_ child of "node"
621 bool removeWhiteOnlyNodes
;
624 // checks that ctx->lastChild is in consistent state
625 #define ASSERT_LAST_CHILD_OK(ctx) \
626 wxASSERT( ctx->lastChild == NULL || \
627 ctx->lastChild->GetNext() == NULL ); \
628 wxASSERT( ctx->lastChild == NULL || \
629 ctx->lastChild->GetParent() == ctx->node )
632 static void StartElementHnd(void *userData
, const char *name
, const char **atts
)
634 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
635 wxXmlNode
*node
= new wxXmlNode(wxXML_ELEMENT_NODE
,
636 CharToString(ctx
->conv
, name
),
638 XML_GetCurrentLineNumber(ctx
->parser
));
639 const char **a
= atts
;
641 // add node attributes
644 node
->AddAttribute(CharToString(ctx
->conv
, a
[0]), CharToString(ctx
->conv
, a
[1]));
648 ASSERT_LAST_CHILD_OK(ctx
);
649 ctx
->node
->InsertChildAfter(node
, ctx
->lastChild
);
650 ctx
->lastAsText
= NULL
;
651 ctx
->lastChild
= NULL
; // our new node "node" has no children yet
656 static void EndElementHnd(void *userData
, const char* WXUNUSED(name
))
658 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
660 // we're exiting the last children of ctx->node->GetParent() and going
661 // back one level up, so current value of ctx->node points to the last
662 // child of ctx->node->GetParent()
663 ctx
->lastChild
= ctx
->node
;
665 ctx
->node
= ctx
->node
->GetParent();
666 ctx
->lastAsText
= NULL
;
669 static void TextHnd(void *userData
, const char *s
, int len
)
671 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
672 wxString str
= CharToString(ctx
->conv
, s
, len
);
676 ctx
->lastAsText
->SetContent(ctx
->lastAsText
->GetContent() + str
);
680 bool whiteOnly
= false;
681 if (ctx
->removeWhiteOnlyNodes
)
682 whiteOnly
= wxIsWhiteOnly(str
);
686 wxXmlNode
*textnode
=
687 new wxXmlNode(wxXML_TEXT_NODE
, wxS("text"), str
,
688 XML_GetCurrentLineNumber(ctx
->parser
));
690 ASSERT_LAST_CHILD_OK(ctx
);
691 ctx
->node
->InsertChildAfter(textnode
, ctx
->lastChild
);
692 ctx
->lastChild
= ctx
->lastAsText
= textnode
;
697 static void StartCdataHnd(void *userData
)
699 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
701 wxXmlNode
*textnode
=
702 new wxXmlNode(wxXML_CDATA_SECTION_NODE
, wxS("cdata"), wxS(""),
703 XML_GetCurrentLineNumber(ctx
->parser
));
705 ASSERT_LAST_CHILD_OK(ctx
);
706 ctx
->node
->InsertChildAfter(textnode
, ctx
->lastChild
);
707 ctx
->lastChild
= ctx
->lastAsText
= textnode
;
710 static void EndCdataHnd(void *userData
)
712 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
714 // we need to reset this pointer so that subsequent text nodes don't append
715 // their contents to this one but create new wxXML_TEXT_NODE objects (or
716 // not create anything at all if only white space follows the CDATA section
717 // and wxXMLDOC_KEEP_WHITESPACE_NODES is not used as is commonly the case)
718 ctx
->lastAsText
= NULL
;
721 static void CommentHnd(void *userData
, const char *data
)
723 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
725 wxXmlNode
*commentnode
=
726 new wxXmlNode(wxXML_COMMENT_NODE
,
727 wxS("comment"), CharToString(ctx
->conv
, data
),
728 XML_GetCurrentLineNumber(ctx
->parser
));
730 ASSERT_LAST_CHILD_OK(ctx
);
731 ctx
->node
->InsertChildAfter(commentnode
, ctx
->lastChild
);
732 ctx
->lastChild
= commentnode
;
733 ctx
->lastAsText
= NULL
;
736 static void PIHnd(void *userData
, const char *target
, const char *data
)
738 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
741 new wxXmlNode(wxXML_PI_NODE
, CharToString(ctx
->conv
, target
),
742 CharToString(ctx
->conv
, data
),
743 XML_GetCurrentLineNumber(ctx
->parser
));
745 ASSERT_LAST_CHILD_OK(ctx
);
746 ctx
->node
->InsertChildAfter(pinode
, ctx
->lastChild
);
747 ctx
->lastChild
= pinode
;
748 ctx
->lastAsText
= NULL
;
751 static void DefaultHnd(void *userData
, const char *s
, int len
)
754 if (len
> 6 && memcmp(s
, "<?xml ", 6) == 0)
756 wxXmlParsingContext
*ctx
= (wxXmlParsingContext
*)userData
;
758 wxString buf
= CharToString(ctx
->conv
, s
, (size_t)len
);
760 pos
= buf
.Find(wxS("encoding="));
761 if (pos
!= wxNOT_FOUND
)
762 ctx
->encoding
= buf
.Mid(pos
+ 10).BeforeFirst(buf
[(size_t)pos
+9]);
763 pos
= buf
.Find(wxS("version="));
764 if (pos
!= wxNOT_FOUND
)
765 ctx
->version
= buf
.Mid(pos
+ 9).BeforeFirst(buf
[(size_t)pos
+8]);
769 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData
),
770 const XML_Char
*name
, XML_Encoding
*info
)
772 // We must build conversion table for expat. The easiest way to do so
773 // is to let wxCSConv convert as string containing all characters to
774 // wide character representation:
782 for (i
= 0; i
< 255; i
++)
784 mbBuf
[0] = (char)(i
+1);
785 if (conv
.MB2WC(wcBuf
, mbBuf
, 2) == (size_t)-1)
787 // invalid/undefined byte in the encoding:
790 info
->map
[i
+1] = (int)wcBuf
[0];
794 info
->convert
= NULL
;
795 info
->release
= NULL
;
802 bool wxXmlDocument::Load(wxInputStream
& stream
, const wxString
& encoding
, int flags
)
807 m_encoding
= encoding
;
810 const size_t BUFSIZE
= 1024;
812 wxXmlParsingContext ctx
;
814 XML_Parser parser
= XML_ParserCreate(NULL
);
815 wxXmlNode
*root
= new wxXmlNode(wxXML_DOCUMENT_NODE
, wxEmptyString
);
817 ctx
.encoding
= wxS("UTF-8"); // default in absence of encoding=""
820 if ( encoding
.CmpNoCase(wxS("UTF-8")) != 0 )
821 ctx
.conv
= new wxCSConv(encoding
);
823 ctx
.removeWhiteOnlyNodes
= (flags
& wxXMLDOC_KEEP_WHITESPACE_NODES
) == 0;
827 XML_SetUserData(parser
, (void*)&ctx
);
828 XML_SetElementHandler(parser
, StartElementHnd
, EndElementHnd
);
829 XML_SetCharacterDataHandler(parser
, TextHnd
);
830 XML_SetCdataSectionHandler(parser
, StartCdataHnd
, EndCdataHnd
);;
831 XML_SetCommentHandler(parser
, CommentHnd
);
832 XML_SetProcessingInstructionHandler(parser
, PIHnd
);
833 XML_SetDefaultHandler(parser
, DefaultHnd
);
834 XML_SetUnknownEncodingHandler(parser
, UnknownEncodingHnd
, NULL
);
839 size_t len
= stream
.Read(buf
, BUFSIZE
).LastRead();
840 done
= (len
< BUFSIZE
);
841 if (!XML_Parse(parser
, buf
, len
, done
))
843 wxString
error(XML_ErrorString(XML_GetErrorCode(parser
)),
845 wxLogError(_("XML parsing error: '%s' at line %d"),
847 (int)XML_GetCurrentLineNumber(parser
));
855 if (!ctx
.version
.empty())
856 SetVersion(ctx
.version
);
857 if (!ctx
.encoding
.empty())
858 SetFileEncoding(ctx
.encoding
);
859 SetDocumentNode(root
);
866 XML_ParserFree(parser
);
878 //-----------------------------------------------------------------------------
879 // wxXmlDocument saving routines
880 //-----------------------------------------------------------------------------
882 // helpers for XML generation
886 // write string to output:
887 bool OutputString(wxOutputStream
& stream
,
896 wxUnusedVar(convMem
);
898 convFile
= &wxConvUTF8
;
900 const wxScopedCharBuffer
buf(str
.mb_str(*convFile
));
903 // conversion failed, can't write this string in an XML file in this
904 // (presumably non-UTF-8) encoding
908 stream
.Write(buf
, buf
.length());
909 #else // !wxUSE_UNICODE
910 if ( convFile
&& convMem
)
912 wxString
str2(str
.wc_str(*convMem
), *convFile
);
913 stream
.Write(str2
.mb_str(), str2
.length());
915 else // no conversions to do
917 stream
.Write(str
.mb_str(), str
.length());
919 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
921 return stream
.IsOk();
930 // Same as above, but create entities first.
931 // Translates '<' to "<", '>' to ">" and so on, according to the spec:
932 // http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping
933 bool OutputEscapedString(wxOutputStream
& stream
,
940 escaped
.reserve(str
.length());
942 for ( wxString::const_iterator i
= str
.begin(); i
!= str
.end(); ++i
)
949 escaped
.append(wxS("<"));
952 escaped
.append(wxS(">"));
955 escaped
.append(wxS("&"));
958 escaped
.append(wxS("
"));
961 if ( mode
== Escape_Attribute
)
966 escaped
.append(wxS("""));
969 escaped
.append(wxS("	"));
972 escaped
.append(wxS("
"));
986 return OutputString(stream
, escaped
, convMem
, convFile
);
989 bool OutputIndentation(wxOutputStream
& stream
,
994 wxString
str(wxS("\n"));
995 str
+= wxString(indent
, wxS(' '));
996 return OutputString(stream
, str
, convMem
, convFile
);
999 bool OutputNode(wxOutputStream
& stream
,
1007 switch (node
->GetType())
1009 case wxXML_CDATA_SECTION_NODE
:
1010 rc
= OutputString(stream
, wxS("<![CDATA["), convMem
, convFile
) &&
1011 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1012 OutputString(stream
, wxS("]]>"), convMem
, convFile
);
1015 case wxXML_TEXT_NODE
:
1016 if (node
->GetNoConversion())
1018 stream
.Write(node
->GetContent().c_str(), node
->GetContent().Length());
1022 rc
= OutputEscapedString(stream
, node
->GetContent(),
1027 case wxXML_ELEMENT_NODE
:
1028 rc
= OutputString(stream
, wxS("<"), convMem
, convFile
) &&
1029 OutputString(stream
, node
->GetName(), convMem
, convFile
);
1033 for ( wxXmlAttribute
*attr
= node
->GetAttributes();
1035 attr
= attr
->GetNext() )
1037 rc
= OutputString(stream
,
1038 wxS(" ") + attr
->GetName() + wxS("=\""),
1039 convMem
, convFile
) &&
1040 OutputEscapedString(stream
, attr
->GetValue(),
1042 Escape_Attribute
) &&
1043 OutputString(stream
, wxS("\""), convMem
, convFile
);
1047 if ( node
->GetChildren() )
1049 rc
= OutputString(stream
, wxS(">"), convMem
, convFile
);
1051 wxXmlNode
*prev
= NULL
;
1052 for ( wxXmlNode
*n
= node
->GetChildren();
1056 if ( indentstep
>= 0 && n
->GetType() != wxXML_TEXT_NODE
)
1058 rc
= OutputIndentation(stream
, indent
+ indentstep
,
1063 rc
= OutputNode(stream
, n
, indent
+ indentstep
,
1064 convMem
, convFile
, indentstep
);
1069 if ( rc
&& indentstep
>= 0 &&
1070 prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
1072 rc
= OutputIndentation(stream
, indent
, convMem
, convFile
);
1077 rc
= OutputString(stream
, wxS("</"), convMem
, convFile
) &&
1078 OutputString(stream
, node
->GetName(),
1079 convMem
, convFile
) &&
1080 OutputString(stream
, wxS(">"), convMem
, convFile
);
1083 else // no children, output "<foo/>"
1085 rc
= OutputString(stream
, wxS("/>"), convMem
, convFile
);
1089 case wxXML_COMMENT_NODE
:
1090 rc
= OutputString(stream
, wxS("<!--"), convMem
, convFile
) &&
1091 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1092 OutputString(stream
, wxS("-->"), convMem
, convFile
);
1096 rc
= OutputString(stream
, wxT("<?"), convMem
, convFile
) &&
1097 OutputString(stream
, node
->GetName(), convMem
, convFile
) &&
1098 OutputString(stream
, wxT(" "), convMem
, convFile
) &&
1099 OutputString(stream
, node
->GetContent(), convMem
, convFile
) &&
1100 OutputString(stream
, wxT("?>"), convMem
, convFile
);
1104 wxFAIL_MSG("unsupported node type");
1111 } // anonymous namespace
1113 bool wxXmlDocument::Save(wxOutputStream
& stream
, int indentstep
) const
1118 wxScopedPtr
<wxMBConv
> convMem
, convFile
;
1121 convFile
.reset(new wxCSConv(GetFileEncoding()));
1123 if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 )
1125 convFile
.reset(new wxCSConv(GetFileEncoding()));
1126 convMem
.reset(new wxCSConv(GetEncoding()));
1128 //else: file and in-memory encodings are the same, no conversion needed
1131 wxString dec
= wxString::Format(
1132 wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
1133 GetVersion(), GetFileEncoding()
1135 bool rc
= OutputString(stream
, dec
, convMem
.get(), convFile
.get());
1137 wxXmlNode
*node
= GetDocumentNode();
1139 node
= node
->GetChildren();
1143 rc
= OutputNode(stream
, node
, 0, convMem
.get(),
1144 convFile
.get(), indentstep
) &&
1145 OutputString(stream
, wxS("\n"), convMem
.get(), convFile
.get());
1146 node
= node
->GetNext();
1151 /*static*/ wxVersionInfo
wxXmlDocument::GetLibraryVersionInfo()
1153 return wxVersionInfo("expat",