开发者

How do I append text to an existing XML file without rewriting the entire document?

开发者 https://www.devze.com 2023-01-10 00:51 出处:网络
开发者_运维技巧I have a large amount of data in an XML file, and I\'d like to append data to this file without rewriting it every time.I already know how to write the entire file out, but I\'m struggl
开发者_运维技巧

I have a large amount of data in an XML file, and I'd like to append data to this file without rewriting it every time. I already know how to write the entire file out, but I'm struggling with how to append data to this file. Do you have a suggestion for how to do this?


XML isn't a good format for this - if you append to a previously-complete document, it's no longer a complete document.

One option (depending on the APIs available to you) is not to write the root tag or document declaration, but to fake them when you read the file. So you'd have:

  • Fake document declaration
  • Fake root open tag
  • Real data from the file
  • Fake root close tag

Then you can just append elements to the end of your file at will. It will depend on what you're trying to do with this file though - and whether you can fake a stream input which effectively "tops and tails" the real data in the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号