如何附加到某个文件行? [英] How to append to certain line of file?

查看:146
本文介绍了如何附加到某个文件行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要附加到文件但我想在< / xml> 标记结束之前开始追加。出现。我该怎么做?

I want to append to a file but I want to start appending before </xml> tag end. appears. How can I do that?

var fs = require("fs");
fs.appendFile('somefile.xml', 'Content')

我有......像这样但它在文件的末尾插入'content',我希望它在特定的'keyword'之前的特定行中

I have sth like this but it insert 'content' at the end of file, and I want it in specific line before specific 'keyword'

推荐答案

一种好方法是将文件拆分为包装文件和正文文件。包装器只包含最外层标记和对body文件的实体引用。然后,您的程序可以附加到正文文件的末尾,而任何以XML格式读取文件的人都应该读取包装文件。包装文件看起来像这样:

A good approach is to split the file into a wrapper file and a body file. The wrapper just contains the outermost tag and an entity reference to the body file. Your program can then append to the end of the body file, whereas anyone reading the file as XML should read the wrapper file. The wrapper file will look something like this:

<!DOCTYPE xml [
<!ENTITY e SYSTEM "body.xml">
]>
<xml>&e;</xml>

顺便说一下,使用xml作为元素名称是不好的做法。所有以xml开头的名称都保留供W3C将来使用。

Using "xml" as an element name, by the way, is bad practice. All names starting "xml" are reserved for future use by W3C.

如果您在浏览器中读取XML,则此方法可能无效。我认为某些浏览器的XML解析器不支持外部实体。

This approach may not work if you're reading the XML in a browser. I think that some browsers' XML parsers don't support external entities.

这篇关于如何附加到某个文件行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆