62,610
社区成员
发帖
与我相关
我的任务
分享
public String getTree(String condition) {
String result = "<XML>";
List data = this.query(condition, "0");
for (int i = 0; i < data.size(); i++) {
KrSpDataDictionary krSpDataDictionary = (KrSpDataDictionary) data
.get(i);
String _regexp = "(.*?)(<node id='" + krSpDataDictionary.getParentNode()
+ ".*?>)(.*?)(</" + krSpDataDictionary.getParentNode()
+ "node>)(.*?)";
String nodes = "<node id='" + krSpDataDictionary.getNode()
+ "' name='" + krSpDataDictionary.getName() + "'></"
+ krSpDataDictionary.getNode() + "node>";
Pattern p = Pattern.compile(_regexp);
Matcher m = p.matcher(result);
boolean b = m.matches();
if (b) {
result = result.replaceAll(_regexp, "$1$2$3"+nodes+"$4$5");
} else {
result += nodes;
}
}
result += "</XML>";
result = result.replaceAll("</.*?node>", "</node>");
return result;
}
package org.quubox.com;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class ListToXML {
//对外方法
public String list2xml(List<KrSysModule> list) throws Exception, IOException {
String xml = "";
if(list != null) {
//第一步,进行List排序,用来保证要创建的节点的父节点事先已经创建了。(比较关键)
this.sortKrSysModules(list);
//第二步,根据list生成XML
Document doc = this.buildXMLByList(list);
//第三步,生成字符串
xml = this.buildXMLString(doc);
}
return xml;
}
//生成字符串
private String buildXMLString(Document doc) throws IOException {
if(doc != null) {
Format format = Format.getPrettyFormat();
java.util.Properties sysProp = new java.util.Properties(System.getProperties());
String sysFileEncoding = sysProp.getProperty("file.encoding");//得到操作系统的编码
format.setEncoding(sysFileEncoding);//设置编码格式
XMLOutputter xmlout = new XMLOutputter(format);
ByteArrayOutputStream out = new ByteArrayOutputStream();
String xmlStr = null;
xmlout.output(doc,out);
xmlStr = out.toString();//将xml内容转化为字符串
return xmlStr;
}
return "";
}
//进行排序
private void sortKrSysModules(List<KrSysModule> list) {
if(list != null) {
Collections.sort(list, new Comparator() {
public int compare(Object o1,Object o2) {
KrSysModule kr1=(KrSysModule)o1;
KrSysModule kr2=(KrSysModule)o2;
return kr1.getParentNode().compareTo(kr2.getParentNode());
}
});
}
}
//通过List构建Document
private Document buildXMLByList(List<KrSysModule> list) throws JDOMException {
Element xml = new Element("xml");
Document doc = new Document(xml);
if(list != null) {
for(KrSysModule m : list) {
if(m.getParentNode().length()>0) {//有父节点
Element pNode = (Element) XPath.selectSingleNode(doc, "//node[@id='"+m.getParentNode()+"']");
if(pNode != null) {
Element sNode = new Element("node");
sNode.setAttribute("id", m.getNode());
sNode.setAttribute("name", m.getName());
pNode.addContent(sNode);
}
}else {//根节点
Element root = new Element("node");
root.setAttribute("id", m.getNode());
root.setAttribute("name", m.getName());
xml.addContent(root);
}
}
}
return doc;
}
//测试主方法
public static void main(String[] args) {
//生成测试数据
KrSysModule module0 = new KrSysModule();
module0.setNode("0");
module0.setName("根节点");
module0.setParentNode("");
KrSysModule module1 = new KrSysModule();
module1.setNode("0001");
module1.setName("节点1");
module1.setParentNode("0");
KrSysModule module2 = new KrSysModule();
module2.setNode("0001001");
module2.setName("节点11");
module2.setParentNode("0001");
KrSysModule module3 = new KrSysModule();
module3.setNode("0002");
module3.setName("节点2");
module3.setParentNode("0");
List<KrSysModule> list = new ArrayList<KrSysModule>();
list.add(module0);
list.add(module1);
list.add(module2);
list.add(module3);
//生成xml
ListToXML listToXml = new ListToXML();
try {
System.out.println(listToXml.list2xml(list));;
} catch (Exception e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<node id="0" name="根节点">
<node id="0001" name="节点1">
<node id="0001001" name="节点11" />
</node>
<node id="0002" name="节点2" />
</node>
</xml>
public class KrSysModule {
private String node ;
private String name;
private String parentNode;
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentNode() {
return parentNode;
}
public void setParentNode(String parentNode) {
this.parentNode = parentNode;
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class ListToXML {
//对外方法
public void list2xml(List<KrSysModule> list) throws Exception, IOException {
if(list != null) {
//第一步,进行List排序,用来保证要创建的节点的父节点事先已经创建了。(比较关键)
this.sortKrSysModules(list);
//第二步,根据list生成XML
Document doc = this.buildXMLByList(list);
//写出文件
XMLOutputter XMLOut = new XMLOutputter();
XMLOut.output(doc, new FileOutputStream("c:/aa.xml"));
}
}
//进行排序
private void sortKrSysModules(List<KrSysModule> list) {
if(list != null) {
Collections.sort(list, new Comparator() {
public int compare(Object o1,Object o2) {
KrSysModule kr1=(KrSysModule)o1;
KrSysModule kr2=(KrSysModule)o2;
return kr1.getParentNode().compareTo(kr2.getParentNode());
}
});
}
}
//通过List构建Document
private Document buildXMLByList(List<KrSysModule> list) throws JDOMException {
Element xml = new Element("xml");
Document doc = new Document(xml);
if(list != null) {
for(KrSysModule m : list) {
if(m.getParentNode().length()>0) {//有父节点
Element pNode = (Element) XPath.selectSingleNode(doc, "//node[@id='"+m.getParentNode()+"']");
if(pNode != null) {
Element sNode = new Element("node");
sNode.setAttribute("id", m.getNode());
sNode.setAttribute("name", m.getName());
pNode.addContent(sNode);
}
}else {//根节点
Element root = new Element("node");
root.setAttribute("id", m.getNode());
root.setAttribute("name", m.getName());
xml.addContent(root);
}
}
}
return doc;
}
//测试主方法
public static void main(String[] args) {
//生成测试数据
KrSysModule module0 = new KrSysModule();
module0.setNode("0");
module0.setName("根节点");
module0.setParentNode("");
KrSysModule module1 = new KrSysModule();
module1.setNode("0001");
module1.setName("节点1");
module1.setParentNode("0");
KrSysModule module2 = new KrSysModule();
module2.setNode("0001001");
module2.setName("节点11");
module2.setParentNode("0001");
KrSysModule module3 = new KrSysModule();
module3.setNode("0002");
module3.setName("节点2");
module3.setParentNode("0");
List<KrSysModule> list = new ArrayList<KrSysModule>();
list.add(module0);
list.add(module1);
list.add(module2);
list.add(module3);
//生成xml
ListToXML listToXml = new ListToXML();
try {
System.out.println("开始生成文件");
listToXml.list2xml(list);
System.out.println("生成文件完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String iterateNode(Node parent,int level){
StringBuilder builder = new StringBuilder();
int spaceNum = level*4;
for(int i=1;i<=spaceNum;i++){
builder.append(' ');
}
builder.append("<node name=\""+parent.name+"\">\n");
List<Node> childList = parent.childNodes;
int childSize = childList.size();
for(int i=0;i<childSize;i++){
Node childNode = childList.get(i);
builder.append(iterateNode(childNode,level+1));
}
builder.append("\n");
for(int i=1;i<=spaceNum;i++){
builder.append(' ');
}
builder.append("</node>\n");
return builder.toString();
}
}
class KrSysModule{
private String node ;
private String name;
private String parentNode;
public KrSysModule(String node,String name,String parentNode){
this.node = node;
this.name = name;
this.parentNode = parentNode;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getParentNode() {
return parentNode;
}
public void setParentNode(String parentNode) {
this.parentNode = parentNode;
}
}
class Node{
Node parentNode;
List<Node> childNodes = new ArrayList<Node>();
String name;
public Node(String name){
this.name = name;
}
public void addChild(Node childNode){
childNodes.add(childNode);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ConvertPlaneToTree {
public static void main(String[] args){
ArrayList<KrSysModule> moduleList = new ArrayList<KrSysModule>();
moduleList.add(new KrSysModule("0","根节点",null));
moduleList.add(new KrSysModule("0001","节点1","0"));
moduleList.add(new KrSysModule("0001001"," 节点11","0001"));
moduleList.add(new KrSysModule("0002","节点2","0"));
HashMap<String,Node> idNodeMap = new HashMap<String,Node>();
int size = moduleList.size();
for(int i=0;i<size;i++){
KrSysModule tempModule = moduleList.get(i);
Node tempNode = new Node(tempModule.getName());
idNodeMap.put(tempModule.getNode(), tempNode);
}
Node root = null;
for(int i=0;i<size;i++){
KrSysModule tempModule = moduleList.get(i);
String parentName = tempModule.getParentNode();
if(parentName != null){
Node parentNode = idNodeMap.get(parentName);
String curName = tempModule.getNode();
Node curNode = idNodeMap.get(curName);
parentNode.childNodes.add(curNode);
curNode.parentNode = parentNode;
}else{
String curName = tempModule.getNode();
root = idNodeMap.get(curName);
}
}
System.out.println(iterateNode(root,1));
}
import java.util.ArrayList;
import java.util.List;
public class Node {
private String id;
private String name;
private Node parentNode;
List childList = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getParentNode() {
return parentNode;
}
public void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public void addChild(Node child){
child.setParentNode(this);
childList.add(child);
}
public List getChildList(){
return childList;
}
}
import java.util.Iterator;
import java.util.List;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
Node root = new Node();
root.setId("root");
root.setName("根节点");
root.setParentNode(null);
for(int i=0;i<10;i++){
Node node = new Node();
node.setId("0");
node.setName("node "+i);
root.addChild(node);
for(int j=0;j<10;j++){
Node node0 = new Node();
node0.setId(""+i+j);
node0.setName("node "+i+j);
node0.setParentNode(node);
node.addChild(node0);
}
}
//输出生成XML文档。
System.out.println("<XML>");
print(root);
System.out.println("</XML>");
}
public static void print(Node node){
System.out.println("<node name="+node.getName()+">");
List child = node.getChildList();
if(!child.isEmpty()){
Iterator it = child.iterator();
while(it.hasNext()){
print((Node)it.next());
}
}
System.out.println("</node>");
}
}