少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

//JDOM xml的生成
import java.io.ByteArrayOutputStream;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;


public class CreateJdom {
 public static String createXml(){
  Element root=new Element("cupMobiles");
  Document doc=new Document(root);
  
  Element cupMobile=new Element("cupMobile");
  cupMobile.setAttribute("application", "UPNoCard");
  cupMobile.setAttribute("version", "1.01");
  root.addContent(cupMobile);
  
  Element transaction=new Element("transaction");
  transaction.setAttribute("type","Purchase.PMReq");
  cupMobile.addContent(transaction);
  
  Element submitTime=new Element("submitTime");
  submitTime.setText("20111207111641");
  transaction.addContent(submitTime);
  
  Element order=new Element("order");
  order.setAttribute("id", "12347733");
  transaction.addContent(order);
  
  Element merchant=new Element("merchant");
  merchant.setAttribute("id","303290047228001");
  transaction.addContent(merchant);
  
  Element accountNumber1=new Element("accountNumber1");
  accountNumber1.setText("6224130665233518");
  transaction.addContent(accountNumber1);
  
  Element transSerialNumber=new Element("transSerialNumber");
  transSerialNumber.setText("201162");
  transaction.addContent(transSerialNumber);
  
  Element billAmount =new Element("billAmount");
  billAmount.setAttribute("currency","156");
  billAmount.setText("000000030231");
  transaction.addContent(billAmount);
  
  Element settleDate=new Element("settleDate");
  settleDate.setText("20111208");
  transaction.addContent(settleDate);
  
  ByteArrayOutputStream byteRsp=new ByteArrayOutputStream();
  XMLOutputter xmlOut=new XMLOutputter();
  try {
   xmlOut.output(doc, byteRsp);
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println(e.getMessage());
  }
  return byteRsp.toString();
 }
 
 
 public static void main(String[] args) {
  
 }
}



//JDOM xml的生成的测试代码
import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CreateJdomTest extends TestCase{
 @Before
 public void setUp(){}
 @After
 public void tearDown(){}
 @Test
 public void testCreateJdom(){
  String result=new CreateJdom().createXml();
  System.out.println(result);
 }
}






//JDOM xml的解析以及解析的测试代码
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 {
 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();
//   map.put(root.getName(), root.getValue());
//   System.out.println("root=" + root.getName());
//   System.out.println("root,s value=" + root.getValue());
   
   Element cupMobile = null;
   cupMobile = root.getChild("cupMobile");
//   map.put(cupMobile.getName(), cupMobile.getValue());
   String application=cupMobile.getAttributeValue("application");
   map.put("application", application);
   String version=cupMobile.getAttributeValue("version");
   map.put("version", version);

   
   Element transaction = null;
   transaction = cupMobile.getChild("transaction");
//   map.put(transaction.getName(), transaction.getValue());
   String type=transaction.getAttributeValue("type");
   map.put("type", type);

   Element submitTime = null;
   submitTime = transaction.getChild("submitTime");
   map.put(submitTime.getName(), submitTime.getValue());
   
   Element order  = null;
   order = transaction.getChild("order");
//   map.put(order.getName(), order.getValue());
   String orderId=order.getAttributeValue("id");
   map.put("orderId", orderId);

   Element merchant  = null;
   merchant = transaction.getChild("merchant");
//   map.put(merchant.getName(), merchant.getValue());
   String merchantId=merchant.getAttributeValue("id");
   map.put("merchantId", merchantId);

   Element accountNumber1  = null;
   accountNumber1 = transaction.getChild("accountNumber1");
   map.put(accountNumber1.getName(), accountNumber1.getValue());
   
   Element transSerialNumber  = null;
   transSerialNumber = transaction.getChild("transSerialNumber");
   map.put(transSerialNumber.getName(), transSerialNumber.getValue());
   
   Element billAmount  = null;
   billAmount = transaction.getChild("billAmount");
   map.put(billAmount.getName(), billAmount.getValue());
   String currency=billAmount.getAttributeValue("currency");
   map.put("currency", currency);
   
   Element settleDate  = null;
   settleDate = transaction.getChild("settleDate");
   map.put(settleDate.getName(), settleDate.getValue());
   
   
   
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println(e.getMessage());
  }
  return map;
 }

 public static void main(String[] args) {
  String xml = new CreateJdom().createXml();
  Map<String, String> map = new ParseJdom().parseXml(xml);
  Iterator it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = (Map.Entry<String, String>) it
     .next();
   System.out.println("key=" + entry.getKey());
   System.out.println("Value=" + entry.getValue());
  }

 }
}


 

posted on 2012-07-20 17:37 abin 阅读(2073) 评论(0)  编辑  收藏 所属分类: JDOM

只有注册用户登录后才能发表评论。


网站导航: