|
#include "stdafx.h"
#include "MiniXML.h"
void ParseAndCloneTest()
{
//
// Create a CXMLConf object, Clone it to the element of pRoot, and output
//
CXmlConf xmlConf("sampleXML.xml");
if (xmlConf)
{
CElement*pRoot=xmlConf.Clone();
cout<<*pRoot;
pRoot->Delete();
}
else
{
cout<<"Operation of file \"sampleXML.xml\" failed!!!"<<endl;
}
}
void AttributeReadTest()
{
//
// Create a XMLConf object and read the attr value of 2nd
// Element1->SubElement1->SubElement2->SubElement2Sub. The right value should be
// "XiAn"
CXmlConf xmlConf("sampleXML.xml");
if (xmlConf)
{
CElementIterator iter(xmlConf.GetRootElement("Element1.SubElement1.SubElement2.SubElement2Sub"));
if (iter.IsValid())
{
++iter;
if (iter.IsValid())
{
vector<char> attribute;
if(iter.GetElementPtr()->GetAttribute("attr",attribute))
{
cout<<"\nAttribute of 2nd Element1.SubElement1.SubElement2.SubElement2Sub is "
<<attribute.begin()<<endl;
}
}
}
}
}
void ElementModifyTest()
{
//
// This example shows how to modify an attribute of
// 2nd Element1->SubElement1->SubElement2->SubElement2Sub from 'XiAn' to 'HuNan'
// and save the element to an validate file
CXmlConf xmlConf("sampleXML.xml");
if (xmlConf)
{
CElementIterator iter(xmlConf.GetRootElement("Element1.SubElement1.SubElement2.SubElement2Sub"));
if (iter.IsValid())
{
++iter;
if (iter.IsValid())
{
iter.GetElementPtr()->ModifyAttribute("attr","HuNan");
CElement*p=CElement::CreateNewElement("NewElement");
p->AddAttributePair("Attribute", "hahahaha");
p->AddAttributePair("Attribute2", "hahahaha2");
iter.GetElementPtr()->AddChildElement(p);
}
}
//
// Output the result to a file
//
ofstream ofs("OutputXML.xml");
ofs<<xmlConf;
}
}
int main(int argc, char* argv[])
{
//
// This function has to be called before CXmlConf constructor to
// initialize the CharType data.
//
CharTypeTable::InitCharType();
ParseAndCloneTest();
AttributeReadTest();
ElementModifyTest();
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Richard Lin is senior software engineer of in Silicon Valley.
Richard Lin was born in Beijing and came to US in the fall of 1995. He began his first software career in bay area of California in 1997. He has worked for many interesting projects including manufacturing testing systems, wireless AP firmware and applications, email anti-virus system and personal firewalls. He loves playing go (WeiQi in Chinese) and soccer in his spare time. He has a beautiful wife and a cute daughter and enjoys his life in San Jose of California.