-------------------------------------------XMLElement.h--------------------------------------------------
#include
#include
#include
#include
using namespace std;
class XMLElement
{
public:
XMLElement();
void setElementName(const string &inName);
void setAttribute(const string &inAttributeName,const string &inAttributeva lue);
void addSubElement(const XMLElement *inElement);
void setTextNode(const string &inValue);
string getMyInfo() const;
string getMySubElemInfo() const;
friend ostream& operator<<(ostream &outStream,const XMLElement &inElem);
protected:
void writeToStream(ostream& outStream,int inIndentLevel=0) const;
void indentStream(ostream& outStream,int inIndentLevel) const;
private:
string mElementName;
map
vector
string mTextNode;
};
---------------------------------------------XMLElement.cpp----------------------------------------------
#include "stdafx.h"
#include "XMLElement.h"
using namespace std;
XMLElement::XMLElement():mElementName("unnamed")
{
}
void XMLElement::setElementName(const std::string &inName)
{
mElementName = inName;
}
void XMLElement::setAttribute(const std::string &inAttributeName, const std::string &inAttributeva lue)
{
mAttributes[inAttributeName] = inAttributeva lue;
}
void XMLElement::addSubElement(const XMLElement *inElement)
{
mSubElements.push_back(inElement);
}
void XMLElement::setTextNode(const std::string &inValue)
{
mTextNode = inValue;
}
string XMLElement::getMyInfo() const
{
string info = mElementName + " " ;
for(map
info += it1->first +" " + it1->second +" ";
info += mTextNode;
return info;
}
string XMLElement::getMySubElemInfo() const
{
string subInfo;
for(vector< const XMLElement* >::const_iterator it2 = mSubElements.begin(); it2 != mSubElements.end(); ++it2)
{
subInfo += (*it2)->getMyInfo() + "\n";
}
return subInfo;
}
ostream& operator<<(ostream& outStream,const XMLElement& inElem)
{
inElem.writeToStream(outStream);
return (outStream);
}
void XMLElement::writeToStream(std::ostream &outStream, int inIndentLevel) const
{
indentStream(outStream,inIndentLevel);
outStream<<"<"< for(map { outStream<<" "< } outStream<<">"; if(mTextNode !="") outStream< else { outStream< for(vector< const XMLElement* >::const_iterator it2 = mSubElements.begin(); it2 != mSubElements.end(); ++it2) (*it2)->writeToStream(outStream,inIndentLevel+1); //(*it2)->writeToStream(outStream,inIndentLevel+1); indentStream(outStream,inIndentLevel); } outStream<<""< } void XMLElement::indentStream(std::ostream &outStream, int inIndentLevel) const { for(int i=0;i outStream<< '\t'; } -------------------------------------------------主函数.cpp-------------------------------------------------- // C++andXML.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include //#include #include "XMLElement.h" //using namespace std; int _tmain(int argc, _TCHAR* argv[]) { XMLElement myXMLElement; myXMLElement.setElementName("我的通讯录"); cout<<"请输入自己的名字:"; string myName; getline(cin,myName); cout<<"请输入自己的电话号码:"; string myPhone; getline(cin,myPhone); myXMLElement.setAttribute(myNa