?? xmlutils.java
字號:
// * 在已有的excelFormName.xml文件上追加新的內容(針對只在屬性里賦值的情況)
// */
// public static void addFormNameXml(String xmlPath,String nodeName,String oneId,String oneValue,String twoId,String twoValue){
// try {
// //Locale.setDefault(new Locale("zh_CN.utf8"));
// SAXBuilder builder = new SAXBuilder(false);
// Document doc = builder.build(xmlPath);
// Element root = doc.getRootElement();
// root.addContent(new Element(nodeName).setAttribute(oneId,oneValue).setAttribute(twoId,twoValue));
// Format format = Format.getPrettyFormat();
// format.setIndent(" ");
// format.setEncoding("UTF-8");
// XMLOutputter outputter = new XMLOutputter(format);
// //outputter.output(doc, new BufferedWriter(new FileWriter(xmlPath)));
// outputter.output(doc,new FileOutputStream(xmlPath));
// }catch(Exception e) {
// e.printStackTrace();
// }
// }
/**
* 創建一個新的xml文件(針對只在屬性里賦值的情況)
*/
public static void createToXml(String xmlPath,String nodeName,String oneId,String oneValue,String twoId,String twoValue,String threeId,String threeValue){
try {
//Locale.setDefault(new Locale("zh_CN.utf8"));
Element root = new Element("root");
Document doc = new Document(root);
root.addContent(new Element(nodeName).setAttribute(oneId,oneValue).setAttribute(twoId,twoValue).setAttribute(threeId,threeValue));
//設置格式
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
}catch(Exception e){
e.printStackTrace();
}
}
// /**
// * 創建一個新的excelFormName.xml文件(針對只在屬性里賦值的情況)
// */
// public static void createFormNameXml(String xmlPath,String nodeName,String oneId,String oneValue,String twoId,String twoValue){
// try {
// //Locale.setDefault(new Locale("zh_CN.utf8"));
// Element root = new Element("root");
// Document doc = new Document(root);
// root.addContent(new Element(nodeName).setAttribute(oneId,oneValue).setAttribute(twoId,twoValue));
// //設置格式
// Format format = Format.getPrettyFormat();
// format.setIndent(" ");
// format.setEncoding("UTF-8");
// XMLOutputter outputter = new XMLOutputter(format);
// outputter.output(doc,new FileOutputStream(xmlPath));
// }catch(Exception e){
// e.printStackTrace();
// }
// }
/**
* 讀取xml文件,取出所有節點的值(只針對xml只有兩層的情況,并且沒有屬性值的情況下)
* @return arrayList
*/
@SuppressWarnings("unchecked")
public static ArrayList getAllNodeValue(String xmlPath){
ArrayList tempList = new ArrayList();
try {
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(xmlPath);
Element books = doc.getRootElement();
List booklist = books.getChildren();
for(int i=0; i < booklist.size() ; i++){
Element book = (Element) booklist.get(i);
tempList.add(book.getText());
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
return tempList;
}
/**
* 讀取xml文件,取出所有字段位置、描述和對應的字段相應的值
*/
@SuppressWarnings("unchecked")
public static ArrayList getAllCellValue(String xmlPath){
ArrayList tempList = new ArrayList();
try {
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(xmlPath);
Element books = doc.getRootElement();
List booklist = books.getChildren("cell");
for(int i=0; i < booklist.size() ; i++){
Element book = (Element) booklist.get(i);
tempList.add(book.getAttributeValue("place"));
tempList.add(book.getAttributeValue("description"));
tempList.add(book.getAttributeValue("field"));
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
return tempList;
}
/**
* 讀取xml文件,取出所有bindClass的值
*/
@SuppressWarnings("unchecked")
public static ArrayList getBindClassValue(String xmlPath){
ArrayList tempList = new ArrayList();
try {
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(xmlPath);
Element books = doc.getRootElement();
List booklist = books.getChildren("bindClass");
for(int i=0; i < booklist.size() ; i++){
Element book = (Element) booklist.get(i);
tempList.add(book.getAttributeValue("formName"));
tempList.add(book.getAttributeValue("className"));
tempList.add(book.getAttributeValue("methodName"));
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
return tempList;
}
/**
* 讀取excelFormName.xml文件,取出表的對應的id和表名的值
*/
@SuppressWarnings("unchecked")
public static ArrayList getFormNameCellValue(String xmlPath){
ArrayList tempList = new ArrayList();
try {
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(xmlPath);
Element books = doc.getRootElement();
List booklist = books.getChildren("formName");
for(int i=0; i < booklist.size() ; i++){
Element book = (Element) booklist.get(i);
tempList.add(book.getAttributeValue("id"));
tempList.add(book.getAttributeValue("name"));
tempList.add(book.getAttributeValue("nameEn"));
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
return tempList;
}
/**
* 讀取excelFormName.xml文件,取出表的對應的id的值
*/
@SuppressWarnings("unchecked")
public static ArrayList getFormNameIdValue(String xmlPath){
ArrayList tempList = new ArrayList();
try {
SAXBuilder builder = new SAXBuilder(false);
Document doc = builder.build(xmlPath);
Element books = doc.getRootElement();
List booklist = books.getChildren("formName");
for(int i=0; i < booklist.size() ; i++){
Element book = (Element) booklist.get(i);
tempList.add(book.getAttributeValue("id"));
}
Format format = Format.getPrettyFormat();
format.setIndent(" ");
format.setEncoding("UTF-8");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(doc,new FileOutputStream(xmlPath));
} catch (Exception e) {
e.printStackTrace();
}
return tempList;
}
/**
* 根據路徑判斷一個文件是否存在
* @return boolean
*/
public static boolean fileIsExits(String xmlPath){
boolean flag = false;
try{
File objFile = new File(xmlPath);
if(objFile.exists()){
flag = true;
}else{
flag = false;
}
}catch(Exception e){
e.printStackTrace();
}
return flag;
}
/**
* 在原來的xml上面追加
*/
/**
* @param args
*/
public static void main(String[] args) throws Exception {
//XmlUtils.createXML("c:/temp.xml","root");
// ArrayList temp = XmlUtils.getAllNodeValue("E:/j2ee/tomcat/webapps/reportIE/pages/reportIE/model/excelFormName.xml");
// System.out.print(temp);
// String xmlPath = "E:/j2ee/tomcat/webapps/reportIE/pages/reportIE/model/excelFormName.xml";
// System.out.print(XmlUtils.fileIsExits(xmlPath));
// String xmlPath = "C:/temp/test.xml";
// System.out.print(XmlUtils.getAllCellValue(xmlPath));
// ArrayList cellList = XmlUtils.getFormNameIdValue(xmlPath);
// System.out.println(XmlUtils.getMaxNumber(cellList));
// System.out.println(XmlUtils.getOtherAttributeValue(xmlPath,"formName","nameEn","rszlk","name"));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -