首页 > 其他分享 >记一次XML文件 批量添加属性

记一次XML文件 批量添加属性

时间:2023-08-01 15:15:43浏览次数:30  
标签:XML XMLWriter 批量 read writer 添加 toString path new

 public static void main(String[] age){

        try {
            List<Path> collect = Files.walk(Paths.get("D:\\modelform")).filter(Files::isRegularFile).collect(Collectors.toList());
            SAXReader reader = new SAXReader();
            Map<String,String> map =new HashMap<>();
            for (Path path : collect) {
                Document read = reader.read(new FileInputStream(path.toString()));
                String name = read.getRootElement().attributeValue("name");
                map.put(path.getFileName().toString(),name);
            }

            List<Path> collect2 = Files.walk(Paths.get("D:\\modelfield")).filter(Files::isRegularFile).collect(Collectors.toList());

            Map<String,Document> m =new HashMap<>();
            for (Path path : collect2) {
                Document read = reader.read(new FileInputStream(path.toString()));
                String s = path.getFileName().toString();
                read.getRootElement().element("model").addAttribute("name",map.get(s));
                m.put(path.toString(),read);
            }
            OutputFormat format = OutputFormat.createPrettyPrint();

            for (Map.Entry<String, Document> stringDocumentEntry : m.entrySet()) {
                File file1 =new File(stringDocumentEntry.getKey());
                XMLWriter writer = new XMLWriter(new FileOutputStream(file1), format);
                writer.setEscapeText(false);
                writer.write(stringDocumentEntry.getValue());
                writer.close();
            }

//            File file1 =new File("");
//            // 6、生成xml文件
//            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
//            // 设置是否转义,默认使用转义字符
//            writer.setEscapeText(false);
//            writer.write(document);
//            writer.close();
            System.out.println("生成rss.xml成功");



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

 

 public static void main(String[] age){

try {
List<Path> collect = Files.walk(Paths.get("D:\\modelform")).filter(Files::isRegularFile).collect(Collectors.toList());
SAXReader reader = new SAXReader();
Map<String,String> map =new HashMap<>();
for (Path path : collect) {
Document read = reader.read(new FileInputStream(path.toString()));
String name = read.getRootElement().attributeValue("name");
map.put(path.getFileName().toString(),name);
}

List<Path> collect2 = Files.walk(Paths.get("D:\\modelfield")).filter(Files::isRegularFile).collect(Collectors.toList());

Map<String,Document> m =new HashMap<>();
for (Path path : collect2) {
Document read = reader.read(new FileInputStream(path.toString()));
String s = path.getFileName().toString();
read.getRootElement().element("model").addAttribute("name",map.get(s));
m.put(path.toString(),read);
}
OutputFormat format = OutputFormat.createPrettyPrint();

for (Map.Entry<String, Document> stringDocumentEntry : m.entrySet()) {
File file1 =new File(stringDocumentEntry.getKey());
XMLWriter writer = new XMLWriter(new FileOutputStream(file1), format);
writer.setEscapeText(false);
writer.write(stringDocumentEntry.getValue());
writer.close();
}

// File file1 =new File("");
// // 6、生成xml文件
// XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// // 设置是否转义,默认使用转义字符
// writer.setEscapeText(false);
// writer.write(document);
// writer.close();
System.out.println("生成rss.xml成功");



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

标签:XML,XMLWriter,批量,read,writer,添加,toString,path,new
From: https://www.cnblogs.com/wangfl/p/17596541.html

相关文章

  • Excel VBA 窗体UserForm制作菜单栏与添加窗体最大化最小化功能(转载)
    窗体'--------------------------------------------------------'->Forms'Module'ClassModules'--------------------------------------------------------OptionExplicitPrivateDeclareFunctionFindWindowLib"user32&qu......
  • 分页批量请求数据封装
    关于淘宝接口每次最多取100个,需要分页获取的封装总体思路采取迭代器的方式来多次发送请求. TOPCollection<TOPArticleOrder>orders=session.getArticleOrdersByNick(articleCode,start,end,nick);size=saveOrders(orders.toList()); 最终调用TOPCollection中的toList()......
  • mybatis-plus数据批量插入
       为了提高数据处理效率,大量数据需要插入数据时可以采用批量数据插入的策略提高数据插入的效率。如下是实现方法1、代码结构 2、实体类packagelittle.tiger.one.application.testblockt.domain;importcom.baomidou.mybatisplus.annotation.TableId;importcom.ba......
  • Win11:添加用户
    学习自:Win11如何添加用户Win11添加用户账户的方法【详解】-太平洋IT百科提示网上还有别的教程,例如Win11如何创建新用户-百度经验,使用该方法我找不到设备管理器下的本地用户和组,遂作罢。过程1)按住WIN+R,输入netplwiz,进入用户账户页面; 2)添加,不使用Microsoft账户登录3)输入......
  • C# 反序列化报错 XML 文档(1, 2)中有错误:不应有 <xml xmlns=''>
    1.XmlSerializer症状用XmlSerializer进行xml反序列化的时候,程序报错:​不应有<xmlxmlns=''>。​​说明: ​执行当前Web请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 ​异常详细信息: ​System.InvalidOperatio......
  • 添加zabbix信息,导致zabbix_server无法启动
    1.添加过多的设备,导致zabbix_server假死 zabbixserverisnotrunning,theinformationdisplayedmaynotbecurrent. 2.查看zabbix日志发现/tmp/zabbix_server.log oom了。 三、问题解决1.根据报错中的如下条目,确认为CacheSize参数配置有问题导致9655:20220726......
  • error while loading shared libraries: libxml2.so.2: cannot open shared object fi
    参考:https://blog.csdn.net/qq_39779233/article/details/128215517  ============================================   errorwhileloadingsharedlibraries:libxml2.so.2:cannotopensharedobjectfile解决方法  解决方法:sudoapt-getinstalllibxml......
  • Maven配置中pom.xml和setting.xml之间的关系
    在日常的开发中,我们拿IDEA举例,一般会在电脑上自行下载安装Maven,然后在IDEA中配置我们安装的Maven路径,通过修改setting.xml文件,例如配置公共的镜像仓库地址,来提高依赖下载速率;或者配置内部私有的镜像仓库地址,来引入一些私有的依赖等等。1、如何从指定的镜像仓库下载依赖如下图在......
  • Mybatis学习(1)——mybatis介绍 & 入门案例 & 全局配置文件详解 & 增删改查 + mybatis事
    Mybatis学习(1)——mybatis介绍&入门案例&全局配置文件详解&增删改查+mybatis事务&mapper.xml文件#{}和${}&动态SQL入门原文链接:https://blog.csdn.net/Pireley/article/details/131520252目录引出一、mybatis是啥1.官网&ORM(ObjectRelationMapping)对象关......
  • Mybatis中的Mapper.xml映射文件sql查询接收多个参数 分页查询 sql语句动态生成的时候,
    Mybatis中的Mapper.xml映射文件sql查询接收多个参数分页查询sql语句动态生成的时候,使用${},sql语句中某个参数进行占位的时候用#{}原文链接:https://blog.csdn.net/Ep_Little_prince/article/details/100687848​我们都知道,在Mybatis中的Mapper.xml映射文件可以定制动态SQ......