如何最好地保存 XML 文件

发布于 2024-08-12 08:57:03 字数 438 浏览 8 评论 0原文

我有一堆类,每个类都使用 TinyXML 从 XML 文件中读取它们的值。

我已经这样做了,所以所有内容都在内存中,并且我的用户正在使用该应用程序并进行更改。如果用户按下 Save,我需要遍历我的对象并调用 Save() 函数来写出 XML 文件。我应该以编程方式从头开始重建 XML 文件并将其写出来吗?或者我应该使用与读取文件相同的 TiXmlHandle ,修改它,然后将其写回?这样,原始顺序就被保留了。如果我重新创建 XML,那么原始的顺序将会改变。

我希望用户能够手动修改 XML,所以我认为他们手动更改它,只是在保存时重新排列它会很奇怪。我正确理解我的选择吗?

以前从未使用过 TinyXML,也没有使用过 XML。

I have a bunch of classes that each read in their values from an XML file using TinyXML.

I've done this so everything is in memory, and my user is using the app and making changes. If the user presses Save, I need to iterate through my objects and call the Save() function which writes out the XML file. Should I rebuild the XML file programmatically from scratch and write it out? Or should I use the same TiXmlHandle that I used to read the file, modify that and then write it back out? That way, the original order is kept. If I recreate the XML, then the order of the original would be changed.

I want the user to be able to modify the XML by hand, so I think it would be wierd for them to change it manually, only to have it reshuffled when saving. Am I understanding my options correctly?

Never used TinyXML before, or XML for that matter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

他是夢罘是命 2024-08-19 08:57:03

一旦您读取了 XML 输入,我就会处理掉读取句柄。

要将它们写回,您通常会在父或主文档类型对象中使用 writeXML() 方法,然后在设计中的每个对象中调用 writeXML() 方法。

附:拥有不同的 XML 读取和写入库并不罕见。
pps。如果您担心 XMl 文档中的顺序无法从对象层次结构中强制执行,那么您就存在一些基本设计错误。

I would dispose of the reading handles once you have read the XML input.

To write them back out you would typically have a writeXML() method in the parent or main document type object an then have that call a writeXML() method in each object in your design.

ps. It's not unusual to have different XML reading and writing libs.
pps. if you are concerned about the order in an XMl document that you can't enforce from your object hierarchy - you have some fundemantal design wrong.

挽袖吟 2024-08-19 08:57:03

第一部分并不是对您的问题的直接回答,但我将继续您的句子“我以前从未使用过 XML”。由于当今 XML 在计算领域的盛行,因此值得花一些时间来了解 XML 的工作原理。对于直接参考,我推荐 Harold 和 Means 所著的《XML in a Nutshell》。它解释了 XML、模式(精确定义传入的 XML 必须是什么样子)、XPath(如何导航节点以获取所需的数据)、XQuery、XSLT、命名空间等。

真正对您有帮助的是概念,为此您可能需要一本不同的书(抱歉,我没有推荐的书。)了解为什么应该以正确的方式使用 XML 很重要,而不仅仅是将其视为美化的 .INI 文件。为什么 DOM 概念很流行。是否应该使用 SAX。

最需要学习的相关内容可能是 XML 模式 (XSD)。虽然有时看起来有些过分,但您应该强烈考虑根据其架构验证您的 XML(这是对完整 XML 解析器(如 xerces)的单一方法调用。)请记住,这是 2009 年,您的程序正在一台计算机上运行大量内存,并且它可以非常非常快地完成大部分工作;因此,知道您的数据良好是完全值得的。一旦您理解了模式,我建议您学习如何对它们进行版本控制。这在处理程序存储时尤其重要,因为您可以让程序理解已保存文件的先前版本并自动使用或升级它们。

为了更直接地回答您的直接问题,请使用 TinyXML(或您选择的任何解析器)进行编写。当您已经拥有工具时,不要重新发明输出轮。

This first part isn't a direct answer to your question, but I'm going on your sentence "I've never used XML before." Because of the prevalence of XML in computing these days, it's worth spending some time to understand how XML works. For a straight-up reference, I recommend "XML in a Nutshell" by Harold and Means. It explains XML, schemas (which define exactly what the incoming XML must look like,) XPath (how you navigate nodes to arrive at the data you want), XQuery, XSLT, namespaces, etc.

What is really going to help you are the concepts, and for that you might want a different book (sorry, I don't have one to recommend.) It's important to know why you should use XML the right way, and not just to think of it as a glorified .INI file. Why the DOM concept is prevalent. Whether or not you should use SAX.

Probably the most relevant things to learn are XML schemas (XSDs). While it sometimes seems like overkill, you should strongly consider validating your XML against its schema (which is a one-method-call to a full XML parser such as xerces.) Remember, this is 2009, your program is running on a computer with a lot of memory, and it can do most of that stuff really, really fast; so it's totally worth it to know your data is good. And once you understand schemas, I'd recommend learning how to version them. That's especially important when dealing with program storage, because you can then make your programs understand previous versions of saved files and use or upgrade them automatically.

To more directly answer your immediate question, use TinyXML (or whatever parser you choose) for writing. Don't reinvent the output wheel when you already have the tool in hand.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文