java实现修改html模板内容

pom.xml中引入
<!-- java 解析htm包 -->
<dependency>
    <groupId>org.jsoup</groupId>
    <artifactId>jsoup</artifactId>
    <version>1.11.3</version>
    <scope>compile</scope>
</dependency>
解析工具类
package com.jyd.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Map;

/**
 * Created by limingliang on 11:41 2018/5/25
 */
public class DocumentUtil {

    /*传入文件路径 传入参数Map 组合后获取document*/
    public static Document getDocument(String fileUrl, Map<String, String> map) throws IOException {
        File file = new File(fileUrl);
        return getDocument(file, "UTF-8", map);
    }

    /*传入文件路径 传入参数Map 转码方式 获取document*/
    public static Document getDocument(String fileUrl, String charsetName, Map<String, String> map) throws IOException {
        File file = new File(fileUrl);
        return getDocument(file, charsetName, map);
    }

    /*传入文件 传入参数 使用默认UTF-8*/
    public static Document getDocument(File file, Map<String, String> map) throws IOException {
        return getDocument(file, "UTF-8", map);
    }

    /*读取文件到Document*/
    public static Document getDocument(File file, String charsetName, Map<String, String> map) throws IOException {
        // 解析模板文件为 doc
        Document doc = Jsoup.parse(file, charsetName);
        Element element = null;
        // 获取元素并操作元素对象赋值
        for (Map.Entry<String, String> entry : map.entrySet()) {
            element = doc.getElementById(entry.getKey());
            if (element != null) {
                element.html(entry.getValue()==null?"":entry.getValue());
            }
        }
        // 把超文本语音html转换为可扩展超文本语句xhtml
//        doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
        return doc;
    }

    /*传入到哪个文件路径 传入生成的document*/
    public static void write(String fileUrl, Document doc) throws IOException {
        File file = new File(fileUrl);
        write(file, "UTF-8", doc);
    }
    /*传入到哪个文件路径 传入生成的document*/
    public static void write(String fileUrl,String charsetName, Document doc) throws IOException {
        File file = new File(fileUrl);
        write(file, charsetName, doc);
    }
    /*传入到哪个文件 传入生成的document*/
    public static void write(File file, Document doc) throws IOException {
        write(file, "UTF-8", doc);
    }

    // 将document内容写入文件中
    public static void write(File file, String charsetName, Document doc) throws IOException {
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file, false);
        // 设置输出流
        OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName);
        // 讲doc写入文件中
        osw.write(doc.html());
        osw.close();
    }
}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值