Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

added examples and cmake compilation #13

Closed
wants to merge 3 commits into from

3 participants

@juan-cardelino

added a folder with simple and clean examples which are built with cmake
They should be easy to follow than the big and messy xmltest.cpp, at least for beginners like me.
I hope you like it.
Thanks for the great library.
Best regards,
Juan

Juan Cardelino added some commits
@leethomason
Owner

What I like about this:

  • it splits the examples into separate files. This is useful for adding doxygen documentation, which would be very useful. (The current docs in the readme.txt are hard to follow.)

Neither good nor bad:

  • having cmake in the examples makes it reasonable to add cmake support for the test file.

What I don't like:

  • having examples separate from the test file. It means building multiple binaries, which I'm trying very hard to avoid. There is elegance and simplicity to having one test program, without defines, that fully reflects the functionality of tinyxml-2.

One approach is to have the examples be functions instead of executables, and have the test program call them. (The return code would indicate success or failure.)

@juan-cardelino

From a newcomer point of view, is very good to have clearly separated examples. The test file is scary at a first glance, and the purpose of the tests is harder to understand.
In addition, the purpose of a test is not the same as the purpose of an example. So it might be reasonable to have only one example file and one test file. That could be a good compromise between clarity and the increase in files.
If I get some time in the near future, I will make one big example file.
But we better agree beforehand, so I don't waste time on that.
Thanks for your answer.
Best regards,
Juan

@leethomason
Owner

Juan, I wasn't suggesting one big example file either. I agree that is confusing, and doxygen (the docs generation system) won't even accept it. I was suggesting that each example file contain a function that is called by the main test program. Generally I don't think running the examples on their own is the key value, so much as having clear examples.

I'm a few pull requests behind: handles, figuring out cmake integration (or not), and the examples.So there is certainly time to iterate. :)

@juan-cardelino

I think I got the point now. We can turn each main into a function, and call them from the main test file. However, I don't see that having the examples in a separate file (or fies) has its own value.
I can also add the four examples as functions into the main xmltest.cpp file.
If you want, we can try to make a proof of concept in one example file and then merge it with the xmltest.cpp.
Let me know.

@leethomason
Owner

Agreed on separate files not really adding value - in fact, causes negative value because it complicates the build, which is silly for something as simple as TinyXML-2.

How about this: there are examples in the code now, but they are part of main(). I will:

  • break them out into separate functions
  • bang on doxygen until I can generate docs for them

And then we can collaboratively make some nice examples. Give me a few days - as I said, I have some other issues to work through - and I'll check in a basic framework.

Because I strongly agree getting some nice examples in there would help a lot.

Juan Cardelino example loop 9764ba3
@leethomason
Owner

I've banged on getting everything working together:

  • putting examples in xmltest.cpp (good suggestion, Juan)
  • doxygen integration, so it shows up in HTML (which helps reading)
  • examples called by the test program (which means that they won't get broken)

Once I clean up the existing examples, I'll check it in, and we can iterate on the examples themselves.

@leethomason
Owner

I've checked the examples, and docs for the examples, into the master. I'm closing this pull request; with the new example structure, it will no longer merge in. I cleaned up the existing examples as well. Juan, take a look at the new approach, and we can merge in new examples as it makes sense.

Note that you need 'doxygen' to build then, and 'doxygen dox' is the command line.

@leethomason leethomason closed this
@Groovounet

One advantage of multiple files is when using CTest (part of CMake) to use the examples as unit tests.
The log would report the success of each individual separate programs. With a single program, this would produce a binary result which is already something but...

With multiple programs, CTest will directly help to locate a problem.

Also, you might think "noooo! it's going to make maintaining the solution really complicate". Not at all with CMake!

Basically, I can write the CMakeFiles.txt so that adding a examples/tests is only adding a single line in the CMakeFiles.txt, something like:

addExamples(my_example.cpp)
End of story: Support GCC/Visual C++/XCode automagically.

Then if a compiler option is required for testing, say -msse2, it can be set once and will be share to all the samples.

@leethomason
Owner

Groovounet -
I like the vision of CMake - and I'm glad to include it - but I do want to keep it simple. I'm not generating the Visual Studio or XCode files, and don't even have test environment for XCode. It's best to stick with a simple one-file test. (xmltest.cpp)

@Groovounet

Erm. Once again, in time, I think you will change your mind and realise that you are better of having a XCode and a Visual Studio project :p

This say, I can send you pull request adding the CMake features I said without any need to modify the code and when you will change your mind, it will be already there waiting for you :D

@Groovounet

And I haven't mention yet CDash to ensure nightly build to TinyXML 2 ... XD

@juan-cardelino

I couldn't agree more. Lee is reinventing the weel (unit testing). However, sometimes the best way to keep things controlled is to reinvent the wheel and use specific solutions. I've seen many projects dying of excess of generality due to the complexity they add.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 31, 2012
  1. added simple examples to get started

    Juan Cardelino authored
  2. clean up and ordering of the examples

    Juan Cardelino authored
Commits on Apr 3, 2012
  1. example loop

    Juan Cardelino authored
This page is out of date. Refresh to see the latest.
View
1  examples/.gitignore
@@ -0,0 +1 @@
+.DS_Store
View
11 examples/CMakeLists.txt
@@ -0,0 +1,11 @@
+project(abc)
+
+cmake_minimum_required(VERSION 2.6)
+
+include_directories(..)
+
+SET(EXE_NAME example1 example2 example3 example4)
+
+foreach (exe ${EXE_NAME})
+ add_executable(${exe} ${exe}.cpp ../tinyxml2.cpp)
+endforeach()
View
20 examples/example1.cpp
@@ -0,0 +1,20 @@
+#include "tinyxml2.h"
+#include <cstdio>
+
+using namespace tinyxml2;
+
+
+int main(void)
+{
+ XMLDocument doc;
+ doc.LoadFile( "../dream.xml" );
+ const char* title = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->GetText();
+ printf( "Name of play (1): %s\n", title );
+
+ // Text is just another Node to TinyXML-2. The more
+ // general way to get to the XMLText:
+ XMLText* textNode = doc.FirstChildElement( "PLAY" )->FirstChildElement( "TITLE" )->FirstChild()->ToText();
+ title = textNode->Value();
+ printf( "Name of play (2): %s\n", title );
+ return 0;
+}
View
15 examples/example2.cpp
@@ -0,0 +1,15 @@
+#include "tinyxml2.h"
+
+using namespace tinyxml2;
+
+int main (int argc, char * const argv[])
+{
+ XMLDocument doc;
+ if (doc.LoadFile("input_example_02.xml") == XML_SUCCESS)
+ {
+ XMLNode *node = doc.NewElement("foo");
+ doc.InsertEndChild(node);
+ doc.SaveFile("output_example_02.xml");
+ }
+ return 0;
+}
View
32 examples/example3.cpp
@@ -0,0 +1,32 @@
+#include "tinyxml2.h"
+#include <cstdio>
+
+using namespace tinyxml2;
+
+int main (int argc, char * const argv[])
+{
+ XMLDocument doc;
+
+ if (doc.LoadFile("input_example_03.xml") == XML_SUCCESS)
+ {
+ //search for a node named Hello
+ XMLNode *node = doc.FirstChildElement("Hello");
+ //read the text content of it and print it to stdout
+ XMLText* textNode = node->FirstChild()->ToText();
+ const char* title = textNode->Value();
+ printf( "Contents of Hello: %s\n", title );
+
+ //create a new node named foo
+ XMLNode *new_node = doc.NewElement("foo");
+ //add foo as child of Hello
+ node->InsertEndChild(new_node);
+ //add some text content to the "foo" node
+ new_node->InsertFirstChild(doc.NewText("bar"));
+
+ //save the output document
+ doc.SaveFile("output_example_03.xml");
+
+ }
+
+ return 0;
+}
View
26 examples/example4.cpp
@@ -0,0 +1,26 @@
+#include "tinyxml2.h"
+#include <cstdio>
+
+using namespace tinyxml2;
+
+int main (int argc, char * const argv[])
+{
+ XMLDocument doc;
+
+ if (doc.LoadFile(argv[1]) == XML_SUCCESS)
+ {
+ //search for a node named Hello
+ XMLElement *node = doc.RootElement();
+
+
+ for(XMLElement* current_node=node->FirstChildElement("type");current_node!=NULL;current_node=current_node->NextSiblingElement("type"))
+ {
+ const char* at = current_node->Attribute("id");
+ printf("id: %s\n",at);
+ }
+
+
+ }
+
+ return 0;
+}
View
4,546 examples/input_example_01.xml
4,546 additions, 0 deletions not shown
View
2  examples/input_example_02.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" ?>
+<Hello>World</Hello>
View
3  examples/input_example_03.xml
@@ -0,0 +1,3 @@
+<?xml version="1.0" ?>
+<Hello>World</Hello>
+
Something went wrong with that request. Please try again.