public String getInfo(String contactId){
System.out.println("在Cmd中");
DataSet ds= contactList.getInfo(contactId);
DataSet ds2 = contactList.getOthersContactAddr(contactId);
DataSet ds3 = contactList.getNickName(contactId);
User userInfo = (User)ds.toListBean(User.class).get(0);
List<ContactAddr> addrList= new ArrayList<ContactAddr>();
addrList =ds2.toListBean(ContactAddr.class);
ContactList contactList=(ContactList)ds3.toListBean(ContactList.class).get(0);
String birthday=userInfo.getBirthday();
String yy=null,mm=null,dd=null;
yy=birthday.substring(0,4);
mm=birthday.substring(4,6);
dd=birthday.substring(6);
String xmlStr=null;
try {
DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = dFact.newDocumentBuilder();
Document doc = build.newDocument();
Element root = doc.createElement("userinfo");
doc.appendChild(root);
Element social = doc.createElement("social");
social.setAttribute("userId",userInfo.getUserId());
social.setAttribute("userName",userInfo.getUserName());
social.setAttribute("nickName",contactList.getNickName());
social.setAttribute("gender", userInfo.getGender());
social.setAttribute("yy", yy);
social.setAttribute("mm", mm);
social.setAttribute("dd", dd);
root.appendChild(social);
//System.out.println(addrList.size());
for(int i=0;i<addrList.size();i++){
Element social2 = doc.createElement("address");
social2.setAttribute("addrId",addrList.get(i).getAddrId());
social2.setAttribute("addrType", addrList.get(i).getDeliveryMode());
social2.setAttribute("addr", addrList.get(i).getDeliveryAddr());
social.appendChild(social2);
}
xmlStr = BeanUtil.getDomXml(doc);
}catch (ParserConfigurationException ex) {
System.out.println("Error building document");
} catch(Exception e){
e.printStackTrace();
}
System.out.println(xmlStr);
return xmlStr;
}
package org.loushang.live.common;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Node;
public class BeanUtil {
public static String getDomXml(Node doc) {
String xmlStr = null;
try {
// 以 Document Object Model(DOM)树的形式充当转换 Source 树的持有者
DOMSource source = new DOMSource(doc);
//用来生成XML文件
// 要生成文件需构造PrintWriter的writer,
//DOM中这种方式name的值没有写进去,由于编码的问题
// PrintWriter writerXml = new PrintWriter(new FileOutputStream ("city-dom.xml"));
//用OutputStreamWriter加了编码就OK了
PrintWriter writerXml = new PrintWriter(new OutputStreamWriter(new FileOutputStream("city-jdom.xml"),"utf-8"));
Result resultXml = new StreamResult(writerXml);
// 实现此接口的对象包含构建转换结果树所需的信息
// Result resultXml = new StreamResult(new FileOutputStream("city-dom.xml"));
//用来得到XML字符串形式
// 一个字符流,可以用其回收在字符串缓冲区中的输出来构造字符串。
StringWriter writerStr = new StringWriter();
Result resultStr = new StreamResult(writerStr);
// 此抽象类的实例能够将源树转换为结果树。
Transformer transformer = TransformerFactory.newInstance().newTransformer();
// 设置编码
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
// 是否缩进
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// 将 XML Source 转换为 Result
transformer.transform(source, resultXml);
transformer.transform(source, resultStr);
//获取XML字符串
xmlStr = writerStr.getBuffer().toString();
} catch (TransformerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return xmlStr;
}
}