Using XML files to store data
If i'm going to use a XML file to store some inf开发者_开发百科ormation, Am I going to need a XML Parser that read/write data? Can i just use string manipulation functions and why not?
You could conceivably use string manipulation functions, as that's what XML libraries end up using anyway. XML documents are just long strings in a special format. However, unless you know a lot about XML (and what is and isn't valid XML), using an XML parser/serializer now will save you a lot of trouble later on. There are nuances to XML (namespaces, escape sequences, etc) that will cause issues in homegrown code that doesn't know how to handle them properly. And by the time you've handled all the special cases, you'll effectively have written a half-assed XML parser anyway.
Do not... I repeat... Do not parse or build XML by hand.
At my work, we implement a lot of interfaces between different vendors. I can't tell you how many times we have been working on an xml based interface and have run into some kind of escape encoding / decoding issue. One of the first tests that I will run when I connect to an xml based service is to put illegal xml characters in the input.
What is your name?: bob<>&"
!ERROR PARSING XML DOCUMENT!
We have run into it so many times that if any of our engineers check anything akin to:
xml = "<rootnode>" xml += "<leafnode>" + someValue + "</leafnode>" xml += "</rootnode>"
we will seriously consider docking their variable pay. As soon as "someValue" contains an illegal character, you are pooched. Then your code starts looking like this:
xml = "<rootnode>" xml += "<leafnode>" + XmlEscape(someValue) + "</leafnode>" xml += "<leafnode>" + XmlEscape(someValue) + "</leafnode>" xml += "</rootnode>"
Then something is still blowing up, so the genius engineer tries:
xml = "<rootnode>" xml += "<leafnode>" + XmlEscape(someValue) + "</leafnode>" xml += "<leafnode>" + XmlEscape(someValue) + "</leafnode>" xml += "</rootnode>" xml = XmlEscape(xml)
Note: this process has actually happened to me twice while working on an interface.
Before you know it, your stream across the wire ends up looking like
&lt;rootnode>HELP<rootnode> &lt;rootnode>ME&lt;rootnode&gt;
SUMMARY:
Please use a library. k thx bye.
We can use tinyXml2 and make our job easier,just include header and cpp file of tinyxml2 and you are almost done parsing and writing .Please find the docs and manual at
http://www.grinninglizard.com/tinyxml2/
精彩评论