日期:2014-05-20  浏览次数:20952 次

改怎么拼成一个xml形式的字符串,在线等
数据库已经取出值,改怎么拼成一个xml形式的字符串

现在数据库已经取出a b c三个字段的所有值

大概是这样  

a b c

a1 b1 3
a2 b2 1
a3 b3 2
a4 b4 1
a5 b5 3
a6 b6 1
a7 b7 2
a8 b8 2
a9 b9 1
a10 b10 3

xml:
<response>
  <catolg name='1'>
  <layer id='a2' counlom='b3'/>
  <layer id='a6' counlom='b6'/>
  <layer id='a9' counlom='b9'/>
  </catolg>
  <catolg name='2'>
  ...
  ...
  </catolg>
  <catolg name='3'>
  ...
  ...
  </catolg>
</response>

拼成串也可以,使用jdom4j建树最好

while(rs.next){......}

------解决方案--------------------
Java code

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("response");

        while(rs.next()){
            String id= rs.getString(0);            //字段a
            String counlom= rs.getString(1);    //字段b
            String catolg = rs.getString(2);    //字段c
            
            Element catolgEl = ((Element)document.selectSingleNode("//response/catolg[@name='"+catolg+"']"));
            if(catolgEl==null){
                catolgEl = root.addElement("catolg").addAttribute("name", catolg);
            }
            catolgEl.addElement("layer").addAttribute("id", id).addAttribute("counlom", counlom);
            
        }
        
        System.out.println(document.asXML());

------解决方案--------------------
这个不是例子,而是根据楼主要求写的。
楼主可以运行改下,只要把取数据那段换成你从数据库取就可以了。
用的就是dom4j


Java code

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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 TestXML {

    class Data {
        public Data() {
        }
        String catolg;
        String id;
        String counlom;
    }
    
    public static void main(String[] args) {
        new TestXML().output("D:\\response.xml");
    }

    public void output(String fileName) {
        String charset = "UTF-8";
        Document doc = DocumentHelper.createDocument();
        Element response = doc.addElement("response"); // 根元素 response
        doc.setRootElement(response);

        List<String> list = new ArrayList<String>();
        list.add("a1 b1 3");
        list.add("a2 b2 1");
        list.add("a3 b3 2");
        list.add("a4 b4 1");
        list.add("a6 b6 1");
        list.add("a7 b7 2");
        list.add("a8 b8 2");
        list.add("a9 b9 1");
        list.add("a10 b10 3");

        //排序  保证 catolg name="1" 然后 "2" "3"
        Map<String, ArrayList<Data>> sortedMap = new TreeMap<String, ArrayList<Data>>();        
        for (String line : list) {
            String arr[] = line.split(" ");
            Data d = new Data();
            d.catolg = arr[2];
            d.id = arr[0];
            d.counlom = arr[1];
            
            ArrayList<Data> l = sortedMap.get(d.catolg);
            if (l == null) {
                l = new ArrayList<Data>();
                sortedMap.put(d.catolg, l);
            }            
            l.add(d);
        }
                        
        Map<String, Element> nameMap = new TreeMap<String, Element>();
        for (ArrayList<Data> datas : sortedMap.values()) {
            for (Data d : datas) {
                String catolg = d.catolg; 
                String id = d.id;
                String counlom = d.counlom;
    
                Element catolgElement = nameMap.get(catolg);
                if (catolgElement == null) {    
                    catolgElement = response.addElement("catolg");
                    catolgElement.addAttribute("name", catolg);
                    nameMap.put(catolg, catolgElement);
                }
    
                Element layer = catolgElement.addElement("layer");
                layer.addAttribute("id", id);
                layer.addAttribute("counlom", counlom);
            }
        }        

        OutputFormat format = OutputFormat.createPrettyPrint(); // 设置XML文档输出格式
        format.setEncoding(charset); // 设置XML文档的编码类型
        format.setSuppressDeclaration(true);
        format.setIndent(true); // 设置是否缩进
        format.setIndent("\t"); // 以空格方式实现缩进
        format.setNewlines(true); // 设置是否换行
        // format.setExpandEmptyElements(true); //设置空元素是头和尾都写
        try {
            XMLWriter xmlWriter = new XMLWriter(new OutputStreamWriter(
                    new FileOutputStream(fileName), charset), format);
            xmlWriter.write(response);
            xmlWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}