简体   繁体   English

如何替换html文件中多个标签的内容? [Node.js]

[英]How to replace the content of multiple tags in html-file? [Node.js]

We have html-file: 我们有html文件:

<div class="my1">MY1</div>
<h1 class="my2">MY2</h1>
<p class="my3">MY3</p>
<div class="my2">MY2</div>

and json-file with replace values: 和带有替换值的json文件:

{
    "my1":"new my1",
    "my2":"new my2",
    "my3":"new my3"
}

In result must be new html-file with new values. 结果必须是带有新值的新html文件。

How to do this? 这个怎么做? Without DOM! 没有DOM!

I don't understand why you are against DOM building as it is relativity easy to do. 我不明白为什么您反对DOM构建,因为它相对容易实现。 There are lightweight modules such as cheerio that fulfill your requirements and have a simple API (similar to JQuery). 有些轻量级模块(例如cheerio)可以满足您的要求,并具有简单的API(类似于JQuery)。

Fully working example 完全正常的例子

This example does exactly what you want using the cheerio node module . 这个示例使用cheerio节点模块完全可以实现您想要的功能

Files 档案

replace.js replace.js

var fs = require('fs')
, cheerio = require('cheerio');

// html file we are working on
var htmlPath = __dirname + '/htmlFile.html';
// html file to output modified html to
var outPath = __dirname + '/htmlFixed.html';

// load in the json file with the replace values
var replaceValues = require('./replaceValues.json');

fs.readFile(htmlPath, {encoding: 'utf8'}, function(error, data) {
    var $ = cheerio.load(data); // load in the HTML into cheerio

    for (var key in replaceValues) {
        // the keys are class names, use them to pick out what element
        // we are going to modify & then replace the innerHTML content
        // of that element
        $('.' + key).html(replaceValues[key]);
    }

    fs.writeFile(outPath, $.html());
});

htmlFile.html htmlFile.html

<div class="my1">MY1</div>
<h1 class="my2">MY2</h1>
<p class="my3">MY3</p>
<div class="my2">MY2</div>

replaceValues.json replaceValues.json

{
    "my1":"new my1",
    "my2":"new my2",
    "my3":"new my3"
}

Usage 用法

node replace.js

Results 结果

After running the script, you will have a new file in the same directory as the original HTML file named htmlFixed.html with the changes performed with the contents: 运行脚本之后,您将在与原始HTML文件htmlFixed.html相同的目录中拥有一个新文件,其中包含对内容进行的更改:

<div class="my1">new my1</div>
<h1 class="my2">new my2</h1>
<p class="my3">new my3</p>
<div class="my2">new my2</div>

Node.js does not have any notion of a Document Object Model (ie the DOM) -- "Why doesn't node.js have a native DOM?" Node.js没有文档对象模型(即DOM)的任何概念- “为什么node.js没有本地DOM?” -- and as such does not have access to a way to instantiate HTML Elements (which would be nice to have access to in order to parse your html). -因此无法访问实例化HTML元素的方法(可以访问以便解析html会很高兴)。

As a result, you must import a library to use as a parser (just like any other server side implementation must when dealing with html - don't regex it! ). 结果,您必须导入一个库以用作解析器(就像其他任何服务器端实现在处理html时一样, 请不要进行正则表达式! )。

The current leader in doing this is jsDOM 当前的领导者是jsDOM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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