XML の書出しテスト – 5
MSXML.dll を利用しない方法を用意.
//#include “Xml_MS_.hxx”
#include “XmlOut.hxx”
#include “SvgOut.hxx”
class Xml_Attribute {
public:
// …
protected:
CString Name ;
CString Value ;
} ;
class XmlOut_E {
public:
// …
protected:
CString Name ;
CString Text ;
CArray<Xml_Attribute,Xml_Attribute> Attribute ;
protected:
CArray<XmlOut_E,XmlOut_E> Child ;
} ;
inline
BOOL XmlOut::Export (const XmlOut_E& elements,LPCTSTR svgName)
{
#ifdef UseMSXML
return Xml_MS::Export(elements,svgName) ;
#endif
CStringArray strAry ;
{
#ifdef _UNICODE
CString xml_ver = _T(“<?xml version=\”1.0\” encoding=\”utf-16\”?>”) ;
#else
CString xml_ver = _T(“<?xml version=\”1.0\” encoding=\”Shift-JIS\”?>”) ;
#endif
strAry.Add( xml_ver) ;
}
{
CStringArray sa ;
CreateElement(elements,&sa,0) ;
strAry.Append(sa) ;
}
return ::SaveText(svgName,strAry,TRUE) ;
}
inline
BOOL XmlOut::CreateElement (const XmlOut_E& elements,CStringArray* sa,const int indent)
{
CString tab = _T(“\t\t\t\t\t\t\t\t\t\t\t\t\t”) ;
CStringArray strAry ;
int nowIndent = indent ;
if (elements.IsComment()) {
sa->Add(tab.Left(indent) + _T(“<!–“)+elements.GetText()+_T(“–>”)) ;
return TRUE ;
}
CString eleStr ;
{
eleStr = tab.Left(indent) + _T(“<“) + elements.GetName() + _T(” “) ;
CString attStr ;
for (int aIndex=0 ; aIndex<elements.GetAttributeCount() ; aIndex++) {
Xml_Attribute a = elements.GetAttribute(aIndex) ;
CString name = a.GetName () ;
CString value= a.GetValue() ;
attStr += name + _T(“=\””) + value + _T(“\”\t”) ;
}
if (!attStr.IsEmpty()) {
eleStr += attStr ;
}
CString nameOrChild ;
if (!elements.GetText().IsEmpty()) {
nameOrChild = elements.GetText() ;
eleStr += _T(” >”) + nameOrChild ;
}
else {
CStringArray saChild ;
for (int cIndex=0 ; cIndex<elements.GetChildCount() ; cIndex++) {
XmlOut_E c = elements.GetChild(cIndex) ;
CreateElement(c,&saChild,indent+1) ;
}
if (saChild.GetSize() > 0) {
::StringArrayToString(saChild,nameOrChild) ;
eleStr += _T(” >\r\n”) + nameOrChild ;
}
}
if (nameOrChild.IsEmpty()) {
eleStr += _T(“/>”) ;
}
else {
if (!elements.GetText().IsEmpty()) {
eleStr += _T(“</”) + elements.GetName() + _T(“>”) ;
}
else {
eleStr += tab.Left(indent+1) + _T(“</”) + elements.GetName() + _T(“>”) ;
}
}
strAry.Add(eleStr) ;
}
return sa->Append(strAry) ;
}