锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨ROOT鍏冪礌order /order 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓殑鎵鏈塱tem鍏冪礌 /order/item 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓嬪厓绱爄tem涓殑鎵鏈塸rice鍏冪礌 娉ㄩ噴錛氱敤“/”璺緞寮濮嬩唬琛ㄥ厓绱犵殑緇濆璺緞 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌item涓殑鎵鏈塸rice鍊煎ぇ浜?2.60鐨勫厓绱?br />
/order/item[price>12.60] 鑺傜偣瀹氫綅錛?/p>
XML鏂囨尅鍙互琛ㄧず涓烘爲緇撴瀯鑺傜偣褰㈠紡 涓涓猉Path鐨勬ā寮忔槸浣跨敤鍙嶆枩鏉?#8220;/”鍒嗗紑瀛愬厓绱犲悕縐版弿榪拌礬寰?/p>
涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓嬪厓绱爄tem涓殑鎵鏈塸rice鍏冪礌 娉ㄩ噴錛氱敤“/”璺緞寮濮嬩唬琛ㄥ厓绱犵殑緇濆璺緞. 涓嶇敤“/”璺緞寮濮嬩唬琛ㄥ厓绱犵殑鐩稿璺緞 item/price 鐢?#8220;//”璺緞寮濮嬩唬琛ㄦ暣涓枃妗f弧瓚蟲潯浠剁殑鎵鏈夊厓绱?/p>
涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鏂囨。涓墍鏈夌殑item鍏冪礌 閫夋嫨鏈煡鍏冪礌 閫氶厤絎?nbsp;“*”鍙敤浜庨夋嫨鏈煡XML鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓殑鎵鏈塱tem鍏冪礌鎵灞炵殑瀛愬厓绱?/p>
/order/item/* 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓嬫墍鏈夊瓩瀛愯緢鐨刾rice鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鎵鏈夊叿鏈変袱涓鍏堢殑price鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鏂囨。鎵鏈夊厓绱?br />
//* 閫夋嫨鍒嗘敮 浣跨敤鏂規嫭鍙穂]鍙互鎸囧畾鐗瑰畾鐨勫厓绱?/p>
涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓殑絎竴涓猧tem鐨勫瓙鍏冪礌 /order/item[1] 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓殑鏈鍚庝竴涓猧tem鐨勫瓙鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮忛夋嫨鍏冪礌order涓叿鏈塸rice鍏冪礌鐨刬tem鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮?浠庡厓绱爋rder涓夋嫨鍏鋒湁price絳変簬12.60鍏冪礌鐨刬tem鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮?浠庨毝灞炰簬鍏冪礌order鐨刬tem鍏冪礌涓夋嫨鍏鋒湁price絳変簬12.60鍏冪礌鐨刾rice鍏冪礌 閫夋嫨鍑犱釜璺緞 鍦╔Path琛ㄨ揪寮忎腑錛屼嬌鐢?nbsp;"|" 榪愮畻絎﹀彲浠ラ夋嫨鍑犱釜璺緞 銆傚疄璐ㄤ笂鏄昏緫“涓?#8221;鎿嶄綔 涓嬮潰鐨刋Path琛ㄨ揪寮?浠庨毝灞炰簬order鐨刬tem鍏冪礌涓夋嫨鎵鏈塱temNumber 鍜宒escription 鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮?浠庢枃妗d腑閫夋嫨鎵鏈塱temNumber 錛宒escription鍜宲rice 鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮?閫夊彇灞炰簬order涓璱tem涓嬫墍鏈塱temNumber鍏冪礌鍜屼粠鏂囨。涓夋嫨鎵鏈塪escription鍏冪礌 閫夋嫨灞炴?br />
鍦╔Path涓紝鎵鏈夊睘鎬т嬌鐢ˊ鍓嶇紑 涓嬮潰鐨刋Path琛ㄨ揪寮?閫夊彇鎵鏈夊悕涓篶atalog鐨勫睘鎬?br />
//@catalog 涓嬮潰鐨刋Path琛ㄨ揪寮?閫夊彇鎵鏈夊叿鏈変換浣曞睘鎬х殑item鍏冪礌 涓嬮潰鐨刋Path琛ㄨ揪寮?閫夊彇鎵鏈夊叿鏈塩atalog絳変簬"parts"灞炴х殑item鍏冪礌
import net.sf.json.*;
import java.util.*;
import domain.*;
public class JsonDemo {
public static void arraytojson(){
String[] kkname={"CCED","WPS","WORD"};
JSONArray jr=JSONArray.fromObject(kkname);
System.out.println(jr);
}
public static void list2json(){
List<Book> bklist=new ArrayList<Book>();
bklist.add(new Book("a1","b","111"));
bklist.add(new Book("a2","b","111"));
bklist.add(new Book("a3","綺鵑欽AVA","111"));
JSONArray jr=JSONArray.fromObject(bklist);
System.out.println(jr);
}
public static void createJson(){
JSONArray jn=JSONArray.fromObject("[{bookname:'JAVA紼嬪簭璁捐',isbn:'885596-99',author:'灝忎笢'}]");//寮澶村拰緇撳熬甯]
List a=JSONArray.toList(jn, Book.class);
Book book=null;
for(Iterator it=a.iterator();it.hasNext();){
book=(Book)it.next();
System.out.println(book.getAuthor()+"-"+book.getBookname());
}
}
public static void Map2Json(){
Map map=new HashMap();
map.put("name", "灝忚4");
map.put("book", new Book("K璇█紼嬪簭璁捐","鏉?/span>","55588"));
map.put("shoBookName","function(){alert('嫻嬭瘯')}");
JSONObject jo=JSONObject.fromObject(map);
System.out.println(jo);
}
public static void bean2json(){
JSONObject jo=JSONObject.fromObject(new Book("1","3","ABCd"));
System.out.println(jo);
}
public static void json2bean(){
JSONObject jo=JSONObject.fromObject("{bookname:'C++紼嬪簭璁捐',isbn:'885596-989',author:'灝忎笢'}");
Book book=(Book)JSONObject.toBean(jo,Book.class);
System.out.println(book.getAuthor()+"-"+book.getBookname());
}
public static void main(String[] args)throws Exception{
arraytojson();
list2json();
createJson();
Map2Json();
json2bean();
}
}
]]>
]]>
<order>
<item>
<itemNumber>C2688-67037</itemNumber>
<description>LCD娑叉櫠鏄劇ず鍣?/span></description>
<quantity>1</quantity>
<price>358.00</price>
</item>
<item>
<itemNumber>C2688-67061</itemNumber>
<description>闊崇</description>
<quantity>1</quantity>
<price>16.50</price>
</item>
<item>
<itemNumber>C2688-67010</itemNumber>
<description>榧犳爣</description>
<quantity>1</quantity>
<price>8.50</price>
</item>
</order>
/order/item/price
XPath瀹氫箟鏍囧噯鍑芥暟搴?/p>
XPath浣跨敤妯″紡琛ㄨ揪寮忚瘑鍒玐ML鏂囨。鐨勮妭鐐廣?/p>
/order/item/price
//item
/order/*/price
/*/*/price
/order/item[last()]
/order/item[price]
/order/item[price=16.50]
/order/item[price=16.50]/price
/order/item/itemNumber | /order/item/description
涓嬮潰鐨刋Path琛ㄨ揪寮?浠庢枃妗d腑閫夋嫨鎵鏈塱temNumber 鍜宒escription 鍏冪礌
//itemNumber | //description
//itemNumber | //description | //price
/order/item/itemnumber | //description
涓嬮潰鐨刋Path琛ㄨ揪寮?閫夊彇鎵鏈夊叿鏈塩atalog灞炴х殑item鍏冪礌
//item[@catalog]
//item[@*]
//item[@catalog="parts"]
]]>
www.w3pop.com榪欑綉绔欎笂鏈変笌XML/HTML鐩稿叧鐨勮緇嗚祫鏂欍?br />
鍦∕yeclipse緙栧啓XSD鏂囦歡闈炲父鐨勪富娉曪紝鍙渶瑕佹妸涓嶺SD鐩稿叧鐨勫厓绱犺浣忓氨琛岋紝鍍忕紪杈慩ML涓鏍鳳紝騫朵笖鏈夋彁紺猴紝榪樺彲浠ユ牴鎹甔SD鏂囦歡寤虹珛XML鏂囦歡銆傝繖鏄竴涓殢渚垮啓鐨刋SD鏂囦歡
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/note" xmlns:tns="http://www.example.org/note">
<element name="note">
<complexType>
<attribute name="id" type="integer" use="required" />
<sequence>
<element name="to" >
<simpleType>
<restriction base="integer">
<minInclusive value="0"></minInclusive>
<maxInclusive value="100"></maxInclusive>
</restriction>
</simpleType>
</element>
<element name="from" type="string"/>
<element name="heading" type="string"/>
<element name="body" type="string"/>
</sequence>
</complexType>
</element>
</schema>
]]>
http://www.tkk7.com/huanghuizz/articles/142875.html json in java鐨勪粙緇?br />
涓綃噅son瀛︿範濂芥枃
http://wangcheng.javaeye.com/blog/71343
]]>
銆銆Java鎻愪緵浜哠AX鍜孌OM涓ょ鏂瑰紡鐢ㄤ簬瑙f瀽XML,浣嗗嵆渚垮姝?瑕佽鍐欎竴涓◢寰鏉傜殑XML,涔熶笉鏄竴浠跺鏄撶殑浜?
銆銆浜屻乆MLBean綆浠?/p>
銆銆Hibernate宸茬粡鎴愪負鐩墠嫻佽鐨勯潰鍚慗ava鐜鐨勫璞?鍏崇郴鏁版嵁搴?/font>鏄犲皠宸ュ叿.鍦℉ibernate絳夊璞?鍏崇郴鏁版嵁搴撴槧灝勫伐鍏峰嚭鐜頒箣鍓?瀵規暟鎹簱鐨勬搷浣滄槸閫氳繃JDBC鏉ュ疄鐜扮殑,瀵規暟鎹簱鐨勪換浣曟搷浣?寮鍙戜漢鍛橀兘瑕佽嚜宸卞啓SQL璇彞鏉ュ疄鐜? 瀵硅薄/鍏崇郴鏁版嵁搴撴槧灝勫伐鍏峰嚭鐜板悗,瀵規暟鎹簱鐨勬搷浣滆漿鎴愬JavaBean鐨勬搷浣?鏋佸ぇ鏂逛究浜嗘暟鎹簱寮鍙? 鎵浠ュ鏋滄湁涓涓被浼肩殑宸ュ叿鑳藉瀹炵幇灝嗗XML鐨勮鍐欒漿鎴愬JavaBean鐨勬搷浣?灝嗕細綆鍖朮ML鐨勮鍐?鍗充嬌瀵筙ML涓嶇啛鎮夌殑寮鍙戜漢鍛樹篃鑳芥柟渚垮湴璇誨啓XML. 榪欎釜宸ュ叿灝辨槸XMLBean.
銆銆涓夈佸噯澶嘪MLBean鍜孹ML鏂囨。
銆銆XMLBean鏄疉pache鐨勪竴涓紑婧愰」鐩?鍙互浠?a snap_preview_added="spa" snap_icon_added="spa" act_suffix="" icon_trigger="false" text_trigger="true" parent_link_icon="false">http://www.apache.org涓嬭澆,鏈鏂扮殑鐗堟湰鏄?.0. 瑙e帇鍚庣洰褰曞涓?
xmlbean2.0.0
+---bin
+---docs
+---lib
+---samples
+---schemas
銆銆鍙﹀榪樿鍑嗗涓涓猉ML鏂囨。(customers.xml),
銆銆鍦ㄦ湰鏂囩殑渚嬪瓙閲?鎴戜滑灝嗗榪欎釜鏂囨。榪涜璇誨啓鎿嶄綔. 鏂囨。婧愮爜濡備笅:
<?xml version="1.0" encoding="UTF-8"?>
<Customers>
<customer>
<id>1</id>
<gender>female</gender>
<firstname>Jessica</firstname>
<lastname>Lim</lastname>
<phoneNumber>1234567</phoneNumber>
<address>
<primaryAddress>
<postalCode>350106</postalCode>
<addressLine1>#25-1</addressLine1>
<addressLine2>SHINSAYAMA 2-CHOME</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Ms Danielle</receiver>
<postalCode>350107</postalCode>
<addressLine1>#167</addressLine1>
<addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
</billingAddress>
</address>
</customer>
<customer>
<id>2</id>
<gender>male</gender>
<firstname>David</firstname>
<lastname>Bill</lastname>
<phoneNumber>808182</phoneNumber>
<address>
<primaryAddress>
<postalCode>319087</postalCode>
<addressLine1>1033 WS St.</addressLine1>
<addressLine2>Tima Road</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Mr William</receiver>
<postalCode>672993</postalCode>
<addressLine1>1033 WS St.</addressLine1>
<addressLine2>Tima Road</addressLine2>
</billingAddress>
</address>
</customer>
</Customers>
銆銆榪欐槸涓涓鎴風殑鏁版嵁妯″瀷,姣忎釜瀹㈡埛閮芥湁瀹㈡埛緙栧彿(ID),濮撳悕,鎬у埆(gender),鐢佃瘽鍙風爜(phoneNumber)鍜屽湴鍧,鍏朵腑鍦板潃鏈変袱涓? 棣栬鍦板潃(PrimaryAddress)鍜屽笎鍗曞湴鍧(BillingAddress),姣忎釜鍦板潃鏈夐偖緙?鍦板潃1,鍜屽湴鍧2緇勬垚.鍏朵腑甯愬崟鍦板潃榪樻湁鏀朵歡浜?receiver).姝ゅ,榪樿鍑嗗涓涓厤緗枃浠?鏂囦歡鍚峜ustomer.xsdconfig),榪欎釜鏂囦歡鐨勪綔鐢ㄦ垜鍚庨潰浼氳,瀹冪殑鍐呭濡備笅:
<xb:config xmlns:xb="http://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace>
<xb:package>sample.xmlbean</xb:package>
</xb:namespace>
</xb:config>
銆銆鍥涖乆MLBean浣跨敤姝ラ
銆銆鍜屽叾浠栭潰鍚慗ava鐜鐨勫璞?鍏崇郴鏁版嵁搴撴槧灝勫伐鍏風殑浣跨敤姝ラ涓鏍?鍦ㄦ寮忎嬌鐢╔MLBean鍓?鎴戜滑瑕佷綔涓や釜鍑嗗.
銆銆1. 鐢熸垚XML Schema鏂囦歡
銆銆浠涔堟槸XML Schema鏂囦歡? 姝e父鎯呭喌涓?姣忎釜XML鏂囦歡閮芥湁涓涓猄chema鏂囦歡,XML Schema鏂囦歡鏄竴涓猉ML鐨勭害鏉熸枃浠?瀹冨畾涔変簡XML鏂囦歡鐨勭粨鏋勫拰鍏冪礌.浠ュ強瀵瑰厓绱犲拰緇撴瀯鐨勭害鏉? 閫氫織鍦拌,濡傛灉璇碭ML鏂囦歡鏄暟鎹簱閲岀殑璁板綍,閭d箞Schema灝辨槸琛ㄧ粨鏋勫畾涔?
銆銆涓轟粈涔堥渶瑕佽繖涓枃浠? XMLBean闇瑕侀氳繃榪欎釜鏂囦歡鐭ラ亾涓涓猉ML鏂囦歡鐨勭粨鏋勪互鍙婄害鏉?姣斿鏁版嵁綾誨瀷絳? 鍒╃敤榪欎釜Schema鏂囦歡,XMLBean灝嗕細浜х敓涓緋誨垪鐩稿叧鐨凧ava Classes鏉ュ疄鐜板XML鐨勬搷浣? 鑰屼綔涓哄紑鍙戜漢鍛?鍒欐槸鍒╃敤XMLBean浜х敓鐨凧ava Classes鏉ュ畬鎴愬XML鐨勬搷浣滆屼笉闇瑕丼AX鎴朌OM.鎬庢牱浜х敓榪欎釜Schema鏂囦歡鍛? 濡傛灉瀵逛簬鐔熸倝XML鐨勫紑鍙戜漢鍛?鍙互鑷繁鏉ュ啓榪欎釜Schema鏂囦歡,瀵逛簬涓嶇啛鎮塜ML鐨勫紑鍙戜漢鍛?鍙互閫氳繃涓浜涘伐鍏鋒潵瀹屾垚.姣旇緝鏈夊悕鐨勫XMLSPY鍜孲tylus Studio閮藉彲浠ラ氳繃XML鏂囦歡鏉ョ敓鎴怱chema鏂囦歡. 鍔犲叆鎴戜滑宸茬粡鐢熸垚榪欎釜Schema鏂囦歡(customer.xsd):
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Customers">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="customer"
type="customerType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="customerType">
<xs:sequence>
<xs:element name="id" type="xs:int"/>
<xs:element name="gender" type="xs:string"/>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:element name="phoneNumber" type="xs:string"/>
<xs:element name="address" type="addressType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="addressType">
<xs:sequence>
<xs:element name="primaryAddress" type="primaryAddressType"/>
<xs:element name="billingAddress" type="billingAddressType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="primaryAddressType">
<xs:sequence>
<xs:element name="postalCode" type="xs:string"/>
<xs:element name="addressLine1" type="xs:string"/>
<xs:element name="addressLine2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="billingAddressType">
<xs:sequence>
<xs:element name="receiver" type="xs:string"/>
<xs:element name="postalCode" type="xs:string"/>
<xs:element name="addressLine1" type="xs:string"/>
<xs:element name="addressLine2" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
銆銆2. 鍒╃敤scomp鏉ョ敓鎴怞ava Classes
銆銆scomp鏄疿MLBean鎻愪緵鐨勪竴涓紪璇戝伐鍏?瀹冨湪bin鐨勭洰褰曚笅. 閫氳繃榪欎釜宸ュ叿,鎴戜滑鍙互灝嗕互涓婄殑Schema鏂囦歡鐢熸垚Java Classes.scomp鐨勮娉曞涓?-
銆銆scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*
銆銆涓昏鍙傛暟璇存槑:
銆銆-src [dir] -- 鐢熸垚鐨凧ava Classes瀛樻斁鐩綍
銆銆-srconly -- 涓嶇紪璇慗ava Classes,涓嶄駭鐢烰ar鏂囦歡
銆銆-out [jarFileName] -- 鐢熸垚鐨凧ar鏂囦歡,緙虹渷鏄痻mltypes.jar
銆銆-compiler -- Java緙栬瘧鍣ㄧ殑璺緞,鍗矹avac鐨勪綅緗?/p>
銆銆schemaFile.xsd -- XML Schema鏂囦歡浣嶇疆
銆銆config.xsdconfig -- xsdconfig鏂囦歡鐨勪綅緗? 榪欎釜鏂囦歡涓昏鐢ㄦ潵鍒跺畾鐢熸垚鐨凧ava Class鐨勪竴浜涙枃浠跺悕瑙勫垯鍜孭ackage鐨勫悕縐?鍦ㄦ湰鏂?package鏄痵ample.xmlbean
銆銆鍦ㄦ湰鏂?鎴戞槸榪欐牱榪愯鐨?
scomp -src build\src -out build\customerXmlBean.jar schema\customer.xsd
-compiler C:\jdk142_04\bin\javac customer.xsdconfig
銆銆榪欎釜鍛戒護琛岀殑鎰忔濇槸鍛婅瘔scomp鐢熸垚customerXmlBean.jar,鏀懼湪build鐩綍涓?鍚屾椂鐢熸垚婧愪唬鐮佹斁鍦╞uild\src涓? Schema鏂囦歡鏄痗ustomer.xsd,xsdconfig鏂囦歡鏄痗ustomer.xsdconfig.鍏跺疄, 鐢熸垚鐨凧ava婧愪唬鐮佹病鏈夊澶т綔鐢?鎴戜滑瑕佺殑鏄痡ar鏂囦歡.鎴戜滑鍏堢湅涓涓媌uild\src\sample\xmlbean涓嬬敓鎴愮殑Classes.
銆銆CustomersDocument.java -- 鏁翠釜XML鏂囨。鐨凧ava Class鏄犲皠
銆銆CustomerType.java -- 鑺傜偣sustomer鐨勬槧灝?/p>
銆銆AddressType.java -- 鑺傜偣address鐨勬槧灝?/p>
銆銆BillingAddressType.java -- 鑺傜偣billingAddress鐨勬槧灝?/p>
銆銆PrimaryAddressType.java -- 鑺傜偣primaryAddress鐨勬槧灝?/p>
銆銆濂戒簡,鍒版鎴戜滑鎵鏈夌殑鍑嗗宸ヤ綔宸茬粡瀹屾垚浜? 涓嬮潰灝卞紑濮嬭繘鍏ラ噸鐐?鍒╃敤鍒氭墠鐢熸垚鐨刯ar鏂囦歡璇誨啓XML.
銆銆浜斻佸埄鐢╔MLBean璇籜ML鏂囦歡
銆銆鏂板緩涓涓狫ava Project,灝哫MLBean2.0.0\lib\涓嬬殑Jar鏂囦歡鍜屽垰鎵嶆垜浠敓鎴愮殑customerXmlBean.jar鍔犲叆鍒癙roject鐨凜lassPath.
銆銆鏂板緩涓涓狫ava Class: CustomerXMLBean. 婧愮爜濡備笅:
package com.sample.reader;
import java.io.File;
import sample.xmlbean.*;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.xmlbeans.XmlOptions;
public class CustomerXMLBean {
private String filename = null;
public CustomerXMLBean(String filename) {
super();
this.filename = filename;
}
public void customerReader() {
try {
File xmlFile = new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++) {
CustomerType customer = customers[i];
println("Customer#" + i);
println("Customer ID:" + customer.getId());
println("First name:" + customer.getFirstname());
println("Last name:" + customer.getLastname());
println("Gender:" + customer.getGender());
println("PhoneNumber:" + customer.getPhoneNumber());
// Primary address
PrimaryAddressType primaryAddress = customer.getAddress().getPrimaryAddress();
println("PrimaryAddress:");
println("PostalCode:" + primaryAddress.getPostalCode());
println("AddressLine1:" + primaryAddress.getAddressLine1());
println("AddressLine2:" + primaryAddress.getAddressLine2());
// Billing address
BillingAddressType billingAddress = customer.getAddress().getBillingAddress();
println("BillingAddress:");
println("Receiver:" + billingAddress.getReceiver());
println("PostalCode:" + billingAddress.getPostalCode());
println("AddressLine1:" + billingAddress.getAddressLine1());
println("AddressLine2:" + billingAddress.getAddressLine2());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void println(String str) {
System.out.println(str);
}
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.customerReader();
}
}
銆銆榪愯瀹?鍙傜湅杈撳嚭緇撴灉:
Customer#0
Customer ID:1
First name:Jessica
Last name:Lim
Gender:female
PhoneNumber:1234567
PrimaryAddress:
PostalCode:350106
AddressLine1:#25-1
AddressLine2:SHINSAYAMA 2-CHOME
BillingAddress:
Receiver:Ms Danielle
PostalCode:350107
AddressLine1:#167
AddressLine2:NORTH TOWER HARBOUR CITY
Customer#1
Customer ID:2
First name:David
Last name:Bill
Gender:male
PhoneNumber:808182
PrimaryAddress:
PostalCode:319087
AddressLine1:1033 WS St.
AddressLine2:Tima Road
BillingAddress:
Receiver:Mr William
PostalCode:672993
AddressLine1:1033 WS St.
AddressLine2:Tima Road
銆銆鎬庝箞鏍?鏄笉鏄緢杞繪澗? XMLBean鐨勫▉鍔?
銆銆鍏佸埄鐢╔MLBean鍐橷ML鏂囦歡
銆銆鍒╃敤XMLBean鍒涘緩涓涓猉ML鏂囨。涔熸槸涓浠惰交鑰屾槗涓劇殑浜?鎴戜滑鍐嶅鍔犱竴涓狹ethod,
銆銆璇風湅涓涓嬬殑Java Class:
public void createCustomer() {
try {
// Create Document
CustomersDocument doc = CustomersDocument.Factory.newInstance();
// Add new customer
CustomerType customer = doc.addNewCustomers().addNewCustomer();
// set customer info
customer.setId(3);
customer.setFirstname("Jessica");
customer.setLastname("Lim");
customer.setGender("female");
customer.setPhoneNumber("1234567");
// Add new address
AddressType address = customer.addNewAddress();
// Add new PrimaryAddress
PrimaryAddressType primaryAddress = address.addNewPrimaryAddress();
primaryAddress.setPostalCode("350106");
primaryAddress.setAddressLine1("#25-1");
primaryAddress.setAddressLine2("SHINSAYAMA 2-CHOME");
// Add new BillingAddress
BillingAddressType billingAddress = address.addNewBillingAddress();
billingAddress.setReceiver("Ms Danielle");
billingAddress.setPostalCode("350107");
billingAddress.setAddressLine1("#167");
billingAddress.setAddressLine2("NORTH TOWER HARBOUR CITY");
File xmlFile = new File(filename);
doc.save(xmlFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
銆銆淇敼main method.
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.createCustomer();
}
銆銆榪愯,鎵撳紑customers_new.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Customers>
<customer>
<id>3</id>
<gender>female</gender>
<firstname>Jessica</firstname>
<lastname>Lim</lastname>
<phoneNumber>1234567</phoneNumber>
<address>
<primaryAddress>
<postalCode>350106</postalCode>
<addressLine1>#25-1</addressLine1>
<addressLine2>SHINSAYAMA 2-CHOME</addressLine2>
</primaryAddress>
<billingAddress>
<receiver>Ms Danielle</receiver>
<postalCode>350107</postalCode>
<addressLine1>#167</addressLine1>
<addressLine2>NORTH TOWER HARBOUR CITY</addressLine2>
</billingAddress>
</address>
</customer>
</Customers>
銆銆涓冦佸埄鐢╔MLBean淇敼XML鏂囦歡
銆銆鎴戜滑鍐嶅鍔犱竴涓狹ethod:
public void updateCustomer(int id,String lastname) {
try {
File xmlFile = new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setLastname(lastname);
break;
}
}
doc.save(xmlFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
銆銆main method:
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.updateCustomer(3,"last");
}
銆銆榪愯涔嬪悗,鎴戜滑灝嗕細鐪嬪埌瀹㈡埛緙栧彿涓?鐨勫鎴風殑lastname宸茬粡鏀逛負last.
銆銆鍏佸埄鐢╔MLBean鍒犻櫎涓涓猚ustomer
銆銆鍐嶅鍔犱竴涓狹ethod:
public void deleteCustomer(int id) {
try {
File xmlFile = new File(filename);
CustomersDocument doc = CustomersDocument.Factory.parse(xmlFile);
CustomerType[] customers = doc.getCustomers().getCustomerArray();
for (int i = 0; i < customers.length; i++) {
CustomerType customer = customers[i];
if(customer.getId()==id){
customer.setNil() ;
break;
}
}
doc.save(xmlFile);
} catch (Exception ex) {
ex.printStackTrace();
}
}
銆銆main method:
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.deleteCustomer(3);
}
銆銆榪愯,鎴戜滑灝嗕細鐪嬪埌瀹㈡埛緙栧彿涓?鐨勫鎴風殑璧勬枡宸茬粡琚垹闄?
銆銆涔濄佹煡璇ML
銆銆闄や簡鏈枃鍦ㄤ互涓婅榪扮殑,鍒╃敤XMLBean鑳借交杞繪澗鏉懼畬鎴怷ML鐨勮鍐欐搷浣滃,緇撳悎XPath鍜孹Query,XMLBean榪樿兘瀹屾垚璞QL鏌ヨ鏁版嵁搴撲竴鏍鋒柟渚垮湴鏌ヨXML鏁版嵁. 鍏充簬XML鏌ヨ浠ュ強濡備綍鍒涘緩XML鏁版嵁搴? 鎴戝皢鍦ㄥ彟涓綃囨枃绔犻噷璁ㄨ.
銆銆鍗併佺粨鏉熻
銆銆XMLBean鑳藉府鍔╂垜浠交鏄撹鍐橷ML,榪欏皢鏈夊姪浜庢垜浠檷浣嶺ML鐨勫涔犲拰浣跨敤,鏈変簡榪欎釜鍩虹,寮鍙戜漢鍛樺皢涓哄涔犳洿澶氬湴XML鐩稿叧鎶鏈拰Web Services,JMS絳夊叾浠朖2EE鎶鏈墦涓嬭壇濂藉湴鍩虹.