锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
聽聽聽 鍦?JDOM 涓紝XML 鍏冪礌灝辨槸 Element 鐨勫疄渚嬶紝XML 灞炴у氨鏄?Attribute 鐨勫疄渚嬶紝XML 鏂囨。鏈韓灝辨槸 Document 鐨勫疄渚嬨?br />聽聽聽 鍥犱負 JDOM 瀵硅薄灝辨槸鍍?Document銆丒lement 鍜?Attribute 榪欎簺綾葷殑鐩存帴瀹炰緥錛屽洜姝ゅ垱寤轟竴涓柊 JDOM 瀵硅薄灝卞鍦?Java 璇█涓嬌鐢?new 鎿嶄綔絎︿竴鏍峰鏄撱侸DOM 鐨勪嬌鐢ㄦ槸鐩存埅浜嗗綋鐨勩?br />聽聽聽 JDOM 浣跨敤鏍囧噯鐨?Java 緙栫爜妯″紡銆傚彧瑕佹湁鍙兘錛屽畠浣跨敤 Java new 鎿嶄綔絎﹁屼笉鏁呭紕鐜勮櫄浣跨敤澶嶆潅鐨勫伐鍘傚寲妯″紡錛屼嬌瀵硅薄鎿嶄綔鍗充究瀵逛簬鍒濆鐢ㄦ埛涔熷緢鏂逛究銆?br />聽聽聽
聽聽聽 鏈枃鍒嗕袱姝ュJDOM鐨勫簲鐢ㄥ姞浠ヤ粙緇嶏細XML鍒涘緩 鍜?XML瑙f瀽
涓銆乆ML鏂囨。鍒涘緩
聽聽聽 鎴戜滑鐢遍浂寮濮嬪埄鐢↗DOM鐢熸垚涓涓猉ML鏂囨。銆傛渶鍚庣殑緇撴灉(鏍鋒湰鏂囨。)鐪嬭搗鏉ヨ薄榪欐牱錛?br />聽聽聽 <?xml version="1.0" encoding="UTF-8"?>
聽聽聽 <MyInfo comment="introduce myself">
聽聽聽聽聽聽聽 <name>kingwong</name>
聽聽聽聽聽聽聽 <sex value="male"/>
聽聽聽聽聽聽聽 <contact>
聽聽聽聽聽聽聽聽聽聽聽 <telephone>87654321</telephone>
聽聽聽聽聽聽聽 </contact>
聽聽聽 </MyInfo>
聽聽聽 1.浠?MyInfo 涓烘牴鍏冪礌鍒涘緩鏂囨。
聽聽聽聽聽聽聽 Element rootElement = new Element("MyInfo");//鎵鏈夌殑XML鍏冪礌閮芥槸 Element 鐨勫疄渚嬨傛牴鍏冪礌涔熶笉渚嬪錛氾級
聽聽聽聽聽聽聽 Document myDocument = new Document(rootElement);//浠ユ牴鍏冪礌浣滀負鍙傛暟鍒涘緩Document瀵硅薄銆備竴涓狣ocument鍙湁涓涓牴錛屽嵆root鍏冪礌銆?br />聽聽聽 2.緇欐牴鍏冪礌娣誨姞灞炴?br />聽聽聽聽聽聽聽 Attribute rootAttri = new Attribute("comment","introduce myself");//鍒涘緩鍚嶄負 commnet,鍊間負 introduce myself 鐨勫睘鎬с?br />聽聽聽聽聽聽聽 rootElement.setAttribute(rootAttri);//灝嗗垰鍒涘緩鐨勫睘鎬ф坊鍔犲埌鏍瑰厓绱犮?br />聽聽聽聽聽聽聽 榪欎袱琛屼唬鐮佷綘涔熷彲浠ュ悎鎴愪竴琛屾潵鍐欙紝璞¤繖鏍鳳細
聽聽聽聽聽聽聽 rootElement.setAttribute(new Attribute("comment","introduce myself"));
聽聽聽聽聽聽聽 鎴栬?br />聽聽聽聽聽聽聽 rootElement.setAttribute("comment","introduce myself");
聽聽聽 3.娣誨姞鍏冪礌鍜屽瓙鍏冪礌
聽聽聽聽聽聽聽 JDOM閲屽瓙鍏冪礌鏄綔涓?content錛堝唴瀹癸級娣誨姞鍒扮埗鍏冪礌閲岄潰鍘葷殑,鎵璋揷ontent灝辨槸綾諱技涓婇潰鏍鋒湰鏂囨。涓?lt;name></name>涔嬮棿鐨勪笢涓滐紝鍗砶ingwong銆傜綏鍡︿簡鐐規槸鍚э細錛?br />聽聽聽聽聽聽聽 Element nameElement = new Element("name");//鍒涘緩 name 鍏冪礌
聽聽聽聽聽聽聽 nameElement.addContent("kingwong");//灝唊ingwong浣滀負content娣誨姞鍒皀ame鍏冪礌
聽rootElement.addContent(nameElement);//灝唍ame鍏冪礌浣滀負content娣誨姞鍒版牴鍏冪礌
聽
聽榪欎笁琛屼綘涔熷彲浠ュ悎涓轟竴鍙ワ紝璞¤繖鏍鳳細
聽rootElement.addContent((Content)(new Element("name").addContent("kingwong")));//鍥犱負addContent(Content child)鏂規硶榪斿洖鐨勬槸涓涓狿arent鎺ュ彛錛岃孍lement綾誨悓鏃剁戶鎵夸簡Content綾誨拰瀹炵幇浜哖arent鎺ュ彛錛屾墍浠ユ垜浠妸瀹冮犲瀷鎴怌ontent銆?br />聽
聽聽聽聽聽聽聽 鎴戜滑鐢ㄥ悓鏍風殑鏂規硶娣誨姞甯﹀睘鎬х殑瀛愬厓绱?lt;sex value="male"/>
聽聽聽聽聽聽聽 rootElement.addContent(new Element("sex").setAttribute("value","male"));//娉ㄦ剰榪欓噷涓嶉渶瑕佽漿鍨嬶紝鍥犱負addAttribute(String name,String value)榪斿洖鍊煎氨鏄竴涓?Element銆?br />聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 鍚屾牱鐨勶紝鎴戜滑娣誨姞<contract />鍏冪礌鍒版牴鍏冪礌涓嬶紝鐢ㄦ硶涓婁竴鏍鳳紝鍙槸紼嶅井澶嶆潅浜嗕竴浜涳細
聽聽聽聽聽聽聽 rootElement.addContent((Content)(new Element("contact").addContent((Content)(new Element("telephone").addContent("87654321")))));
聽聽聽聽聽聽聽 濡傛灉浣犲榪欑綆鍐欏艦寮忚繕涓嶅お涔犳儻錛屼綘瀹屽叏鍙互鍒嗘鏉ュ仛錛屽氨璞℃湰鑺傚垰寮濮嬬殑鏃跺欎竴鏍楓備簨瀹炰笂濡傛灉灞傛姣旇緝澶氾紝鍐欐垚鍒嗘鐨勫艦寮忔洿娓呮櫚浜涳紝涔熶笉瀹規槗鍑洪敊銆?br />聽聽聽 4.鍒犻櫎瀛愬厓绱?br />聽聽聽聽聽聽聽 榪欎釜鎿嶄綔姣旇緝綆鍗曪細
聽聽聽聽聽聽聽 rootElement.removeChild("sex");//璇ユ柟娉曡繑鍥炰竴涓竷灝斿?br />聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 鍒扮洰鍓嶄負姝紝鎴戜滑瀛︿範浜嗕竴涓婮DOM鏂囨。鐢熸垚鎿嶄綔銆備笂闈㈠緩绔嬩簡涓涓牱鏈枃妗o紝鍙槸鎴戜滑鎬庝箞鐭ラ亾瀵逛笉瀵瑰憿錛熷洜姝ら渶瑕佽緭鍑烘潵鐪嬩竴涓嬨傛垜浠皢JDOM鐢熸垚鐨勬枃妗h緭鍑哄埌鎺у埗鍙幫紝浣跨敤 JDOM 鐨?XMLOutputter 綾匯?br />聽聽聽 5.聽 灝?JDOM 杞寲涓?XML 鏂囨湰
聽聽聽聽聽聽聽 XMLOutputter xmlOut = new XMLOutputter("聽 ",true);
聽try {
聽 xmlOut.output(myDocument,System.out);
聽} catch (IOException e) {
聽 e.printStackTrace();
聽}
聽XMLOutputter 鏈夊嚑涓牸寮忛夐」銆傝繖閲屾垜浠凡鎸囧畾甯屾湜瀛愬厓绱犱粠鐖跺厓绱犵緝榪涗袱涓┖鏍鹼紝騫朵笖甯屾湜鍏冪礌闂存湁絀鴻銆?br />聽new XMLOutputter(java.lang.String indent, boolean newlines)榪欎釜鏂規硶鍦ㄦ渶鏂扮増鏈腑宸茬粡涓嶅緩璁嬌鐢ㄣ侸DOM鏈変竴涓笓闂ㄧ殑鐢ㄦ潵瀹氫箟鏍煎紡鍖栬緭鍑虹殑綾伙細org.jdom.output.Format錛屽鏋滀綘娌℃湁鐗規畩鐨勮姹傦紝鏈夋椂鍊欎嬌鐢ㄩ噷闈㈢殑鍑犱釜闈欐佹柟娉曪紙搴旇鍙互璇存槸棰勫畾涔夋牸寮忥級濡?getPrettyFormat()灝卞彲浠ヤ簡銆傛垜浠妸涓婇潰鐨勮緭鍑烘牸寮忕◢寰敼涓涓嬶紝灝辮薄榪欐牱錛?br />聽XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());聽
聽聽聽 6.灝咼DOM鏂囨。杞寲涓哄叾浠栧艦寮?br />聽聽聽聽聽聽聽 XMLOutputter 榪樺彲杈撳嚭鍒?Writer 鎴?OutputStream銆備負浜嗚緭鍑篔DOM鏂囨。鍒頒竴涓枃鏈枃浠訛紝鎴戜滑鍙互榪欐牱鍋氾細
聽聽聽聽聽聽聽 FileWriter writer = new FileWriter("/some/directory/myFile.xml");
聽聽聽聽聽聽聽 outputter.output(myDocument, writer);
聽聽聽聽聽聽聽 writer.close();
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 XMLOutputter 榪樺彲杈撳嚭鍒板瓧絎︿覆,浠ヤ究紼嬪簭鍚庨潰榪涜鍐嶅鐞?
聽聽聽聽聽聽聽 Strng outString = xmlOut.outputString(myDocument);
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 褰撶劧錛屽湪杈撳嚭鐨勬椂鍊欎綘涓嶄竴瀹氳杈撳嚭鎵鏈夌殑鏁翠釜鏂囨。錛屼綘鍙互閫夋嫨鍏冪礌榪涜杈撳嚭錛?br />聽聽聽聽聽聽聽 xmlOut.output(rootElement.getChild("name"),System.out);
聽聽聽聽聽聽聽 涓鍙ヨ瘽錛孞DOM闈炲父鐏墊椿鏂逛究錛佸鏋滀綘鎯寵繘涓姝ョ爺絀禞DOM錛岃鍒板畼鏂圭綉绔欏幓鐪嬩竴鐪嬶細http://www.jdom.org
聽聽聽 鏈妭紺轟緥婧愮爜錛?br />package com.cyberobject.study;
import java.io.IOException;
import org.jdom.Attribute;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
聽* @author kingwong
聽*
聽* TODO To change the template for this generated type comment go to
聽* Window - Preferences - Java - Code Style - Code Templates
聽*/
public class TestJDOM {
聽public static void main(String[] args)
聽{
聽 Element rootElement = new Element("MyInfo");
聽 Document myDocument = new Document(rootElement);
聽
//聽 Attribute rootAttri = new Attribute("comment","introduce myself");
//聽 rootElement.setAttribute(rootAttri);
聽
聽 rootElement.setAttribute("comment","introduce myself");
聽 //rootElement.setAttribute(new Attribute("comment","introduce myself"));
//聽 Element sexElement = new Element("sex");
//聽 rootElement.addContent(sexElement);
聽
//聽 Element nameElement = new Element("name");
//聽 nameElement.addContent("kingwong");
//聽 rootElement.addContent(nameElement);
聽
聽 rootElement.addContent((Content)(new Element("name").addContent("kingwong")));
聽 rootElement.addContent(new Element("sex").setAttribute("value","male"));
聽 rootElement.addContent((Content)(new Element("contract").addContent((Content)(new Element("telephone").addContent("87654321")))));
聽
聽 rootElement.removeChild("sex");
聽
聽 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
聽 try {
聽聽 xmlOut.output(myDocument,System.out);
聽聽 //xmlOut.output(rootElement.getChild("name"),System.out);
聽聽 //String outString = xmlOut.outputString(myDocument);
聽 } catch (IOException e) {
聽聽 e.printStackTrace();
聽 }
聽}
}
聽聽聽聽聽聽聽
浜屻乆ML鏂囨。瑙f瀽
聽聽聽 JDOM 涓嶅厜鍙互寰堟柟渚跨殑寤虹珛XML鏂囨。錛屽畠鐨勫彟涓涓敤澶勬槸瀹冭兘澶熻鍙栧茍鎿嶄綔鐜版湁鐨?XML 鏁版嵁銆?br />聽聽聽 JDOM鐨勮В鏋愬櫒鍦╫rg.jdom.input.*榪欎釜鍖呴噷錛屽叾涓殑DOMBuilder鐨勫姛鑳芥槸灝咲OM妯″瀷鐨凞ocument瑙f瀽鎴怞DOM妯″瀷鐨凞ocument錛汼AXBuilder鐨勫姛鑳芥槸浠庢枃浠舵垨嫻佷腑瑙f瀽鍑虹鍚圝DOM妯″瀷鐨刋ML鏍戙傜敱浜庢垜浠粡甯歌浠庝竴涓枃浠墮噷璇誨彇鏁版嵁錛屽洜姝ゆ垜浠簲璇ラ噰鐢ㄥ悗鑰呬綔涓鴻В鏋愬伐鍏楓?br />瑙f瀽涓涓獂ml鏂囨。錛屽熀鏈彲浠ョ湅鎴愪互涓嬪嚑涓楠わ細
聽聽聽 1.瀹炰緥鍖栦竴涓悎閫傜殑瑙f瀽鍣ㄥ璞?br />聽聽聽聽聽聽聽 鏈緥涓垜浠嬌鐢⊿AXBuilder:
聽聽聽聽聽聽聽 SAXBuilder sb = new SAXBuilder();
聽聽聽 2.浠ュ寘鍚玐ML鏁版嵁鐨勬枃浠朵負鍙傛暟錛屾瀯寤轟竴涓枃妗e璞yDocument
聽聽聽聽聽聽聽 Document myDocument = sb.build(/some/directory/myFile.xml);
聽聽聽 3.鑾峰埌鏍瑰厓绱?br />聽聽聽聽聽聽聽 Element rootElement = myDocument.getRootElement();
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 涓鏃︿綘鑾峰彇浜嗘牴鍏冪礌錛屼綘灝卞彲浠ュ緢鏂逛究鍦板瀹冧笅闈㈢殑瀛愬厓绱犺繘琛屾搷浣滀簡錛屼笅闈㈠Element瀵硅薄鐨勪竴浜涘父鐢ㄦ柟娉曚綔涓涓嬬畝鍗曡鏄庯細
聽聽聽聽聽聽聽 getChild("childname") 榪斿洖鎸囧畾鍚嶅瓧鐨勫瓙鑺傜偣,濡傛灉鍚屼竴綰ф湁澶氫釜鍚屽悕瀛愯妭鐐癸紝鍒欏彧榪斿洖絎竴涓紱濡傛灉娌℃湁榪斿洖null鍊箋?br />聽聽聽聽聽聽聽 getChildren("childname") 榪斿洖鎸囧畾鍚嶅瓧鐨勫瓙鑺傜偣List闆嗗悎銆傝繖鏍蜂綘灝卞彲浠ラ亶鍘嗘墍鏈夌殑鍚屼竴綰у悓鍚嶅瓙鑺傜偣銆?
聽聽聽聽聽聽聽 getAttributeValue("name") 榪斿洖鎸囧畾灞炴у悕瀛楃殑鍊箋傚鏋滄病鏈夎灞炴у垯榪斿洖null,鏈夎灞炴т絾鏄間負絀猴紝鍒欒繑鍥炵┖瀛楃涓層?br />聽聽聽聽聽聽聽 getChildText("childname") 榪斿洖鎸囧畾瀛愯妭鐐圭殑鍐呭鏂囨湰鍊箋?br />聽聽聽聽聽聽聽 getText() 榪斿洖璇ュ厓绱犵殑鍐呭鏂囨湰鍊箋?br />聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 榪樻湁鍏朵粬娌℃湁緗楀垪鍑烘潵鐨勬柟娉曪紝濡傛灉闇瑕佺殑璇濓紝鍙互闅忔椂鏌ラ槄JDOM鐨勫湪綰挎枃妗o細http://www.jdom.org/docs/apidocs/index.html銆傚綋鐒朵綘鍙互鍦ㄤ綘闇瑕佺殑鍦版柟娣誨姞銆佸垹闄ゅ厓绱犳搷浣滐紝榪樿寰椾笂闈㈢殑鍒涘緩XML鐨勬柟娉曞悧錛熷懙鍛祣~~
聽聽聽聽聽聽聽
聽聽聽聽聽聽聽 瀛︿範鏂頒笢涓滆繕鏄粠瀹炰緥瀛﹁搗鏈涓哄揩鎹鳳紝涓嬮潰綆鍗曚婦涓緥瀛愶紝灝變互涓婇潰鐨刋ML鏍鋒湰浠g爜鏉ュ涔燡DOM鐨刋ML瑙f瀽銆傛湰渚嬩腑璇誨彇浜嗘牱鏈琗ML鏂囦歡閲屼竴浜涘睘鎬у拰content錛屾渶鍚庢垜浠繕鍦╟ontact鍏冪礌閲屾彃鍏ヤ簡涓涓柊鍏冪礌<email value="wanghua@cyberobject.com" />銆傚敖綆℃垜浠疄鐜頒簡瀵逛簬XML鐨勫熀鏈搷浣滐紝緇嗗績鐨勬湅鍙嬪彲鑳戒細
鏈夌枒闂細濡傛灉XML鏂囨。鐨勫眰嬈$◢寰鏉備竴浜涳紝濡傛灉宓屽澶氳揪鍑犲崄涓婄櫨灞傜殑璇濓紙寮涓帺絎戯級錛屽鏋滈潬榪欐牱浠庢牴鍏冪礌涓綰т竴綰у湴閫氳繃getChild("childname")鏉ヨ闂瓙鍏冪礌鐨勮瘽錛屽皢浼氶潪甯哥棝鑻︼紒鏄殑錛岀殑紜槸榪欐牱錛屼絾鏄垜浠湁鍙︿竴涓湁鍔涚殑宸ュ叿XPath錛屼負浠涔堜笉鐢ㄥ憿錛熻繖鏄悗璇濓紒鍏堝崠涓叧瀛愶紙鎵嬫暡绱暒錛屼笅鍥炲惂錛屽懙鍛碉級銆?br />聽聽聽聽聽聽聽
/*
聽* Created on 2004-8-21
聽*
聽* TODO To change the template for this generated file go to
聽* Window - Preferences - Java - Code Style - Code Templates
聽*/
package com.cyberobject.study;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
聽* @author kingwong
聽*
聽* TODO To change the template for this generated type comment go to
聽* Window - Preferences - Java - Code Style - Code Templates
聽*/
public class TestJDOM2 {
聽public static void main(String[] args){
聽SAXBuilder sb = new SAXBuilder();
聽聽聽 try
聽聽聽 {聽聽聽聽聽聽聽
聽聽聽聽 Document doc = sb.build("myFile.xml");
聽 Element root = doc.getRootElement();
聽
聽 String str1 = root.getAttributeValue("comment");
聽 System.out.println("Root Element's comment attribute is : " + str1);
聽 String str2 = root.getChild("sex").getAttributeValue("value");
聽 System.out.println("sex Element's value attribute is : " + str2);
聽 String str3 = root.getChildText("name");
聽 System.out.println("name Element's content is :" + str3);
聽 String str4 = root.getChild("contact").getChildText("telephone");
聽 System.out.println("contact Element's telephone subelement content is : " + str4 + "\n");
聽 Element inputElement = root.getChild("contact");
聽 inputElement.addContent(new Element("email").setAttribute("value","wanghua@cyberobject.com"));
聽
聽 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
聽聽聽聽 String outStr = xmlOut.outputString(root);
聽聽聽聽 System.out.println(outStr);
聽聽聽 }
聽聽聽 catch(Exception e)
聽聽聽 {
聽聽聽聽聽聽聽 e.printStackTrace();
聽聽聽 }
}
}
銆銆import java.io.*; 銆銆import java.net.*; 銆銆public class MyServer { 銆銆public static void main(String[] args) throws IOException{ 銆銆ServerSocket server=new ServerSocket(5678); 銆銆while(true){ 銆銆銆Socket client=server.accept(); 銆銆銆BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); 銆銆銆PrintWriter out=new PrintWriter(client.getOutputStream()); 銆銆銆while(true){ 銆銆銆銆String str=in.readLine(); 銆銆銆銆System.out.println(str); 銆銆銆銆out.println("has receive...."); 銆銆銆銆out.flush(); 銆銆銆銆if(str.equals("end")) 銆銆銆銆銆break; 銆銆銆} 銆銆銆client.close(); 銆銆} 銆銆} 銆銆} |
銆銆import java.net.*; 銆銆import java.io.*; 銆銆public class MultiUser extends Thread{ 銆銆private Socket client; 銆銆public MultiUser(Socket c){ 銆銆this.client=c; 銆銆} 銆銆public void run(){ 銆銆try{ 銆銆銆BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); 銆銆銆PrintWriter out=new PrintWriter(client.getOutputStream()); 銆銆銆//Mutil User but can parallel 銆銆銆while(true){ 銆銆銆銆String str=in.readLine(); 銆銆銆銆System.out.println(str); 銆銆銆銆out.println("has receive...."); 銆銆銆銆out.flush(); 銆銆銆銆if(str.equals("end")) 銆銆銆銆銆break; 銆銆銆} 銆銆銆client.close(); 銆銆}catch(IOException ex){ 銆銆}finally{ 銆銆} 銆銆} 銆銆public static void main(String[] args)throws IOException{ 銆銆ServerSocket server=new ServerSocket(5678); 銆銆while(true){ 銆銆//transfer location change Single User or Multi User 銆銆MultiUser mu=new MultiUser(server.accept()); 銆銆mu.start(); 銆銆} 銆銆} 銆銆} |
BufferedReader in=new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out=new PrintWriter(client.getOutputStream()); |
DataInputStream isFromClient = new DataInputStream(client.getInputStream()); DataOutputStream osToClient = new DataOutputStream(client.getOutputStream()); |