xmlread.java
来自「TTreeview 的 Java实例 Eclipes环境下」· Java 代码 · 共 243 行
JAVA
243 行
package demo.pluginA.treeview.xmlutils;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import demo.pluginA.treeview.data.*;
public class XMLRead {
/** XML tags */
private final static String id = "id";
private final static String description = "description";
private final static String status = "status";
private final static String name = "name";
private final static String timestamp = "timestamp";
private final static String order = "order";
private final static String option = "option";
private final static String root_activity = "activities";
private final static String node_activity = "activity";
private final static String root_task = "tasks";
private final static String node_task = "task";
private final static String root_step = "steps";
private final static String node_step = "step";
private final static String root_query = "queries";
private final static String node_query = "query";
private final static short tag_process = 1;
private final static short tag_activity = 2;
private final static short tag_task = 3;
private final static short tag_step = 4;
private final static short tag_bug = 0;
//attributes
private String strFilename;
private Document doc;
private ArrayList<ITreeEntry> list;
private boolean isValid = true;
//constructors
public XMLRead(String strFilename) {
super();
this.strFilename = strFilename;
initParameters();
}
//methods
public void initParameters(){
doc = null;
list = new ArrayList<ITreeEntry>();
try{
setDoc();
}catch(Exception e){
this.isValid = false;
System.out.println(e.getMessage());
}
}
public String getStrFilename() {
return strFilename;
}
private void setDoc() throws Exception {
try{
File f = new File(strFilename);
SAXReader reader = new SAXReader();
doc = reader.read(f);
}catch (Exception e) {
this.isValid = false;
System.out.println(e.getMessage());
throw e;
}
}
public boolean isValid() {
return isValid;
}
public ArrayList<ITreeEntry> getList() {
return list;
}
public void DocToList() throws Exception {
if(doc == null){
this.isValid = false;
System.out.println("doc is null");
return;
}
Element root;
try{
root = doc.getRootElement();
List<ITreeEntry> listOfNode;
listOfNode = addEntity(root, tag_process);
ProcessEntity pe = new ProcessEntity();
pe.setName(root.elementText(name));
pe.setDescription(root.elementText(description));
//*******pe.setIdProcess(root.attribute(id).getValue());
pe.setChildren(listOfNode);
list.add(pe);
}catch(Exception e){
this.isValid = false;
System.out.println(e.getMessage());
throw e;
}
}
public List<QueryEntity> addQueryEntity(Element root){
Element foo, element;
if(root.elementIterator(root_query).hasNext() == false)
return null;
if(tag_bug == 1)
System.out.println("***********Display Query**************");
foo = (Element)root.elementIterator(root_query).next();
List<QueryEntity> parent = new ArrayList<QueryEntity>();
for (Iterator i = foo.elementIterator(node_query); i.hasNext();) {
element = (Element) i.next();
if(tag_bug == 1)
System.out.println("id:" + element.attribute("id").getValue());
QueryEntity qe = new QueryEntity();
qe.setName(element.attribute(id).getValue());
//***********
// qe.setOption(true);
String str = element.elementText(option);
if(str != null && str.trim().toLowerCase().compareTo("t") == 0)
qe.setOption(true);
else
qe.setOption(false);
//*******
parent.add(qe);
}
if(tag_bug == 1)
System.out.println("***********Display Query**************");
return parent;
}
public List<ITreeEntry> addEntity(Element root, short tag){
Element foo, element;
String nameRoot, nameNode;
ITreeEntry te;
switch(tag){
case 1:
nameRoot = root_activity;
nameNode = node_activity;
break;
case 2:
nameRoot = root_task;
nameNode = node_task;
break;
case 3:
nameRoot = root_step;
nameNode = node_step;
break;
default:
return null;
}
if(tag_bug == 1){
System.out.println("***********Display**************");
System.out.println("tag = " + tag);
System.out.println("root = " + root.elementText("name"));
}
if(root.elementIterator(nameRoot).hasNext() == false)
return null;
foo = (Element)root.elementIterator(nameRoot).next();
List<ITreeEntry> parent = new ArrayList<ITreeEntry>();
List<ITreeEntry> children = new ArrayList<ITreeEntry>();
tag++;
for (Iterator i = foo.elementIterator(nameNode); i.hasNext();) {
element = (Element) i.next();
if(tag_bug == 1){
System.out.println("name:" + element.elementText("name"));
System.out.println("description:" + element.elementText("description"));
}
switch(tag){
case 2:
te = new ActivityEntity();
break;
case 3:
te = new TaskEntity();
break;
case 4:
te = new StepEntity();
break;
default:
continue;
}
te.setName(element.elementText("name"));
te.setDescription(element.elementText("description"));
children = this.addEntity(element, tag);
if(tag_bug == 1 && children != null){
System.out.println("children = " + children.size());
}
if(children == null){
//System.out.println("tag = " + tag);
if(element.elementText("name").compareTo("send out the survey") == 0)
System.out.println(" ");
List<QueryEntity> querylist = this.addQueryEntity(element);
if(querylist != null){
te.setChildren(querylist);
}
parent.add(te);
}
else{
te.setChildren(children);
parent.add(te);
}
}
if(tag_bug == 1){
System.out.println("tag = " + tag);
System.out.println("***********End**************");
}
//parent = this.addQueryEntity(root, parent);
List<QueryEntity> querylist = this.addQueryEntity(root);
if(querylist != null){
for(int i=0; i<querylist.size(); i++){
parent.add(querylist.get(i));
}
}
return parent;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?