properties文件转xml

来源:互联网 发布:成都网络推广 编辑:程序博客网 时间:2024/06/03 13:31
poem.propertiesa0=人生若只如初见,a6=何如薄幸锦衣郎,a2=等闲变却故人心,a3=却道故人心易变。a5=泪雨零铃终不怨。a1=何事秋风悲画扇。a7=比翼连枝当日愿。a4=骊山语罢清宵半,b0=莺啼岸柳弄春晴夜月明b3=红炉透炭炙寒风御隆冬b2=秋江楚雁宿沙洲浅水流b1=\u9999\u83B2\u78A7\u6C34\u52A8\u98CE\u51C9\u590F\u65E5\u957F
poem.xml
<?xml version="1.0" encoding="UTF-8"?>
<poem>  <poem0>    <line>      <p>人生若只如初见,</p>      <p>何事秋风悲画扇。</p>    </line>    <line>      <p>等闲变却故人心,</p>      <p>却道故人心易变。</p>    </line>    <line>      <p>骊山语罢清宵半,</p>      <p>泪雨零铃终不怨。</p>    </line>    <line>      <p>何如薄幸锦衣郎,</p>      <p>比翼连枝当日愿。</p>    </line>  </poem0>  <poem1>    <line>      <p>莺啼岸柳弄春晴</p>    </line>    <line>      <p>柳弄春晴夜月明</p>    </line>    <line>      <p>明月夜晴春弄柳</p>    </line>    <line>      <p>柳弄春晴夜月明</p>    </line>  </poem1>  <poem2>    <line>      <p>香莲碧水动风凉</p>    </line>    <line>      <p>水动风凉夏日长</p>    </line>    <line>      <p>长日夏凉风动水</p>    </line>    <line>      <p>水动风凉夏日长</p>    </line>  </poem2>  <poem3>    <line>      <p>秋江楚雁宿沙洲</p>    </line>    <line>      <p>雁宿沙洲浅水流</p>    </line>    <line>      <p>流水浅洲沙宿雁</p>    </line>    <line>      <p>雁宿沙洲浅水流</p>    </line>  </poem3>  <poem4>    <line>      <p>红炉透炭炙寒风</p>    </line>    <line>      <p>炭炙寒风御隆冬</p>    </line>    <line>      <p>冬隆御风寒炙炭</p>    </line>    <line>      <p>炭炙寒风御隆冬</p>    </line>  </poem4></poem>
package day01;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.Map.Entry;import java.util.Properties;import java.util.TreeMap;import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.XMLWriter;public class PropertiesToXml {public static void main(String[] args) throws Exception  {// 读取资源文件Properties properties = new Properties();InputStreamReader in = new InputStreamReader(PropertiesToXml.class.getClassLoader().
getResourceAsStream("poem.properties"),"UTF-8");properties.load(in);// 存在TreeMap中自然排序TreeMap<Integer, String> aMap = new TreeMap<>();TreeMap<Integer, String> bMap = new TreeMap<>();for (Entry<Object, Object> entry : properties.entrySet()) {String key = entry.getKey().toString().replace("\uFEFF", "");String value = (String) entry.getValue();if (key.startsWith("a")) {aMap.put(Character.digit(key.charAt(1), 10), value);} else if (key.startsWith("b")) {bMap.put(Character.digit(key.charAt(1), 10), value);}}final String rootName = "poem";Document doc = DocumentHelper.createDocument();//创建根节点 <poem>Element poem = doc.addElement(rootName);int index = 0;// 解析aif (aMap.size() > 0) {Element poem0 = poem.addElement(rootName + index++);Element currentLine = null;for (String content : aMap.values()) {if (currentLine == null) {currentLine = poem0.addElement("line");}Element p = currentLine.addElement("p");p.setText(content);if (content.endsWith("。")) {currentLine = null;}}}// 解析bif (bMap.size() > 0) {for (String value : bMap.values()) {Element poemN = poem.addElement(rootName + index++);String[] targets = new String[4];targets[0] = value.substring(0, 7);targets[1] = value.substring(3);StringBuilder valueB = new StringBuilder(value);targets[2] = valueB.reverse().substring(0, 7);targets[3] = valueB.reverse().substring(3);for (String target : targets) {poemN.addElement("line").addElement("p").setText(target);}}}//输出poem.xml文件XMLWriter writer = new XMLWriter(new FileOutputStream(new File("poem.xml")),
 OutputFormat.createPrettyPrint());writer.write(doc);writer.close();}}


 
原创粉丝点击