今天下午写了个自动生成xml文件的demo.代码如下:
#include <fstream>
#include <string>
#include <vector>
#include <map>
using namespace std;
class XmlElement
{
public:
XmlElement();
void SetElementName(string& name);
void SetAttribute(const string& attributeName,const string& attributeValue);
void AddSubElement(const XmlElement* InElement);
void SetTextNode(string& InValue);
friend ofstream& operator << (ofstream& out,const XmlElement& InElem)
{
out.open("E:\\myXmlDemo.xml");
out << "<" << "?xml version=\"1.0\" encoding=\"UTF-8\"?" << ">" << endl;
InElem.WriteToXmlFile(out);
out.close();
return out;
}
void WriteToXmlFile(ofstream& outFile,int IndentLevel = 0) const;
void IndentXmlFile(ofstream& outFile,int IndentLevel) const;
protected:
string m_ElementName;
map<string,string> m_Attribute;
vector<const XmlElement* > m_XmlSubElement;
string m_TextNode;
};
XmlElement::XmlElement()
{
}
void XmlElement::SetElementName(string& name)
{
this->m_ElementName = name;
}
void XmlElement::SetAttribute(const string& attributeName,const string& attributeValue)
{
this->m_Attribute[attributeName] = attributeValue;
}
void XmlElement::AddSubElement(const XmlElement* InElement)
{
this->m_XmlSubElement.push_back(InElement);
}
void XmlElement::SetTextNode(string& InValue)
{
this->m_TextNode = InValue;
}
// output to xml file
void XmlElement::WriteToXmlFile(ofstream& outFile,int IndentLevel) const
{
IndentXmlFile(outFile,IndentLevel);
outFile << "<" << m_ElementName;
map<string,string>::const_iterator ptr;
for (ptr = m_Attribute.begin(); ptr != m_Attribute.end(); ++ptr)
{
outFile << " " << ptr->first << "=\"" << ptr->second << "\"";
}
outFile << ">";
if (m_TextNode != "")
{
outFile << m_TextNode;
}
else
{
outFile << endl;
vector<const XmlElement* >::const_iterator pos;
for ( pos = m_XmlSubElement.begin(); pos != m_XmlSubElement.end(); ++pos)
{
(*pos)->WriteToXmlFile(outFile,IndentLevel+1);
}
IndentXmlFile(outFile,IndentLevel);
}
outFile << "</" << m_ElementName << ">" << endl;
}
void XmlElement::IndentXmlFile(ofstream& outFile,int IndentLevel) const
{
for (int i=0; i < IndentLevel; ++i)
{
outFile << "\t";
}
}
int main()
{
string strWindow = "window";
string strEdit = "Edit";
string strButton = "Button";
string strAttributeName = "name";
string strAttributeValue = "author--->xiaohe"; // 我的名字
string strValueName = "Color";
string strValue = "#ffff";
string strButtonName = "text";
string strButtonValue = "button1";
string strColor = "color";
string strColorValue = "#0000";
string strTextNode1 = "hello,xml I am admiring you!";
string strTextNode2 = "hello,button layout~";
XmlElement WindowElement;
WindowElement.SetElementName(strWindow);
XmlElement Element_1;
Element_1.SetElementName(strEdit);
Element_1.SetAttribute(strAttributeName,strAttributeValue);
Element_1.SetAttribute(strValueName,strValue);
Element_1.SetTextNode(strTextNode1);
XmlElement Element_2;
Element_2.SetElementName(strButton);
Element_2.SetAttribute(strColor,strColorValue);
Element_2.SetAttribute(strButtonName,strButtonValue);
Element_2.SetTextNode(strTextNode2);
WindowElement.AddSubElement(&Element_1);
WindowElement.AddSubElement(&Element_2);
ofstream out;
out << WindowElement;
return 0;
}