Java删除xml里的元素和属性

avatar 2019年10月12日09:49:56 6 4352 views
博主分享免费Java教学视频,B站账号:Java刘哥
今天有个朋友给了我一个xml文件,几千行左右,让我帮她写个Demo,实现删除一些元素和属性的功能。 例子如下
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE population SYSTEM "http://www.matsim.org/files/dtd/population_v6.dtd">
  3. <population>
  4.     <person id="0">
  5.         <plan selected="yes">
  6.             <activity type="home" link="30263" x="1.1587762319108987E7" y="3594363.525306449" end_time="09:46:37" >
  7.             </activity>
  8.             <leg mode="car" dep_time="09:46:37" >
  9.             </leg>
  10.             <activity type="home" link="22096" x="1.1587164533443427E7" y="3587487.902932038" >
  11.             </activity>
  12.         </plan>
  13.     </person>
  14.     <person id="1">
  15.         <plan selected="yes">
  16.             <activity type="home" link="4714" x="1.1585743985421415E7" y="3602582.1929422976" end_time="15:33:05" >
  17.             </activity>
  18.             <leg mode="car" dep_time="15:33:05" >
  19.             </leg>
  20.             <activity type="home" link="24738" x="1.1584318094063845E7" y="3579553.166465933" >
  21.             </activity>
  22.         </plan>
  23.     </person>
  24. </population>
需求如下:
  1. 删除 leg 元素
  2. 删除 x 和 y 属性
  3. 替换 type="home"  为 type="activity"
  实现方法我觉得有两种,一种是通过如 dom4j 之类的工具解析 xml,然后操作 xml 节点;另一种是通过正则表达式字符串替换 这里选择后者,通过正则替换,因为文件不大,可以一次性读取所有的内容,实现代码如下
  1. import java.io.*;
  2. /**
  3.  * @author 言曌
  4.  * @date 2019-10-11 16:10
  5.  */
  6. public class Demo {
  7.     /**
  8.      * 从文件里读取内容
  9.      *
  10.      * @param filePath
  11.      * @return
  12.      */
  13.     private static String readFromFile(String filePath) {
  14.         // 使用ArrayList来存储每行读取到的字符串
  15.         StringBuilder sb = new StringBuilder();
  16.         try {
  17.             FileReader fr = new FileReader(filePath);
  18.             BufferedReader bf = new BufferedReader(fr);
  19.             String str;
  20.             // 按行读取字符串
  21.             while ((str = bf.readLine()) != null) {
  22.                 sb.append(str + "\r\n");
  23.             }
  24.             bf.close();
  25.             fr.close();
  26.         } catch (IOException e) {
  27.             e.printStackTrace();
  28.         }
  29.         return sb.toString();
  30.     }
  31.     /**
  32.      * 往文件中写入内容
  33.      *
  34.      * @param outputFilePath 输出文件路径
  35.      * @param content
  36.      */
  37.     public static void writeToFile(String outputFilePath, String content) {
  38.         File file = new File(outputFilePath);
  39.         try {
  40.             FileWriter writer = new FileWriter(file);
  41.             writer.write(content);
  42.             writer.close();
  43.         } catch (IOException e) {
  44.             e.printStackTrace();
  45.         }
  46.     }
  47.     /**
  48.      * 修改
  49.      * @param args
  50.      */
  51.     public static void main(String[] args) {
  52.         //1. 一次性读取该文件所有内容
  53.         String content = readFromFile("/Users/liuyanzhao/Downloads/population.xml");
  54.         //2. 处理数据
  55.         //2.1 去掉<leg></leg>标签里的内容和leg标签
  56.         content = content.replaceAll("(?<=<leg)[\\s\\S]*?(?=</leg>)""");
  57.         content = content.replaceAll("<leg</leg>""");
  58.         //2.2 去掉x=""标签
  59.         content = content.replaceAll("x=\"[^\"]*\"""");
  60.         //2.3 去掉y=""标签
  61.         content = content.replaceAll("y=\"[^\"]*\"""");
  62.         //2.4 替换 type="home"为type="activity"
  63.         content = content.replaceAll("type=\"home\"""type=\"activity\"");
  64.         System.out.println(content);
  65.         //3. 新内容输出到文件
  66.         writeToFile("/Users/liuyanzhao/Downloads/population-output.xml", content);
  67.     }
  68. }
 
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0