I am currently in a task to generate an XML file for an srt text file containing timestamps and corresponding text. To generate an exe file which accepts file name input and outputs the relevant XML file to be used as part of an automated script.
Is it Advisable to use Tinyxml for this?
Edit:
your comments regarding this are very much appreciated what's the easiest way to generate xml in c++?
-
1May I recommend TiCpp: code.google.com/p/ticpp This is a C++ interface over Tiny Xml. The iterator style is a bit weird, but it is much more natural for C++ programming.Matthieu M.– Matthieu M.2011年03月20日 19:51:21 +00:00Commented Mar 20, 2011 at 19:51
3 Answers 3
Generating an XML file is a different task to consuming an XML file. If you're just generating an XML file in a specific format with fairly simple inputs, then it's not too much work to just do something like:
mystream << "<xmldata>" << endl;
mystream << "<something>Blah</something>" << endl;
mystream << "</xmldata>" << endl;
That's probably going to be much simpler than building a TinyXML object model, populating it and then writing it out.
The only thing you need to worry about is proper escaping (i.e. turning "&" into "&", "<" into "<" and so on).
Now, if you were parsing an XML file, then I would definitely recommend using a third-party library (and TinyXML is a good one if you don't need XSLT or schema validation, etc).
-
While it's easier, I'd still recommend using a proper XML library just so you don't need to worry about escaping things. If you get it wrong, people will have issues trying to consume your XML somewhere down the track when some edge case crops up.Matthew Scharley– Matthew Scharley2011年03月14日 06:15:41 +00:00Commented Mar 14, 2011 at 6:15
-
@Matthew would saving the file in correct encoding be an issue? does encoding differ in Linux environmentAditya P– Aditya P2011年03月14日 06:24:01 +00:00Commented Mar 14, 2011 at 6:24
-
@AdityaGameProgrammer I'm not a C++ programmer so I can't answer that one for you sorry. That said, encodings are another tricky issue, one that should be transparent and not an issue if you use a proper library.Matthew Scharley– Matthew Scharley2011年03月14日 06:26:37 +00:00Commented Mar 14, 2011 at 6:26
-
1@AdityaGameProgrammer: Line endings work just like any other text file on Windows vs. Linux. Encodings like UTF-8 vs. ISO-8859-1 or whatever are handled similarly, though I would suggest there's little reason to encode XML files in anything but UTF-8.Dean Harding– Dean Harding2011年03月14日 08:35:44 +00:00Commented Mar 14, 2011 at 8:35
-
@Dean Harding: I would suggest writing a small framework around it still. Just to make sure that escaping is done correctly and tags get closed properly... Might as well use a simple library at that point ? Oh and by the way, don't use
endl
all over the place.Matthieu M.– Matthieu M.2011年03月20日 19:54:17 +00:00Commented Mar 20, 2011 at 19:54
I see no reason to reinvent the wheel when you just can use a library.
The little performance boost you may gain by writing your own parsing code is nothing compared to the overhead of rolling your own vs. using a library.
Anyway, what you describe is not parsing. It's generating an XML file. At which point, using a XML library could be overkill.
You can also use this xml library.
To answer your question : don't waste your time on something that is already done and works fine.
Explore related questions
See similar questions with these tags.