package org.litsoft.air.unionpay;
import java.io.ByteArrayOutputStream;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
public class CreateJdom {
/**
* 生成支付宝需要的XML格式,自己模拟测试支付接口的异步返回结果
* @return
*/
public static String createXml(){
Element root=new Element("notify");
Document doc=new Document(root);
Element trade_status=new Element("trade_status");
trade_status.setText("TRADE_FINISHED");
root.addContent(trade_status);
Element total_fee=new Element("total_fee");
total_fee.setText("0.90");
root.addContent(total_fee);
Element subject=new Element("subject");
subject.setText("123465");
root.addContent(subject);
Element out_trade_no=new Element("out_trade_no");
out_trade_no.setText("1118060201-7555");
root.addContent(out_trade_no);
Element notify_reg_time=new Element("notify_reg_time");
notify_reg_time.setText("2010-11-1814:02:43.000");
root.addContent(notify_reg_time);
Element trade_no=new Element("trade_no");
trade_no.setText("2010111800209965");
root.addContent(trade_no);
ByteArrayOutputStream byteRsp=new ByteArrayOutputStream();
XMLOutputter xmlOut=new XMLOutputter();
try {
xmlOut.output(doc, byteRsp);
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
String temp=byteRsp.toString();
String result=temp.substring(temp.indexOf(">")+1, temp.length());
return result.trim();
// return byteRsp.toString();
}
public static void main(String[] args) {
CreateJdom jdom=new CreateJdom();
String result=jdom.createXml();
System.out.println(result);
}
}
package org.litsoft.air.unionpay;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;
public class ParseJdom {
/**
* 解析XMl里面的每个字段
* @param xml
* @return
*/
public static Map<String,String> parseXml(String xml){
Map<String,String> map=Collections.synchronizedMap(new HashMap<String,String>());
StringReader reader=new StringReader(xml);
InputSource source=new InputSource(reader);
SAXBuilder sax=new SAXBuilder();
try {
Document doc=sax.build(source);
Element root=doc.getRootElement();
System.out.println("root,s name="+root.getName());
System.out.println("root,s value="+root.getValue());
List node=root.getChildren();
Element element=null;
for(int i=0;i<node.size();i++){
element=(Element)node.get(i);
map.put(element.getName(), element.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public static void main(String[] args) {
String xmlHeader="<"+"?"+"xml"+" version="+"\"1.0\""+" encoding="+"\"UTF-8\""+"?"+">";
CreateJdom jdom=new CreateJdom();
String result=jdom.createXml();
System.out.println("xmlHeader+result="+xmlHeader+result);
Map<String,String> map=new ParseJdom().parseXml(xmlHeader+result);
Iterator it=map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry=(Map.Entry<String, String>)it.next();
System.out.println(entry.getKey()+" , "+entry.getValue());
}
}
}