1. Load the file.
Tixmldocument Doc ("DEMO.XML");
Doc.loadFile ();
2.
Void main (void)
{
Tixmldocument Doc ("DATA.XML");
Bool loadokay = doc.loadfile ();
IF (loadokay)
{
Printf ("/ n% s: / n", pfilename);
DUMP_TO_STDOUT (& DOC); // Defined Later In The Tutorial
}
Else
{
Printf ("Failed to Load File /"% S / "/ N", PFileName);
}
Return;
}
EXAMPLE1.XML content If it is:
XML Version = "1.0"?>
The output is:
Document
Declaration
ELEMENT HELLO
Text [world]
3. Method of establishing a document.
void build ()
{Tixmldocument doc;
Tixmldeclaration * decl = New Tixmldeclaration ("1.0", "," ");
Tixmlelement * Element = New Tixmlelement ("Hello");
TixmlText * text = new tixmltext ("world");
Element-> Linkendchild (Text);
Doc.Linkendchild (DECL);
Doc.Linkendchild (Element);
Doc.savefile ("eXample1.xml");
}
4. Set node properties.
Tixmlelement Window = New Tixmlelement ("DEMO");
Window-> SetAttribute ("Name", "Circle");
Window-> SetAttribute ("X", 5);
Window-> SetAttribute ("Y", 15);
Window-> SetDoublettribute ("Radius", 3.14159);
5. Get all the properties of the element and print the attribute name and value
INT Printelement (Tixmlelement * Pelement, unsigned int indent)
{
IF (! pelement) Return 0;
Tixmlattribute * Pattribute (); int = 0; int = 0; inter = Double Dval; const char * pindent = getindent (indent); printf ("/ n"); while (PattriB) {printf ("% s % S: Value = [% s] ", Pindent, Pattrib-> Name (), Pattrib-> Value ());
IF (Pattrib-> QueryintValue (& IVAL) == Tixml_suCcess) Printf ("INT =% D", IVAL); if (Pattrib-> QuerydouBLEVALUE (& DVAL) == Tixml_suCcess) Printf ("D =% 1.1F", DVAL) PRINTF ("/ n"); i ; Pattrib = Pattrib-> Next ();} returni i;} 6. Write files, in fact, it has been used. Doc.savefile (SaveFileName); 7. Create a document its content: XML Version = "1.0"?>
Tixmlcomment * comment = new tixmlcomment (); // XML Note Comment-> setValue ("settings for myApp"); root-> linkendchild (comment); // Insert a root element Tixmlelement * msgs = new Tixmlelement ("Messages" ); Root-> linkendchild (msgs); // Define element Messages, insert to root msg = new Tixmlelement ("Welcome"); // Define new elements and insert into Msgs MSG-> Linkendchild (New TixmlText ("Welcome to MyApp ")); msgs-> linkendchild (msg); msg = new Tixmlelement (" FAREWELL "); // Defines new elements and inserts into MSGS MSG-> Linkendchild (New TixmlText (" Thank you for using myApp ")) ; msgs-> LinkEndChild (msg); TiXmlElement * windows = new TiXmlElement ( "Windows"); root-> LinkEndChild (windows); insert a new element windowsTiXmlElement * window // root in; window = new TiXmlElement ( "window"); Windows-> LinkendChild (Window); // Defines the new element and set its properties. Window-> SetAttribute ("Name", "MainFrame"); Window-> SetAttribute ("x", 5); Window-> SetAttribute ("Y", 15); Window-> SetAttribute ("W", 400); Window-> SetAttribute ("H", 250);
Tixmlelement * cxn = new tixmlelement ("connection"); root-> linkendchild (cxn); CXN-> SetAttribute ("IP", "192.168.0.1"); CXN-> SetDoublettribute ("Timeout", 123.456); // Floating point attrib dump_to_stdout (& doc); doc.savefile ("appsettings.xml"); // Save file} 8. The object to XML conversion. /// Class #include
Typedef st :: map
// a Basic window Abstract - Demo Purposes Only Class WindowSettings {Public: INT X, Y, W, H; String Name; WindowSettings (): x (0), Y (0), W (100), H (100) , Name ("Untitled") {}
WindowSettings (int X, int y, int w, int h, const string & name) {this-> x = x; this-> y = y; this-> w = W; this-> h = h; this-> Name = name;}};
Class ConnectionSettings {public: string ip; double timeout;
Class appsettings {public: string m_name; messagemap m_messages; list
Appsettings () {}
Void save (const char * pfilename); Void Load (const char * pfilename); // Just to show how to do it void setDemovAlues () {m_name = "myApp"; m_Messages.clear (); m_Messages ["Welcome"] = "Welcome to" m_name; m_Messages] = "Thank you for using" m_name; m_windows.clear (); m_windows.push_back (WindowSettings (15, 15, 400, 250, "main")))); m_connection.ip = "Unknown"; m_connection.timeout = 123.456;}}; /// creation file, and load int main (void) {appsettings settings; settings.save ("appsettings2.xml"); settings.load ("appsettings2.xml" Return 0;} // creation, modify, and save int main (void) {// block: Customise and save settings {appsettings settings; settings.m_name = "hitchhikerapp"; settings.m_messages ["Welcome"] = " Don't Panic "; settings.m_messages [" farewell "] =" Thanks for all the fish "; settings.m_windows.push_back (WindowSettings (15, 25, 300, 250," BookFrame "); settings.m_connection.ip =" 192.168. 0.77 "; settings.m_connection.timeout = 42.0;
Settings.save ("appsettings2.xml");} // block: load settings {appsettings settings; settings.load ("appsettings2.xml"); Printf ("% s:% s / n", settings.m_name.c_str (), Settings.m_messages ["Welcome"]. C_str ()); windowsettings & w = settings.m_windows.front (); printf ("% s: show window '% s' at% D,% D (% D) X% d) / n ", settings.m_name.c_str (), w.Name.c_str (), wx, wy, ww, wh); Printf ("% s:% s / n ", settings.m_name.c_str (), Settings.m_messages ["falwell"]. C_str ());} return 0;} Output: hitchhikerapp: don't panic hitchhikerapp: show window Window 'Bookframe' AT 15,25 (300 x 100) Hitchhikerapp: Thanks for All the fish 9.xml to the object's conversion. Void Appsettings :: Load (const char * pfilename) {Tixmldocument Doc (pfilename); if (! doc.loadfile ()) return; tixmlhandle hdoc (& doc); tixmlhalement * pelem; tixmlhandle hroot (0);
// block: name {pelem = hdoc.firstchildElement (). Element (); // shop always have a valid root but handle gracefully if it does if (! Pelem) return; m_name = pelem-> value ();
// save this for later hroot = tixmlhandle (pelem);
// block: string table {m_Messages.clear (); // trash existing table
Pelem = HROOT.FIRSTCHILD ("Messages") .firstchild (). Element (); for (pelem; pelem; pelem = pelem-> nextsiblingeElement ()) {const char * pKey = pelem-> value (); const char * PTEXT = pelem-> getText (); if (pkey && ptext) {m_messages [pkey] = ptext;}}}
// block: windows {m_windows.clear (); // trash existing list
TiXmlElement * pWindowNode = hRoot.FirstChild ( "Windows") .FirstChild () Element ();. For (pWindowNode; pWindowNode; pWindowNode = pWindowNode-> NextSiblingElement ()) {WindowSettings w; const char * pName = pWindowNode-> Attribute ( "name"); if (pname) w.Name = PNAME; PWindownode-> QueryintAttribute ("x", & w.x); // if this fails, Original Value IS LEFTAS PWINDOWNODE-> QueryintAttribute ("Y", & w.y); PWindownode-> QueryintAttribute ("W", & W.W); PWindownode-> QueryintAttribute ("HH", & W.H);
m_windows.push_back (w);}}
// block: Connection {pelem = hroot.firstchild ("Connection"). Element (); if (pelem) {m_connection.ip = pelem-> attribute ("ip"); pelem-> querydoublettribute ("timeout", & m_connection .timeout);}}}
10. A relatively complete example, load any XML document, and output on the console. // #include "stdafx.h" #include "tinyxml.h"
/ / -------------------------------------------------------------------------------------------- ---------------------- // stdout dump and indenting utility functions // ------------------ -------------------------------------------------- - const unsigned int num_indents_per_space = 2;
const char * getIndent (unsigned int numIndents) {static const char * pINDENT = " "; static const unsigned int LENGTH = strlen (pINDENT); unsigned int n = numIndents * NUM_INDENTS_PER_SPACE; if (n> LENGTH) n = LENGTH;
Return & Pind [Length-n];
// same as getIndent but no " " at the end const char * getIndentAlt (unsigned int numIndents) {static const char * pINDENT = ""; static const unsigned int LENGTH = strlen (pINDENT); unsigned int n = numIndents * NUM_INDENTS_PER_SPACE ; If (n> length) n = length; return & pindent [length-n];
INT DUMP_ATTRIBS_TO_STDOUT (Tixmlelement * Pelement, unsigned int inde "{if (! pelement) Return 0;
Tixmlattribute * Pattribute (); int = 0; int = 0; inter = Double Dval; const char * pindent = getindent (indent); printf ("/ n"); while (PattriB) {printf ("% s % S: Value = [% s] ", Pindent, Pattrib-> Name (), Pattrib-> Value ());
IF (Pattrib-> QueryintValue (& IVAL) == Tixml_suCcess) Printf ("INT =% D", IVAL); if (Pattrib-> QuerydouBLEVALUE (& DVAL) == Tixml_suCcess) Printf ("D =% 1.1F", DVAL) PRINTF ("/ n"); i ; Pattrib = Pattrib-> Next ();} Return i;}
Void dump_to_stddout (tixmlnode * pparent, unsigned int indeent = 0) {if (! pparent) return;
TixmlNode * PChild; TixmlText * Ptext; int T = pParent-> type (); Printf ("% s", getindent (indent); int Num;
Switch (T) {Case Tixmlnode :: Document: Printf ("Document"); Break;
Case Tixmlnode :: Element: Printf ("Element [% s]", pParent-> value ()); NUM = DUMP_ATTRIBS_TO_STDOUT (PParent-> TOELEMENT (), Indenter (NUM) {Case 0: Printf "(NO Attributes)"); Break; Case 1: Printf ("% s1 attribute", getIndentalt (indent)); Break; default: Printf ("% s% d attributes", getindentalt (indent), Num); Break } Break; Case Tixmlnode :: Comment: Printf ("Comment: [% s]", pParent-> value ();
Case Tixmlnode :: UNKNOWN: Printf ("unknown"); Break;
Case Tixmlnode :: text: ptext = pParent-> TOTEXT (); Printf ("text: [% s]", ptext-> value ()); Break;
Case Tixmlnode :: Declaration: Printf ("Declaration"); Break; Default: Break;} printf ("/ n"); for (pchild = pParent-> firstchild (); pchild! = 0; pchild = pchild-> Nextsibling ()) {DUMP_TO_STDOUT (PCHILD, INDENT 1);}}
// load the named file and dump its structure to STDOUT void dump_to_stdout (const char * pFilename) {TiXmlDocument doc (pFilename); bool loadOkay = doc.LoadFile (); if (loadOkay) {printf ( "/ n% s: / n ", pfilename); dump_to_stdout (& doc); // defined later in the tutorial} else {printf (" failed to load file / "% s /" / n ", pfilename);}}
/ / -------------------------------------------------------------------------------------------- ---------------------- // main () for printing files named on the command line // ------------- -------------------------------------------------- ----- Int main (int Argc, char * argv []) {for (int i = 1; i The above code is taken from the TinyXML example. In the actual use, there is a need to add a lot of your own code, such as a number of elements, etc.