锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2錛塉DOM杈撳嚭UTF-8鏂囦歡涔辯爜鐨勬牴鏈師鍥犲茍闈炲湪JDOMAPI錛岃屾槸鍦↗DK銆?
鍏蜂綋鎻忚堪錛?
JDOM鐨勮緭鍑虹被XMLOutputter鏈変袱涓猳utput鎺ュ彛錛岄櫎浜?jiǎn)閮藉呬h湁涓涓狣ocument鍙傛暟澶栵紝鍒嗗埆鎺ュ彈Writer鍜?OutputStream鍙傛暟銆?
榪欑粰鎴戜滑涓涓敊瑙夛紝涓や釜鎺ュ彛鍙互浠繪剰浣跨敤銆?
棣栧厛鎴戜滑鐢╫utput(doc,System.out)鏉ュ仛嫻嬭瘯錛屾鏃跺緱鍒頒貢鐮侊紝
鐒跺悗鎴戜滑鏀逛負(fù)output(doc,new PrintWriter(System.out))鏉ユ祴璇曪紝杈撳嚭涓嶆槸涔辯爜錛?
涔熷氨鏄鍦ㄦ帶鍒跺彴鐨勬椂鍊欎竴瀹氳鐢ㄤ竴涓猈riter鎺ュ彛鍖呰涓涓嬨?
鐒跺悗鎴戜滑鐢╫utput(doc,new FileWriter(path))鏉ュ仛嫻嬭瘯錛岀粨鏋滃嵈寰楀埌涔辯爜錛?
鐒跺悗鎴戜滑鏀逛負(fù)output(doc,new FileOutputStream(path))鏉ユ祴璇曪紝杈撳嚭涓嶆槸涔辯爜錛?
涔熷氨鏄鍦ㄨ緭鍑烘枃浠剁殑鏃跺欎竴瀹氳鐢ㄤ竴涓狾utputStream鎺ュ彛鍖呰涓涓嬨?
鐤簡(jiǎn)鍚э紵鍛靛懙錛屽緢鎼炵瑧鏄惂銆傜粡榪囧埌JDOM鐨勬簮鐮佷腑璋冭瘯錛屽彂鐜版病鏈変換浣曢棶棰橈紝闂鍑哄湪浜?jiǎn)JDK閲岄潰銆?
JDK鍐呯殑瀵瑰簲鎺ュ彛澶勭悊錛?
1錛塒rintWriter綾繪湁鍙傛暟涓篛utputStream鐨勬瀯閫犳柟娉曪紝鍥犳鍙互浠嶴ystem.out鍖呰鍒癙rintWriter
2錛塅ileWriter綾繪病鏈夊弬鏁頒負(fù)OutputStream鐨勬瀯閫犳柟娉曪紝鍥犳涓嶈兘浠嶧ileOutputStream鍖呰鍒?FileWriter
3錛夊鏋淧rintWriter綾葷敤浜?jiǎn)鍙傛曨C負(fù)Writer鐨勬瀯閫犳柟娉曪紙Writer瀹炵幇涓篎ileWriter錛夛紝鏈鍚庤緭鍑轟篃鏄貢鐮?
4錛夊鏋滅敤涓涓狥ileOutputStream鏉ュ寘瑁呬竴涓帶鍒跺彴杈撳嚭錛屼篃鏄貢鐮?
鍥犳錛屽浜嶫DK鍐呯殑鍚勭杈撳嚭浣撶郴錛屽悇縐岻nputStream銆丱utputStream銆乺eader鍜寃riter瑕佸厖鍒嗚璇嗭紝鍚﹀垯鏋佸鏄撳嚭鐜頒竴浜涙剰鎯充笉鍒扮殑闂銆?
]]>
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鐨勭敓鎴愮殑嫻嬭瘯浠g爜
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鐨勮В鏋愪互鍙?qiáng)瑙f瀽鐨剫箣璇曚唬鐮?br />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());
}
}
}
import java.io.ByteArrayOutputStream;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
public class CreateJdom {
/**
* 鐢熸垚鏀粯瀹濋渶瑕佺殑XML鏍煎紡錛岃嚜宸辨ā鎷熸祴璇曟敮浠樻帴鍙g殑寮傛榪斿洖緇撴灉
* @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 {
/**
* 瑙f瀽XMl閲岄潰鐨勬瘡涓瓧孌?br /> * @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());
}
}
}