public class XMLUtil {
//一层解析
public static Document map2xml(Map<String, Object> map) {
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
for(String k:map.keySet()){
Element node = xml.addElement(k);
if(!"body".equals(k)){
CDATA cdata = DocumentHelper.createCDATA(String.valueOf(map.get(k)));
node.add(cdata);
}else{
String t =String.valueOf( map.get(k) );
CDATA cdata = DocumentHelper.createCDATA(t);
node.add(cdata);
// Text text = DocumentHelper.createText(t);
// node.addText(t );
}
}
return document;
}
//一层解析
public static Document map2xml2(Map<String, Object> map) {
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
for(String k:map.keySet()){
Element node = xml.addElement(k);
String t =String.valueOf( map.get(k) );
Text text = DocumentHelper.createText(t);
node.addText(t);
}
return document;
}
//一层解析
public static Map<String,String> xml2map(Document d){
Map<String,String> map = new HashMap<String, String>();
Element root = d.getRootElement();
List<Element> list = root.elements();
for(Element ele : list){
String name = ele.getName();
String text = ele.getText();
map.put(name, text);
}
return map;
}
//一层解析
public static SortedMap<String,String> xml2smap(Document d){
SortedMap<String,String> map = new TreeMap<String, String>();
Element root = d.getRootElement();
List<Element> list = root.elements();
for(Element ele : list){
String name = ele.getName();
String text = ele.getText();
map.put(name, text);
}
return map;
}
//从输入流中以xml形式读取数据
public static Document readXml4InputStream(InputStream is) throws DocumentException{
Document d =null;
SAXReader sreader = new SAXReader();
d = sreader.read(is);
return d;
}
//将xml数据写到输出流流中
public static void writeXml2OutputStream(Document d,OutputStream os,boolean needDeclaration) throws DocumentException, IOException{
// //直接流处理
// PrintWriter pw = new PrintWriter(os);
// pw.print(xml);
// pw.close();
//去除声明
OutputFormat format = new OutputFormat();
format.setSuppressDeclaration(needDeclaration); //是否需要声明
XMLWriter out = new XMLWriter(os,format);
out.write(d);
out.flush();
out.close();
}
public static void main(String[] args) throws IOException, DocumentException {
System.out.println(MD5Util.MD5("中文"));
}
}
//一层解析
public static Document map2xml(Map<String, Object> map) {
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
for(String k:map.keySet()){
Element node = xml.addElement(k);
if(!"body".equals(k)){
CDATA cdata = DocumentHelper.createCDATA(String.valueOf(map.get(k)));
node.add(cdata);
}else{
String t =String.valueOf( map.get(k) );
CDATA cdata = DocumentHelper.createCDATA(t);
node.add(cdata);
// Text text = DocumentHelper.createText(t);
// node.addText(t );
}
}
return document;
}
//一层解析
public static Document map2xml2(Map<String, Object> map) {
Document document = DocumentHelper.createDocument();
Element xml = document.addElement("xml");
for(String k:map.keySet()){
Element node = xml.addElement(k);
String t =String.valueOf( map.get(k) );
Text text = DocumentHelper.createText(t);
node.addText(t);
}
return document;
}
//一层解析
public static Map<String,String> xml2map(Document d){
Map<String,String> map = new HashMap<String, String>();
Element root = d.getRootElement();
List<Element> list = root.elements();
for(Element ele : list){
String name = ele.getName();
String text = ele.getText();
map.put(name, text);
}
return map;
}
//一层解析
public static SortedMap<String,String> xml2smap(Document d){
SortedMap<String,String> map = new TreeMap<String, String>();
Element root = d.getRootElement();
List<Element> list = root.elements();
for(Element ele : list){
String name = ele.getName();
String text = ele.getText();
map.put(name, text);
}
return map;
}
//从输入流中以xml形式读取数据
public static Document readXml4InputStream(InputStream is) throws DocumentException{
Document d =null;
SAXReader sreader = new SAXReader();
d = sreader.read(is);
return d;
}
//将xml数据写到输出流流中
public static void writeXml2OutputStream(Document d,OutputStream os,boolean needDeclaration) throws DocumentException, IOException{
// //直接流处理
// PrintWriter pw = new PrintWriter(os);
// pw.print(xml);
// pw.close();
//去除声明
OutputFormat format = new OutputFormat();
format.setSuppressDeclaration(needDeclaration); //是否需要声明
XMLWriter out = new XMLWriter(os,format);
out.write(d);
out.flush();
out.close();
}
public static void main(String[] args) throws IOException, DocumentException {
System.out.println(MD5Util.MD5("中文"));
}
}