工具类-创建xml格式文件

/**
     * 创建xml文件
     * @param dirName 文件夹名称
     * @param rootName 根节点
     * @param channelName 子节点
     * @param fileName 文件名
     * @param filePath 文件路径
     * @param mapItems 结果集
     * @Param fileSize 文件大小 (子节点个数)
     */
    public static void createXml(String dirName,String rootName, String channelName, String fileName, String filePath, List<Map<String, String>> mapItems,Integer fileSize) {
        if (mapItems == null) {
            return;
        }
        List<List<Map<String,String>>> trans = listChunk(mapItems,fileSize);
        try {

            int token = 0;
           for (int i = 0; i < trans.size(); i ++) {
               // 生成一个根节点
               Element root = new Element(rootName);
               // 生成一个document对象
               Document document = new Document(root);
                //添加子节点数据
                buildNodes(root, channelName, trans.get(i) );

                Format format = Format.getCompactFormat();
                // 设置换行Tab或空格
                format.setIndent("	");
                format.setEncoding("UTF-8");

                // 创建XMLOutputter的对象
                XMLOutputter outputer = new XMLOutputter(format);
                // 利用outputer将document转换成xml文档
                File file = null;
                if(trans.size() <= 1){
                   file = new File(filePath +File.separator+dirName, fileName +  ".xml");
                }else {
                    file = new File(filePath +File.separator+dirName, fileName +"_"+ token + ".xml");
                }

                if (!file.exists()) {
                    file.getParentFile().mkdirs();
                }
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                outputer.output(document, fos);
                fos.close();
                token++;

                root.detach();
            }


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

    private static void buildNodes(Element root, String channelName, List<Map<String, String>> mapItems) {
        Element channel = null;
        Element node = null;
        Long i = 0L;
        for (Map<String, String> mapItem : mapItems) {
            if ("".equals(channelName)) {
                channel = new Element("item" + i);
                i++;
            } else {
                channel = new Element(channelName);
            }
            for (Map.Entry<String, String> entry : mapItem.entrySet()) {
                node = new Element(entry.getKey());
                if (!(entry.getValue() instanceof String)) {
                    String text = String.valueOf(entry.getValue());
                    node.setText(text);
                } else {
                    node.setText(entry.getValue());
                }
                channel.addContent(node);
            }
            root.addContent(channel);
        }
    }

    public static List<List<Map<String,String>>> listChunk(List<Map<String,String>> chunkList, int chunkNum) {
        if (chunkList == null || chunkNum <= 0) {
            List<List<Map<String,String>>> t = new ArrayList<>();
            t.add(chunkList);
            return t;
        }
        Iterator<Map<String,String>> iterator = chunkList.iterator();
        int i = 1;
        List<List<Map<String,String>>> total = new ArrayList<>();
        List<Map<String,String>> tem = new ArrayList<>();
        while (iterator.hasNext()) {
            Map<String,String> next = iterator.next();
            tem.add(next);
            if (i == chunkNum) {
                total.add(tem);
                tem = new ArrayList<>();
                i = 0;
            }
            i++;
        }
        if(!CollectionUtils.isEmpty(tem)){
            total.add(tem);
        }
        return total;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在WebFlux中接收XML格式文件可以使用以下步骤: 1. 添加依赖 在Maven项目中,需要添加以下依赖: ```xml <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.12.1</version> </dependency> ``` 2. 创建POJO类 根据XML文件的格式,创建一个POJO类来表示它的结构。使用注解`@JacksonXmlRootElement`指定根元素的名称。 ```java @JacksonXmlRootElement(localName = "person") public class Person { @JacksonXmlProperty(localName = "name") private String name; @JacksonXmlProperty(localName = "age") private int age; // getters and setters } ``` 3. 创建控制器方法 在控制器中创建一个方法来接收XML文件。使用注解`@PostMapping`指定请求的路径和HTTP方法。使用注解`@RequestBody`指定接收的请求体,并使用`MappingJackson2XmlHttpMessageConverter`将XML转换为POJO对象。 ```java @PostMapping(value = "/persons", consumes = MediaType.APPLICATION_XML_VALUE) public Mono<Person> createPerson(@RequestBody Mono<Person> person) { return person; } ``` 4. 测试 使用Postman或其他工具发送XML格式的请求体,请求路径为`/persons`,HTTP方法为POST。 ```xml <person> <name>Tom</name> <age>25</age> </person> ``` 控制器方法将接收请求体,并将其转换为`Person`对象。返回`Mono<Person>`对象表示响应体。 参考资料: - [How to consume XML request in Spring WebFlux?](https://stackoverflow.com/questions/61289256/how-to-consume-xml-request-in-spring-webflux)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值