java jdom2_JDOM 生成和解析XML(二)

本文介绍了如何使用JDOM库在Java中创建XML文档,包括元素的添加、属性设置以及XML输出,并提供了详细的创建和解析XML的代码示例。通过CreateJdom类创建XML,ParseJdom类解析XML并存储数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//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 parseXml(String xml) {

Map map = Collections

.synchronizedMap(new HashMap());

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 map = new ParseJdom().parseXml(xml);

Iterator it = map.entrySet().iterator();

while (it.hasNext()) {

Map.Entry entry = (Map.Entry) it

.next();

System.out.println("key=" + entry.getKey());

System.out.println("Value=" + entry.getValue());

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值