简体   繁体   中英

XML comments being read as child nodes

I have some code which reads in an xml file as input to a computer model (written in C++ and compiled using VS2012). Following an update to the XML format of some of the input data, the software stopped reading in the file in its new format (the code had been changed to accomodate the new data format) and upon investigation it became clear that this was because the new data format included comments in the XML file which the xml parser (msxml6) was counting as child nodes. Consequently, the first child node it read it couldn't understand and the software exited as designed.

Although we have fixed the issue now (by removing the not strictly necessary comments from the XML file) I was wondering if anyone knew why the parser thought the comments were child nodes? I was under the impression comments were meant to be ignored by parsers?

In most tree models of XML (for example, DOM), comments are indeed child nodes of the containing element, and it is up to the application to make sure they are skipped when reading the tree.

It's a common mistake to forget this, and to imagine that when you test your application on source documents without comments, it will continue to work if the sender chooses to include comments.

Stripping comments from the document before passing it to your application is one way of solving the problem (it's a very simple XSLT transformation) but it would be better if the application were written to handle the comments in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM