【XML生成】根据JSON格式化的报文,动态生成XML

无需创建实体对象,根据属性动态生成XML方式

java 复制代码
	import org.w3c.dom.Document;
	import org.w3c.dom.Element;
	import java.io.File;
	import javax.xml.parsers.DocumentBuilder;
	import javax.xml.parsers.DocumentBuilderFactory;
	import javax.xml.transform.OutputKeys;
	import javax.xml.transform.Transformer;
	import javax.xml.transform.TransformerFactory;
	import javax.xml.transform.dom.DOMSource;
	import javax.xml.transform.stream.StreamResult;

	//根据动态属性,生成XML文件
    public static void generateXmlFromMap(Map<String, String> data, String rootName, String filePath) {
        try {
            // 创建 XML 文档
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.newDocument();

            // 创建根节点
            Element root = doc.createElement(rootName);
            doc.appendChild(root);

            // 遍历 Map,每个 key 作为标签名,value 作为文本内容
            for (Map.Entry<String, String> entry : data.entrySet()) {
                String tagName = entry.getKey();
                String value = entry.getValue() == null ? "" : entry.getValue().trim();

                Element element = doc.createElement(tagName);
                element.setTextContent(value);
                root.appendChild(element);
            }

            // 写出到文件
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 美化格式
            transformer.transform(new DOMSource(doc), new StreamResult(new File(filePath)));

            System.out.println("XML 文件已生成:" + filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 测试
    public static void main(String[] args) {
        // 模拟从 JSON
        Map<String, String> data = new HashMap<>();
        data.put("archiveNumber", "JZ-2025-0000");
        data.put("archiveDate", "2025-11-20");
        data.put("operUser", "张三");
        data.put("archiveTitle", "2025年11月记账凭证");
        data.put("creater", "张三");

        // 生成 XML
        generateXmlFromMap(data, "Archive", "archive.xml");
    }
相关推荐
@#---3 小时前
如何准确判断json文件并且拿到我想要的信息
android·python·json
WarPigs7 小时前
Unity添加Newtonsoft.json
json
jiayong2314 小时前
Spring XML解析与BeanDefinition注册详解
xml·java·spring
张彦峰ZYF14 小时前
巨大 JSON / 图结构数据架构层面选型:该放 Redis 还是 MongoDB?
redis·架构·json·巨大json/图结构架构选型·redis-mongodb
一颗不甘坠落的流星1 天前
【Antd】基于 Upload 组件,导入Json文件并转换为Json数据
前端·javascript·json
亮子AI1 天前
application/json 服务器收到的是字符串,还是json对象?
运维·服务器·json
im_AMBER1 天前
weather-app开发手记 02 JSON基础 | API 调用 400 错误修复 | JWT 认证问题
笔记·学习·json·axios·jwt
就叫飞六吧2 天前
Spring 框架中的 Bean 继承:`parent` 属性 (XML配置)
xml·java·spring
极客智造2 天前
深入解析.NET 中的 XDocument:解锁 XML 处理的高级特性
xml·.net
Irene19912 天前
Prettier 配置文件 .prettierrc.js 和 .prettierrc.json 的区别
javascript·json