TinyXML 无法读取自己的文件
我目前正在 Visual Studio 2003 上开发一个 C++ MFC 项目。该项目的目的是能够获取包含语言数据的 XML 文件,将其转换为简单的 CSV 文件,然后再转换回 XML。为此,我使用了 TinyXML 库,它很简单并且足以满足我的需求。
问题是,一旦我尝试将 .CSV 文件转换为 XML,创建的 XML 就无法再次使用 TinyXML 打开。从我现在看到的来看,它似乎无法打开刚刚创建的文件。
经过一些测试,我已经能够让 TinyXML 告诉我它的错误:“错误#9:无法读取结束标记”。如果我绕过该错误,则该文件可读到 DOM 树无缘无故向右缩进的程度,然后,稍后,该文件似乎没有任何元素。
这是一个由 TinyXML 创建后读取的错误文件。
<app>
−
<global>
<langidglobal1 lang="fr">GLOBAL1</langidglobal1>
<langidglobal1 lang="en">GLOBAL1</langidglobal1>
<langidglobal1 lang="ru"/>
<langidglobal1 lang="de"/>
</global>
−
<mainmenu>
<system lang="fr">Syst�me</system>
<system lang="en">System</system>
<system lang="ru"/>
<system lang="de"/>
<motor lang="fr">Moteurs</motor>
<motor lang="en">Motors</motor>
<motor lang="ru"/>
<motor lang="de"/>
<param lang="fr">Param�tres</param>
<param lang="en">Parameters</param>
<param lang="ru"/>
<param lang="de"/>
<diag lang="fr">Diagnostique</diag>
<diag lang="en">Diagnostic</diag>
<diag lang="ru"/>
<diag lang="de"/>
<rod lang="fr">Tiges</rod>
<rod lang="en">Rods</rod>
<rod lang="ru"/>
<rod lang="de"/>
<aide lang="fr">Aide</aide>
<aide lang="en">Help</aide>
<aide lang="ru"/>
<aide lang="de"/>
<exit lang="fr">Quitter</exit>
<exit lang="en">Exit</exit>
<exit lang="ru"/>
<exit lang="de"/>
<runningtools lang="fr">Outils</runningtools>
<runningtools lang="en">Running Tools</runningtools>
<runningtools lang="ru"/>
<runningtools lang="de"/>
<manualpatterns lang="fr">Patron Manuel</manualpatterns>
<manualpatterns lang="en">Manual Patterns</manualpatterns>
<manualpatterns lang="ru"/>
<manualpatterns lang="de"/>
<alarm lang="fr">Alarmes Actives</alarm>
<alarm lang="en">Active Alarms</alarm>
<alarm lang="ru"/>
<alarm lang="de"/>
<patternno lang="fr"># Patron</patternno>
<patternno lang="en">Pattern #</patternno>
<patternno lang="ru"/>
<patternno lang="de"/>
<lastlogs lang="fr">Derni�res Billes</lastlogs>
<lastlogs lang="en">Last Logs</lastlogs>
<lastlogs lang="ru"/>
<lastlogs lang="de"/>
<maintenancemode lang="fr">Entretien</maintenancemode>
<maintenancemode lang="en">Maintenance</maintenancemode>
<maintenancemode lang="ru"/>
<maintenancemode lang="de"/>
−
<toolcustom1 lang="fr"> <!-- Random indentation starts here -->
Force Entr�e
<toolcustom1 lang="en">Force Input</toolcustom1>
<toolcustom1 lang="ru"/>
<toolcustom1 lang="de"/>
<toolcustom2 lang="fr">Force Sortie</toolcustom2>
<toolcustom2 lang="en">Force Output</toolcustom2>
<toolcustom2 lang="ru"/>
<toolcustom2 lang="de"/>
<jogaxis lang="fr">Jog Axes</jogaxis>
<jogaxis lang="en">Jog Axis</jogaxis>
<jogaxis lang="ru"/>
<jogaxis lang="de"/> <!-- And finishes here -->
</toolcustom1>
<toolcustom1 lang="en">Force Input</toolcustom1>
<toolcustom1 lang="ru"/>
<toolcustom1 lang="de"/>
</mainmenu>
<menuparam/> <!-- File is empty from here. There should be elements like above. -->
<menudiag/>
<menurod/>
<menuaide/>
<button/>
<progrid/>
<aboutbox/>
<dlgexit/>
<doc/>
<!-- ...like this until... -->
</app>
如果我在第一个元素上获得指针并在调试时进行浏览,这正是 TinyXML 使用“LoadFile()”命令读取的内容。
另请注意,如果我使用 Firefox、写字板或其他任何工具打开该文件(上述错误文件的来源),则语法是正确的,并且根本没有错误。一切都在这里,没有遗漏。
我不怀疑我的读/写代码是错误的,因为当我打开/读/写一个未修改的 XML 文件时,一切似乎都按计划进行。
现在,如果有人知道我为什么会收到此错误,我将非常感激。 TinyXML 支持和文档有些有限,不能解决我的问题。 我正在等待答案。
I'm currently working on a C++ MFC project on visual studio 2003. The aim of this project is to be able to take an XML file containing language data, convert it to a simple CSV file and back to XML. To do this, I'm using the TinyXML library, which is simple and good enough for my needs.
The problem is that, once I try to convert my .CSV file to an XML, the created XML cannot be opened again with TinyXML. From what I see now, it seems it cannot open the file it just created.
After some testing, I've been able to make TinyXML tell me its error: "Error #9: Unable to read end tag". If I bypass the error, the file is readable up to a point where the DOM tree indents to the right for no reason and then, a bit later,the file seems to be empty of any element.
This is an erroneous file as read by TinyXML, after having been created by it.
<app>
−
<global>
<langidglobal1 lang="fr">GLOBAL1</langidglobal1>
<langidglobal1 lang="en">GLOBAL1</langidglobal1>
<langidglobal1 lang="ru"/>
<langidglobal1 lang="de"/>
</global>
−
<mainmenu>
<system lang="fr">Syst�me</system>
<system lang="en">System</system>
<system lang="ru"/>
<system lang="de"/>
<motor lang="fr">Moteurs</motor>
<motor lang="en">Motors</motor>
<motor lang="ru"/>
<motor lang="de"/>
<param lang="fr">Param�tres</param>
<param lang="en">Parameters</param>
<param lang="ru"/>
<param lang="de"/>
<diag lang="fr">Diagnostique</diag>
<diag lang="en">Diagnostic</diag>
<diag lang="ru"/>
<diag lang="de"/>
<rod lang="fr">Tiges</rod>
<rod lang="en">Rods</rod>
<rod lang="ru"/>
<rod lang="de"/>
<aide lang="fr">Aide</aide>
<aide lang="en">Help</aide>
<aide lang="ru"/>
<aide lang="de"/>
<exit lang="fr">Quitter</exit>
<exit lang="en">Exit</exit>
<exit lang="ru"/>
<exit lang="de"/>
<runningtools lang="fr">Outils</runningtools>
<runningtools lang="en">Running Tools</runningtools>
<runningtools lang="ru"/>
<runningtools lang="de"/>
<manualpatterns lang="fr">Patron Manuel</manualpatterns>
<manualpatterns lang="en">Manual Patterns</manualpatterns>
<manualpatterns lang="ru"/>
<manualpatterns lang="de"/>
<alarm lang="fr">Alarmes Actives</alarm>
<alarm lang="en">Active Alarms</alarm>
<alarm lang="ru"/>
<alarm lang="de"/>
<patternno lang="fr"># Patron</patternno>
<patternno lang="en">Pattern #</patternno>
<patternno lang="ru"/>
<patternno lang="de"/>
<lastlogs lang="fr">Derni�res Billes</lastlogs>
<lastlogs lang="en">Last Logs</lastlogs>
<lastlogs lang="ru"/>
<lastlogs lang="de"/>
<maintenancemode lang="fr">Entretien</maintenancemode>
<maintenancemode lang="en">Maintenance</maintenancemode>
<maintenancemode lang="ru"/>
<maintenancemode lang="de"/>
−
<toolcustom1 lang="fr"> <!-- Random indentation starts here -->
Force Entr�e
<toolcustom1 lang="en">Force Input</toolcustom1>
<toolcustom1 lang="ru"/>
<toolcustom1 lang="de"/>
<toolcustom2 lang="fr">Force Sortie</toolcustom2>
<toolcustom2 lang="en">Force Output</toolcustom2>
<toolcustom2 lang="ru"/>
<toolcustom2 lang="de"/>
<jogaxis lang="fr">Jog Axes</jogaxis>
<jogaxis lang="en">Jog Axis</jogaxis>
<jogaxis lang="ru"/>
<jogaxis lang="de"/> <!-- And finishes here -->
</toolcustom1>
<toolcustom1 lang="en">Force Input</toolcustom1>
<toolcustom1 lang="ru"/>
<toolcustom1 lang="de"/>
</mainmenu>
<menuparam/> <!-- File is empty from here. There should be elements like above. -->
<menudiag/>
<menurod/>
<menuaide/>
<button/>
<progrid/>
<aboutbox/>
<dlgexit/>
<doc/>
<!-- ...like this until... -->
</app>
If I get a pointer on the first element and browse while debugging, this is exactly what TinyXML read with the "LoadFile()" command.
Also note that if I do open the file (the source for the erroneous one above) with Firefox, or wordpad, or anything else, the syntax is correct, and there is no error at all. Everything is here, nothing missing.
I don't suspect my read/write code to be wrong, as when I open/read/write an untouched XML file, everything seems to work as planned.
Now if anyone would have any idea as for why do I get this error, I would be very grateful.
The TinyXML support and documentation is somewhat limited and doesn't answer to my problem.
I'm staying tuned for answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我没有得到回复,但我通过解决另一个错误解决了这个错误。我写入 XML 的数据使用了我出于某种原因使用的编码表。通过让 TinyXML 管理自己的编码,这个问题就自行消失了。
这是一件好事,没有明显的原因(即使通过十六进制比较,两个编码表之间的两个ascii字符完全相同,所以我无法意识到是什么导致了问题)。
Well, I've got no replies, but I solved this bug by solving another one. The data I was writing into my XML used encoding tables I was using for whatever reason. By letting TinyXML manage its own encodings, this problem dissapeared on its own.
It's a good thing, there were no apparent cause (even by hex comparing, two ascii characters are exactly the same between two encoding table, so I couldn't realise what caused the problem).
我遇到了同样的问题。
我正在 Notepad++ 中以 UTF-8 编码编辑 XML 文件,同时也在 XML 中指定该编码。还是不行。
所以我尝试将 XML 保存为没有 BOM 的 UTF-8,效果很好;)
I have encountered the same issue.
I was editing my XML file in Notepad++ in UTF-8 encoding, while specifying that encoding in the XML too. Still wouldn't work.
So I tried saving my XML as UTF-8 without BOM and it worked fine ;)