解析规律文件 创建XML

前端之家收集整理的这篇文章主要介绍了解析规律文件 创建XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.maple.util;
//package com.maple.util;
//
//import java.util.Date;
//
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileReader;
//import java.io.FileWriter;
//import java.text.SimpleDateFormat;
//
//import javax.xml.parsers.DocumentBuilder;
//import javax.xml.parsers.DocumentBuilderFactory;
//import javax.xml.transform.Result;
//import javax.xml.transform.Source;
//import javax.xml.transform.Transformer;
//import javax.xml.transform.TransformerFactory;
//import javax.xml.transform.dom.DOMSource;
//import javax.xml.transform.stream.StreamResult;
//
//import org.dom4j.DocumentHelper;
//import org.dom4j.io.OutputFormat;
//import org.dom4j.io.XMLWriter;
//import org.w3c.dom.Document;
//import org.w3c.dom.Element;
//
//
//public class CreateXml {
//
// /**
// * @param args
// */
// public static void main(String[] args) {
// // TODO Auto-generated method stub
// // TODO Auto-generated method stub
// try {
// //鍒涘缓涓�釜绌虹櫧鐨剎ml鏂囨。
//
// File file =new File("F:\\mycountry.txt");
// BufferedReader input = new BufferedReader(new FileReader(file));
// String str = null;
// String[] array =null;
// DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
// DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
// Document document = documentBuilder.newDocument(); //鍦ㄥ唴瀛樹腑鏂板垱寤轰竴涓獂ml鏂囨。
//
// //涓簒ml鏂囨。娣诲姞鍐呭
// Element rootElement = document.createElement("Countrys"); //鍒涘缓鏍瑰厓绱狅紝鍙湁document瀵硅薄鎵嶅彲浠ュ垱寤哄厓绱�country
// document.appendChild(rootElement); //灏嗘牴鍏冪礌娣诲姞鍒皒ml鏂囨。涓�
// while((str=input.readLine())!=null){
//
// array = str.split(":");
//
//
//
//
//
// Element employeeElement = document.createElement("country"); //鍒涘缓employee瀛愬厓绱�
// String s2 = new String(array[1].getBytes("ISO-8859-1"),"GBK");
// String s1 = new String(array[0].getBytes("ISO-8859-1"),"GBK");
// employeeElement.setAttribute("name",s2);//涓鸿鑺傜偣璁剧疆灞炴�鍜屽�
// employeeElement.setAttribute("number",s1);
// rootElement.appendChild(employeeElement);//娣诲姞鍒版牴鍏冪礌涓�
// }
//
// //Element nameElement = document.createElement("name");
// //nameElement.setTextContent(employee.getName()); //璁剧疆璇ヨ妭鐐圭殑鏂囨湰鍐呭

// //employeeElement.appendChild(nameElement);

//
// //Element ageElement = document.createElement("age");
// //ageElement.setTextContent(employee.getAge().toString()); //璁剧疆璇ヨ妭鐐圭殑鏂囨湰鍐呭
// //employeeElement.appendChild(ageElement);
//
// //Element sexElement = document.createElement("sex");
// //sexElement.setTextContent(employee.getSex()); //璁剧疆璇ヨ妭鐐圭殑鏂囨湰鍐呭
// //employeeElement.appendChild(sexElement);
//
// //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// // Element birthdayElement = document.createElement("birthday");
// //birthdayElement.setTextContent(dateFormat.format(employee.getBirthday())); //璁剧疆璇ヨ妭鐐圭殑鏂囨湰鍐呭
// ///employeeElement.appendChild(birthdayElement);
//
// //淇濆瓨xml鏂囨。鍒扮‖鐩樹笂
// TransformerFactory transformerFactory = TransformerFactory.newInstance();
// Transformer transformer = transformerFactory.newTransformer();
// Source xmlSource = new DOMSource(document); //鏍规嵁鍐呭瓨涓殑xml鏂囨。鍒涘缓涓�釜璧勬簮
// Result outputTarget = new StreamResult(new File("F:\\employees.xml")); //浠ユ祦鐨勬柟寮忚緭鍑哄埌鐩殑鍦帮紝鍗充繚瀛樿矾寰� 杩欓噷鍐欐浜嗕繚瀛樿矾寰�
// transformer.transform(xmlSource,outputTarget);//绗竴涓弬鏁帮細瑕佷繚瀛樼殑璧勬簮 绗簩涓弬鏁帮細淇濆瓨鐨勭洰鐨勫湴
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
//
// }
//
//
//
//
//
//
//}




//package com.zjm.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import com.maple.util.Region;

public class CreateXML {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CreateXML xml = new CreateXML();
xml.create();
}

public boolean create() {
boolean flag = false;
Document document = null; //文档
Element books = null; //根节点
Element book = null; //book节点
Element text = null; //text节点
OutputFormat format = null;
XMLWriter writer = null;
String str = null;
String[] array =null;
List<Region> regionList = new ArrayList<Region>();



try {
document = DocumentHelper.createDocument();
books = document.addElement("Countrys");
books.addComment("注释");

File file =new File("F:\\mycountry.txt");
try
{
BufferedReader instreamPut = new BufferedReader(new FileReader(file));

while( (str = instreamPut.readLine())!=null){

array = str.split(":");
// String s2 = new String(array[0].getBytes("ISO-8859-1"),"gb2312");
// String s1 = new String(array[1].getBytes("ISO-8859-1"),"gb2312");
Region region = new Region(array[1],array[0],array[2]);
regionList.add(region);
Collections.sort(regionList,new Comparator<Region>(){


@Override
public int compare(Region arg0,Region arg1) {
// TODO Auto-generated method stub
return arg0.getPinyin().compareToIgnoreCase( arg1.getPinyin());

}});
}
for(Region element:regionList)
{
book = books.addElement("country");
book.addAttribute("name",element.getName());
book.addAttribute("number",element.getNumber());
book.addAttribute("pinyin",element.getPinyin());
//text = book.addElement("text");
// text.setText("text1");

// book = books.addElement("book");
// book.addAttribute("name","book2");
// book.addAttribute("show","false");
// text = book.addElement("text");
// text.setText("text2");



}
format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
//format.setEncoding("utf-8"); //用utf-8也不行

// System.out.println(document.asXML());

writer = new XMLWriter(new FileOutputStream("E://mytext.xml"),format);//FileOutputStream
writer.write(document);
writer.close();

flag = true;

return flag;

} catch (FileNotFoundException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
catch(Exception e) {
e.printStackTrace();
}
return flag;
}

}



package com.maple.util;


public class Region {


private String name;
private String number;
private String pinyin;


/**
* @return the name
*/
public Region(String name,String number,String pinyin)
{
this.name = name;
this.number = number;
this.pinyin = pinyin;


}
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the number
*/
public String getNumber() {
return number;
}
/**
* @param number the number to set
*/
public void setNumber(String number) {
this.number = number;
}
/**
* @return the pinyin
*/
public String getPinyin() {
return pinyin;
}
/**
* @param pinyin the pinyin to set
*/
public void setPinyin(String pinyin) {
this.pinyin = pinyin;
}



}


You could write a custom comparator:

Collections.sort(list, new Comparator<String>() { public int compare(String a, String b) { return Integer.signum(fixString(a) - fixString(b)); } private int fixString(String in) { Integer.parseInt(in.substring(0, in.indexOf('_'))); } });
原文链接:https://www.f2er.com/xml/300221.html

猜你在找的XML相关文章