少年阿賓

          那些青春的歲月

            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 閱讀(2081) 評論(0)  編輯  收藏 所屬分類: JDOM
          主站蜘蛛池模板: 琼海市| 通城县| 禄丰县| 营口市| 怀安县| 西昌市| 浮山县| 巴东县| 平定县| 定南县| 边坝县| 鄂温| 客服| 宣化县| 舒兰市| 阿拉善右旗| 贵德县| 乐东| 泽库县| 历史| 关岭| 吴堡县| 文山县| 镇平县| 兰考县| 嫩江县| 丰台区| 梧州市| 莱州市| 怀柔区| 麻江县| 胶南市| 靖州| 莱芜市| 高邑县| 唐河县| 双桥区| 文安县| 仪征市| 郓城县| 江达县|