锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鑰屼笖嫻忚鍣ㄧ殑緙栫爜涔熸槸UTF-8鐨勶紝榪欏氨鎺掗櫎浜嗘祻瑙堝櫒鐨勯棶棰樸?br />
鍐嶇敤VIM鎵撳紑錛屽彂鐜扮敤GB2312鐪嬫槸娌¢棶棰樼殑錛屾崲鎴?set encoding=UTF-8浠ュ悗寮濮嬩貢鐮?br />
榪欐椂鎴戝皾璇曞皢瀛楃涓茶漿鐮佸悗鍐欏叆鏂囦歡錛屼絾鍦║TF-8錛孏BK鍜孖SO8859_1涓棿鎬庝箞杞篃娌℃湁鐢ㄣ?br />
蹇界劧鎯寵搗鍓嶅嚑澶﹜iyayoyo鍚屽鍜屾垜鎻愯繃Java鍐欐枃浠墮粯璁ょ紪鐮佺殑闂錛屼簬鏄紑濮媑oogle錛屽彂鐜版垜鐢ㄧ殑鍐欐枃浠剁殑鏂瑰紡鏃犳硶鎸囧畾緙栫爜錛屼簬鏄崲鐢ㄥ彟涓縐嶅啓鏂囦歡鐨勬柟寮忔寚瀹歎TF-8錛岄亗鎼炲畾銆備唬鐮佸涓嬶細
鑰佷唬鐮侊細
PrintWriter pw = new PrintWriter(new FileWriter(path));
pw.print(content);
pw.close();
鏂頒唬鐮侊細
FileOutputStream fos = new FileOutputStream(path);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(content);
out.close();
fos.close();
欏轟究鎶辨ㄤ竴鍙ワ紝Java涓啓鏂囦歡鐨勬柟寮忚繕鐪熸槸澶氶樋澶?#8230;…鎴戠瓑涓涓ゅ勾緇忛獙鐨勫皬紼嬪簭鍛樼湅浜嗛兘鐪兼檿
搴撳崱 璇達細
璇諱唬鐮佷篃鏈夌紪鐮佺殑闂錛屽鏋滆璇誨彇UTF-8鐨勬枃浠訛紝搴旈噰鐢ㄥ涓嬫柟寮忚鐩栭粯璁ょ紪鐮侊細
FileInputStream fis = new FileInputStream(s);
StringBuffer content = new StringBuffer();
DataInputStream in = new DataInputStream(fis);
BufferedReader d = new BufferedReader(new InputStreamReader(in, "UTF-8"));
String line = null;
while ((line = d.readLine()) != null)
content.append(line + "\n");
d.close();
in.close();
fis.close();
<?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>
<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>
<?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>
scomp [options] [dirs]* [schemaFile.xsd]* [service.wsdl]* [config.xsdconfig]*
scomp -src build\src -out build\customerXmlBean.jar schema\customer.xsd
-compiler C:\jdk142_04\bin\javac customer.xsdconfig
CustomersDocument.java -- 鏁翠釜XML鏂囨。鐨凧ava Class鏄犲皠
CustomerType.java -- 鑺傜偣sustomer鐨勬槧灝?br /> AddressType.java -- 鑺傜偣address鐨勬槧灝?br /> BillingAddressType.java -- 鑺傜偣billingAddress鐨勬槧灝?br /> PrimaryAddressType.java -- 鑺傜偣primaryAddress鐨勬槧灝?br />
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
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();
}
}
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.createCustomer();
}
<?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>
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();
}
}
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");
}
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();
}
}
public static void main(String[] args) {
String filename = "F://JavaTest//Eclipse//XMLBean//xml//customers_new.xml";
CustomerXMLBean customerXMLBean = new CustomerXMLBean(filename);
customerXMLBean.deleteCustomer(3);
}
榪欐浠g爜鐨勬墦鍗拌緭鍑轟負錛?/p>
銆銆null
銆銆alterrjzjh
銆銆jjjjjj
銆銆浠庝笂闈㈠彲浠ョ湅鍑?/p>
銆銆String name=node.getNodeValue();
銆銆鏄竴涓┖鍊箋傝?/p>
銆銆String name1=node.getFirstChild().getNodeValue();
銆銆鎵嶆槸鐪熸鐨勫鹼紝榪欐槸鍥犱負DOM鎶?lt;name>rjzjh</name>涔熷綋浣滄槸涓ゅ眰緇撴瀯鐨勮妭鐐癸紝鍏剁埗鑺傜偣
銆銆 閫夋嫨 DOM 榪樻槸閫夋嫨 SAX 錛?
銆銆瀵逛簬闇瑕佽嚜宸辯紪鍐欎唬鐮佹潵澶勭悊 XML 鏂囨。鐨勫紑鍙戜漢鍛樻潵璇達紝閫夋嫨 DOM 榪樻槸 SAX 瑙f瀽妯″瀷鏄竴涓潪甯擱噸瑕佺殑璁捐鍐崇瓥銆?
銆銆DOM 閲囩敤寤虹珛鏍戝艦緇撴瀯鐨勬柟寮忚闂?XML 鏂囨。錛岃?SAX 閲囩敤鐨勪簨浠舵ā鍨嬨?
銆銆DOM 瑙f瀽鍣ㄦ妸 XML 鏂囨。杞寲涓轟竴涓寘鍚叾鍐呭鐨勬爲錛屽茍鍙互瀵規(guī)爲榪涜閬嶅巻銆傜敤 DOM 瑙f瀽妯″瀷鐨勪紭鐐規(guī)槸緙栫▼瀹規(guī)槗錛屽紑鍙戜漢鍛樺彧闇瑕佽皟鐢ㄥ緩鏍戠殑鎸囦護錛岀劧鍚庡埄鐢╪avigation APIs璁塊棶鎵闇鐨勬爲鑺傜偣鏉ュ畬鎴愪換鍔°傚彲浠ュ緢瀹規(guī)槗鐨勬坊鍔犲拰淇敼鏍戜腑鐨勫厓绱犮傜劧鑰岀敱浜庝嬌鐢?DOM 瑙f瀽鍣ㄧ殑鏃跺欓渶瑕佸鐞嗘暣涓?XML 鏂囨。錛屾墍浠ュ鎬ц兘鍜屽唴瀛樼殑瑕佹眰姣旇緝楂橈紝灝ゅ叾鏄亣鍒板緢澶х殑 XML 鏂囦歡鐨勬椂鍊欍傜敱浜庡畠鐨勯亶鍘嗚兘鍔涳紝DOM 瑙f瀽鍣ㄥ父鐢ㄤ簬 XML 鏂囨。闇瑕侀綣佺殑鏀瑰彉鐨勬湇鍔′腑銆?
銆銆SAX 瑙f瀽鍣ㄩ噰鐢ㄤ簡鍩轟簬浜嬩歡鐨勬ā鍨嬶紝瀹冨湪瑙f瀽 XML 鏂囨。鐨勬椂鍊欏彲浠ヨЕ鍙戜竴緋誨垪鐨勪簨浠訛紝褰撳彂鐜扮粰瀹氱殑tag鐨勬椂鍊欙紝瀹冨彲浠ユ縺媧諱竴涓洖璋冩柟娉曪紝鍛婅瘔璇ユ柟娉曞埗瀹氱殑鏍囩宸茬粡鎵懼埌銆係AX 瀵瑰唴瀛樼殑瑕佹眰閫氬父浼氭瘮杈冧綆錛屽洜涓哄畠璁╁紑鍙戜漢鍛樿嚜宸辨潵鍐沖畾鎵瑕佸鐞嗙殑tag銆傜壒鍒槸褰撳紑鍙戜漢鍛樺彧闇瑕佸鐞嗘枃妗d腑鎵鍖呭惈鐨勯儴鍒嗘暟鎹椂錛孲AX 榪欑鎵╁睍鑳藉姏寰楀埌浜嗘洿濂界殑浣撶幇銆備絾鐢?SAX 瑙f瀽鍣ㄧ殑鏃跺欑紪鐮佸伐浣滀細姣旇緝鍥伴毦錛岃屼笖寰堥毦鍚屾椂璁塊棶鍚屼竴涓枃妗d腑鐨勫澶勪笉鍚屾暟鎹?
銆銆Bean鏂囦歡錛?
銆銆package com.test;
銆銆import org.xml.sax.*;
銆銆import org.xml.sax.helpers.*;
銆銆import javax.xml.parsers.*;
銆銆public class MyXMLReader extends DefaultHandler {
銆銆java.util.Stack tags = new java.util.Stack();
銆銆public MyXMLReader() {
銆銆super();
銆銆}
銆銆public static void main(String args[]) {
銆銆long lasting = System.currentTimeMillis();
銆銆try {
銆銆銆SAXParserFactory sf = SAXParserFactory.newInstance();
銆銆銆SAXParser sp = sf.newSAXParser();
銆銆銆MyXMLReader reader = new MyXMLReader();
銆銆銆sp.parse(new InputSource("data_10k.xml"), reader);
銆銆} catch (Exception e) {
銆銆銆e.printStackTrace();
銆銆}
銆銆System.out.println("榪愯鏃墮棿錛? + (System.currentTimeMillis() - lasting) + " 姣");
銆銆}
銆銆public void characters(char ch[], int start, int length) throws SAXException {
銆銆String tag = (String) tags.peek();
銆銆if (tag.equals("NO")) {
銆銆銆System.out.print("杞︾墝鍙風爜錛? + new String(ch, start, length));
銆銆}
銆銆if (tag.equals("ADDR")) {
銆銆System.out.println(" 鍦板潃:" + new String(ch, start, length));
銆銆}
銆銆}
銆銆public void startElement(
銆銆String uri,
銆銆String localName,
銆銆String qName,
銆銆Attributes attrs) {
銆銆tags.push(qName);
銆銆}
銆銆}
銆銆10k娑堣楁椂闂達細110 47 109 78
銆銆100k娑堣楁椂闂達細344 406 375 422
銆銆1000k娑堣楁椂闂達細3234 3281 3688 3312
銆銆10000k娑堣楁椂闂達細32578 34313 31797 31890 30328
銆銆鐒跺悗鏄?JDOM http://www.jdom.org/
銆銆JDOM 鐨勭洰鐨勬槸鎴愪負 Java 鐗瑰畾鏂囨。妯″瀷錛屽畠綆鍖栦笌 XML 鐨勪氦浜掑茍涓旀瘮浣跨敤 DOM 瀹炵幇鏇村揩銆傜敱浜庢槸絎竴涓?Java 鐗瑰畾妯″瀷錛孞DOM 涓鐩村緱鍒板ぇ鍔涙帹騫垮拰淇冭繘銆傛鍦ㄨ冭檻閫氳繃“Java 瑙勮寖璇鋒眰 JSR-102”灝嗗畠鏈緇堢敤浣?#8220;Java 鏍囧噯鎵╁睍”銆備粠 2000 騫村垵灝卞凡緇忓紑濮嬩簡 JDOM 寮鍙戙?
銆銆JDOM 涓?DOM 涓昏鏈変袱鏂歸潰涓嶅悓銆傞鍏堬紝JDOM 浠呬嬌鐢ㄥ叿浣撶被鑰屼笉浣跨敤鎺ュ彛銆傝繖鍦ㄦ煇浜涙柟闈㈢畝鍖栦簡 API錛屼絾鏄篃闄愬埗浜嗙伒媧繪с傜浜岋紝API 澶ч噺浣跨敤浜?Collections 綾伙紝綆鍖栦簡閭d簺宸茬粡鐔熸?zhèn)墭q欎簺綾葷殑 Java 寮鍙戣呯殑浣跨敤銆?
銆銆JDOM 鏂囨。澹版槑鍏剁洰鐨勬槸“浣跨敤 20%錛堟垨鏇村皯錛夌殑綺懼姏瑙e喅 80%錛堟垨鏇村錛塉ava/XML 闂”錛堟牴鎹涔犳洸綰垮亣瀹氫負 20%錛夈侸DOM 瀵逛簬澶у鏁?Java/XML 搴旂敤紼嬪簭鏉ヨ褰撶劧鏄湁鐢ㄧ殑錛屽茍涓斿ぇ澶氭暟寮鍙戣呭彂鐜?API 姣?DOM 瀹規(guī)槗鐞嗚В寰楀銆侸DOM 榪樺寘鎷紼嬪簭琛屼負鐨勭浉褰撳箍娉涙鏌ヤ互闃叉鐢ㄦ埛鍋氫換浣曞湪 XML 涓棤鎰忎箟鐨勪簨銆傜劧鑰岋紝瀹冧粛闇瑕佹?zhèn)ㄥ厖鍒嗙悊瑙?XML 浠ヤ究鍋氫竴浜涜秴鍑哄熀鏈殑宸ヤ綔錛堟垨鑰呯敋鑷崇悊瑙f煇浜涙儏鍐典笅鐨勯敊璇級銆傝繖涔熻鏄瘮瀛︿範 DOM 鎴?JDOM 鎺ュ彛閮芥洿鏈夋剰涔夌殑宸ヤ綔銆?
銆銆JDOM 鑷韓涓嶅寘鍚В鏋愬櫒銆傚畠閫氬父浣跨敤 SAX2 瑙f瀽鍣ㄦ潵瑙f瀽鍜岄獙璇佽緭鍏?XML 鏂囨。錛堝敖綆″畠榪樺彲浠ュ皢浠ュ墠鏋勯犵殑 DOM 琛ㄧず浣滀負杈撳叆錛夈傚畠鍖呭惈涓浜涜漿鎹㈠櫒浠ュ皢 JDOM 琛ㄧず杈撳嚭鎴?SAX2 浜嬩歡嫻併丏OM 妯″瀷鎴?XML 鏂囨湰鏂囨。銆侸DOM 鏄湪 Apache 璁稿彲璇佸彉浣撲笅鍙戝竷鐨勫紑鏀炬簮鐮併?
銆銆Bean鏂囦歡錛?
銆銆package com.test;
銆銆import java.io.*;
銆銆import java.util.*;
銆銆import org.jdom.*;
銆銆import org.jdom.input.*;
銆銆public class MyXMLReader {
銆銆public static void main(String arge[]) {
銆銆long lasting = System.currentTimeMillis();
銆銆try {
銆銆銆SAXBuilder builder = new SAXBuilder();
銆銆銆Document doc = builder.build(new File("data_10k.xml"));
銆銆銆Element foo = doc.getRootElement();
銆銆銆List allChildren = foo.getChildren();
銆銆銆for(int i=0;i錛渁llChildren.size();i++) {
銆銆銆銆System.out.print("杞︾墝鍙風爜:" + ((Element)allChildren.get(i)).getChild("NO").getText());
銆銆銆銆System.out.println(" 杞︿富鍦板潃:" + ((Element)allChildren.get(i)).getChild("ADDR").getText());
銆銆銆}
銆銆} catch (Exception e) {
銆銆銆e.printStackTrace();
銆銆}
銆銆System.out.println("榪愯鏃墮棿錛? + (System.currentTimeMillis() - lasting) + " 姣");
銆銆}
銆銆}
銆銆10k娑堣楁椂闂達細125 62 187 94
銆銆100k娑堣楁椂闂達細704 625 640 766
銆銆1000k娑堣楁椂闂達細27984 30750 27859 30656
銆銆10000k娑堣楁椂闂達細OutOfMemoryError
銆銆鏈鍚庢槸 DOM4J http://dom4j.sourceforge.net/
銆銆铏界劧 DOM4J 浠h〃浜嗗畬鍏ㄧ嫭绔嬬殑寮鍙戠粨鏋滐紝浣嗘渶鍒濓紝瀹冩槸 JDOM 鐨勪竴縐嶆櫤鑳藉垎鏀傚畠鍚堝茍浜嗚澶氳秴鍑哄熀鏈?XML 鏂囨。琛ㄧず鐨勫姛鑳斤紝鍖呮嫭闆嗘垚鐨?XPath 鏀寔銆乆ML Schema 鏀寔浠ュ強鐢ㄤ簬澶ф枃妗f垨嫻佸寲鏂囨。鐨勫熀浜庝簨浠剁殑澶勭悊銆傚畠榪樻彁渚涗簡鏋勫緩鏂囨。琛ㄧず鐨勯夐」錛屽畠閫氳繃 DOM4J API 鍜屾爣鍑?DOM 鎺ュ彛鍏鋒湁騫惰璁塊棶鍔熻兘銆備粠 2000 涓嬪崐騫村紑濮嬶紝瀹冨氨涓鐩村浜庡紑鍙戜箣涓?
銆銆涓烘敮鎸佹墍鏈夎繖浜涘姛鑳斤紝DOM4J 浣跨敤鎺ュ彛鍜屾娊璞″熀鏈被鏂規(guī)硶銆侱OM4J 澶ч噺浣跨敤浜?API 涓殑 Collections 綾伙紝浣嗘槸鍦ㄨ澶氭儏鍐典笅錛屽畠榪樻彁渚涗竴浜涙浛浠f柟娉曚互鍏佽鏇村ソ鐨勬ц兘鎴栨洿鐩存帴鐨勭紪鐮佹柟娉曘傜洿鎺ュソ澶勬槸錛岃櫧鐒?DOM4J 浠樺嚭浜嗘洿澶嶆潅鐨?API 鐨勪唬浠鳳紝浣嗘槸瀹冩彁渚涗簡姣?JDOM 澶у緱澶氱殑鐏墊椿鎬с?
銆銆鍦ㄦ坊鍔犵伒媧繪с乆Path 闆嗘垚鍜屽澶ф枃妗e鐞嗙殑鐩爣鏃訛紝DOM4J 鐨勭洰鏍囦笌 JDOM 鏄竴鏍風殑錛氶拡瀵?Java 寮鍙戣呯殑鏄撶敤鎬у拰鐩磋鎿嶄綔銆傚畠榪樿嚧鍔涗簬鎴愪負姣?JDOM 鏇村畬鏁寸殑瑙e喅鏂規(guī)錛屽疄鐜板湪鏈川涓婂鐞嗘墍鏈?Java/XML 闂鐨勭洰鏍囥傚湪瀹屾垚璇ョ洰鏍囨椂錛屽畠姣?JDOM 鏇村皯寮鴻皟闃叉涓嶆紜殑搴旂敤紼嬪簭琛屼負銆?
銆銆DOM4J 鏄竴涓潪甯擱潪甯鎬紭縐鐨凧ava XML API錛屽叿鏈夋ц兘浼樺紓銆佸姛鑳藉己澶у拰鏋佺鏄撶敤浣跨敤鐨勭壒鐐癸紝鍚屾椂瀹冧篃鏄竴涓紑鏀炬簮浠g爜鐨勮蔣浠躲傚浠婁綘鍙互鐪嬪埌瓚婃潵瓚婂鐨?Java 杞歡閮藉湪浣跨敤 DOM4J 鏉ヨ鍐?XML錛岀壒鍒煎緱涓鎻愮殑鏄繛 Sun 鐨?JAXM 涔熷湪鐢?DOM4J銆?
銆銆Bean鏂囦歡錛?
銆銆package com.test;
銆銆import java.io.*;
銆銆import java.util.*;
銆銆import org.dom4j.*;
銆銆import org.dom4j.io.*;
銆銆public class MyXMLReader {
銆銆public static void main(String arge[]) {
銆銆long lasting = System.currentTimeMillis();
銆銆try {
銆銆銆File f = new File("data_10k.xml");
銆銆銆SAXReader reader = new SAXReader();
銆銆銆Document doc = reader.read(f);
銆銆銆Element root = doc.getRootElement();
銆銆銆Element foo;
銆銆銆for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
銆銆銆銆foo = (Element) i.next();
銆銆銆銆System.out.print("杞︾墝鍙風爜:" + foo.elementText("NO"));
銆銆銆銆System.out.println(" 杞︿富鍦板潃:" + foo.elementText("ADDR"));
銆銆銆}
銆銆} catch (Exception e) {
銆銆銆e.printStackTrace();
銆銆}
銆銆System.out.println("榪愯鏃墮棿錛? + (System.currentTimeMillis() - lasting) + " 姣");
銆銆}
銆銆}
銆銆10k娑堣楁椂闂達細109 78 109 31
銆銆100k娑堣楁椂闂達細297 359 172 312
銆銆1000k娑堣楁椂闂達細2281 2359 2344 2469
銆銆10000k娑堣楁椂闂達細20938 19922 20031 21078
銆銆JDOM 鍜?DOM 鍦ㄦц兘嫻嬭瘯鏃惰〃鐜頒笉浣籌紝鍦ㄦ祴璇?10M 鏂囨。鏃跺唴瀛樻孩鍑恒傚湪灝忔枃妗f儏鍐典笅榪樺煎緱鑰冭檻浣跨敤 DOM 鍜?JDOM銆傝櫧鐒?JDOM 鐨勫紑鍙戣呭凡緇忚鏄庝粬浠湡鏈涘湪姝e紡鍙戣鐗堝墠涓撴敞鎬ц兘闂錛屼絾鏄粠鎬ц兘瑙傜偣鏉ョ湅錛屽畠紜疄娌℃湁鍊煎緱鎺ㄨ崘涔嬪銆傚彟澶栵紝DOM 浠嶆槸涓涓潪甯稿ソ鐨勯夋嫨銆侱OM 瀹炵幇騫挎硾搴旂敤浜庡縐嶇紪紼嬭璦銆傚畠榪樻槸璁稿鍏跺畠涓?XML 鐩稿叧鐨勬爣鍑嗙殑鍩虹錛屽洜涓哄畠姝e紡鑾峰緱 W3C 鎺ㄨ崘錛堜笌鍩轟簬闈炴爣鍑嗙殑 Java 妯″瀷鐩稿錛夛紝鎵浠ュ湪鏌愪簺綾誨瀷鐨勯」鐩腑鍙兘涔熼渶瑕佸畠錛堝鍦?JavaScript 涓嬌鐢?DOM錛夈?
銆銆SAX琛ㄧ幇杈冨ソ錛岃繖瑕佷緷璧栦簬瀹冪壒瀹氱殑瑙f瀽鏂瑰紡銆備竴涓?SAX 媯嫻嬪嵆灝嗗埌鏉ョ殑XML嫻侊紝浣嗗茍娌℃湁杞藉叆鍒板唴瀛橈紙褰撶劧褰揦ML嫻佽璇誨叆鏃訛紝浼氭湁閮ㄥ垎鏂囨。鏆傛椂闅愯棌鍦ㄥ唴瀛樹腑錛夈?
銆銆鏃犵枒錛孌OM4J鏄繖鍦烘祴璇曠殑鑾瘋儨鑰咃紝鐩墠璁稿寮婧愰」鐩腑澶ч噺閲囩敤 DOM4J錛屼緥濡傚ぇ鍚嶉紟榧庣殑 Hibernate 涔熺敤 DOM4J 鏉ヨ鍙?XML 閰嶇疆鏂囦歡銆傚鏋滀笉鑰冭檻鍙Щ妞嶆э紝閭e氨閲囩敤DOM4J鍚э紒
鏈鏂扮殑jdom鍙互鍒頒粬鐨勭綉绔欙細http://www.jdom.org鍘諱笅杞斤紝鐜板湪鐨勭増鏈槸1.0鐗堬紝涓嬭澆涔嬪悗灝嗗緱鍒?span xml:lang="EN-US">jdom-1.0.zip鏂囦歡錛岃В鍘嬪悗榪涘叆build鏂囦歡澶瑰皢鐪嬪埌涓涓悕涓?span xml:lang="EN-US">jdom.jar鐨勫寘錛岃繖涓氨鏄?span xml:lang="EN-US">jdom鐨勭被鍖呬簡錛屽皢瀹冨姞鍒頒綘鐨?span xml:lang="EN-US">classpath閲屽氨鍙互浣跨敤jdom鎻愪緵鐨勫悇縐嶅鐞?span xml:lang="EN-US">xml鐨勭被鍜屼粬浠殑鏂規(guī)硶浜嗐傚簲璇ユ敞鎰忕殑鏄湪瑙e帇鍚庣殑鏂囦歡澶歸噷榪樻湁涓涓?span xml:lang="EN-US">lib鏂囦歡澶癸紝閲岄潰淇濆瓨鐨勬槸浣跨敤jdom鐨勭幆澧冨寘錛屼笉榪囨垜鍦ㄦ垜鐨?span xml:lang="EN-US">jdk1.4涓嬩嬌鐢ㄦ病寮曠敤榪欎簺鍖呬竴鏍峰ソ鐢紝涓嶇煡閬撴槸jdk1.4涓凡緇忓寘鍚簡榪欎簺涓滆タ榪樻槸鍘熸潵鎴戠殑eclipse宸茬粡寮曠敤浜嗚繖浜涘寘錛屽懙鍛點?/span>
濂戒簡錛屼功褰掓浼狅紝鐜板湪寮濮嬩粙緇?span xml:lang="EN-US">jdom鍖呯殑浣跨敤銆?br />
jdom鍖呯殑緇撴瀯鍖呮嫭錛?/span>
org.jdom 鍖呭惈浜嗘墍鏈夌殑xml鏂囨。瑕佺礌鐨?span xml:lang="EN-US">java綾?/span>
org.jdom.adapters 鍖呭惈浜嗕笌dom閫傞厤鐨?span xml:lang="EN-US">java綾?/span>
org.jdom.filter 鍖呭惈浜?span xml:lang="EN-US">xml鏂囨。鐨勮繃婊ゅ櫒綾?/span>
org.jdom.input 鍖呭惈浜嗚鍙?span xml:lang="EN-US">xml鏂囨。鐨勭被
org.jdom.output 鍖呭惈浜嗗啓鍏?span xml:lang="EN-US">xml鏂囨。鐨勯浄
org.jdom.transform 鍖呭惈浜嗗皢jdom xml鏂囨。鎺ュ彛杞崲涓哄叾浠?span xml:lang="EN-US">xml鏂囨。鎺ュ彛
org.jdom.xpath 鍖呭惈浜嗗xml鏂囨。xpath鎿嶄綔鐨勭被
涓嬮潰灝嗛氳繃涓涓緥瀛愪粙緇?span xml:lang="EN-US">jdom鐨勫父鐢ㄦ搷浣?/span>
鐢熸垚xml鏂囨。錛?/span>
涓嬮潰鐨勭被灝嗙敓鎴愪竴涓?span xml:lang="EN-US">xml鏂囨。錛?/span>
/*
* Created on 2004-10-9
*
* 鍐欏叆xml鏂囦歡鐨勪緥瀛?/span>
*/
/**
* @author lnman
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import java.io.*;
import org.jdom.*;
import org.jdom.output.*;
public class WriteXML
{
public void BuildXML() throws Exception
{
Element root,student,number,name,age;
root = new Element("student-info"); //鐢熸垚鏍瑰厓绱狅細student-info
student = new Element("student"); //鐢熸垚鍏冪礌錛?span xml:lang="EN-US">student,璇ュ厓绱犱腑灝嗗寘鍚厓绱?span xml:lang="EN-US">number,name,age
number = new Element("number");
name = new Element("name");
age = new Element("age");
Document doc = new Document(root); //灝嗘牴鍏冪礌妞嶅叆鏂囨。doc涓?/span>
number.setText("001");
name.setText("lnman");
age.setText("24");
student.addContent(number);
student.addContent(name);
student.addContent(age);
root.addContent(student);
Format format = Format.getCompactFormat();
format.setEncoding("gb2312"); //璁劇疆xml鏂囦歡鐨勫瓧絎︿負gb2312
format.setIndent(" "); //璁劇疆xml鏂囦歡鐨勭緝榪涗負4涓┖鏍?/span>
XMLOutputter XMLOut = new XMLOutputter(format);//鍦ㄥ厓绱犲悗鎹㈣錛屾瘡涓灞傚厓绱犵緝鎺掑洓鏍?/span>
XMLOut.output(doc, new FileOutputStream("studentinfo.xml"));
}
public static void main(String[] args) throws Exception
{
WriteXML w = new WriteXML();
System.out.println("Now we build an XML document .....");
w.BuildXML();
System.out.println("finished!");
}
}
鐢熸垚鐨?span xml:lang="EN-US">xml鏂囨。涓猴細
<?xml version="1.0" encoding="gb2312"?>
<student-info>
<student>
<number>001</number>
<name>lnman</name>
<age>24</age>
</student>
</student-info>
璇誨彇xml鏂囨。鐨勪緥瀛愶細
/*
* Created on 2004-10-9
*
*鐢?span xml:lang="EN-US">jdom璇誨彇xml鏂囨。鐨勪緥瀛?/span>
*
*/
/**
* @author lnman
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
import org.jdom.output.*;
import org.jdom.input.*;
import org.jdom.*;
import java.io.*;
import java.util.*;
public class ReadXML
{
public static void main(String[] args) throws Exception
{
SAXBuilder builder = new SAXBuilder();
Document read_doc = builder.build("studentinfo.xml");
Element stu = read_doc.getRootElement();
List list = stu.getChildren("student");
for(int i = 0;i < list.size();i++)
{
Element e = (Element)list.get(i);
String str_number = e.getChildText("number");
String str_name = e.getChildText("name");
String str_age = e.getChildText("age");
System.out.println("---------STUDENT--------------");
System.out.println("NUMBER:" + str_number);
System.out.println("NAME:" + str_name);
System.out.println("AGE:" + str_age);
System.out.println("------------------------------");
System.out.println();
}
}
}
Xpath渚嬪瓙錛?/span>
JDOM鐨勫叧浜?span xml:lang="EN-US">XPATH鐨?span xml:lang="EN-US">api鍦?span xml:lang="EN-US">org.jdom.xpath榪欎釜鍖呴噷銆傝繖涓寘涓嬶紝鏈変竴涓娊璞$被XPath.java鍜屽疄鐜扮被JaxenXPath.java錛?nbsp;浣跨敤鏃跺厛鐢?span xml:lang="EN-US">XPath綾葷殑闈欐佹柟娉?span xml:lang="EN-US">newInstance(String xpath)寰楀埌XPath瀵硅薄錛岀劧鍚庤皟鐢ㄥ畠鐨?span xml:lang="EN-US">selectNodes(Object context)鏂規(guī)硶鎴?span xml:lang="EN-US">selectSingleNode(Object context)鏂規(guī)硶錛屽墠鑰呮牴鎹?span xml:lang="EN-US">xpath璇彞榪斿洖涓緇勮妭鐐?span xml:lang="EN-US">(List瀵硅薄)錛涘悗鑰呮牴鎹竴涓?span xml:lang="EN-US">xpath璇彞榪斿洖絎﹀悎鏉′歡鐨勭涓涓妭鐐?span xml:lang="EN-US">(Object綾誨瀷)銆傝鐪?span xml:lang="EN-US">jdom-1.0鑷甫鐨勮寖渚嬬▼搴忥細
瀹冨垎鏋愬湪web.xml鏂囦歡涓殑娉ㄥ唽鐨?span xml:lang="EN-US">servlet鐨勪釜鏁板強鍙傛暟涓暟錛屽茍杈撳嚭瑙掕壊鍚嶃?
web.xml鏂囦歡錛?
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
-->
<web-app>
<servlet>
<servlet-name>snoop</servlet-name>
<servlet-class>SnoopServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>file </servlet-name>
<servlet-class>ViewFile</servlet-class>
<init-param>
<param-name>initial</param-name>
<param-value>1000</param-value>
<description>The initial value for the counter <!-- optional --></description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mv</servlet-name>
<url-pattern>*.wm</url-pattern>
</servlet-mapping>
<distributed/>
<security-role>
<role-name>manager</role-name>
<role-name>director</role-name>
<role-name>president</role-name>
</security-role>
</web-app>
澶勭悊紼嬪簭錛?
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.xpath.*;
/**
* <p><code>XPathReader</code> demonstrates how to
* read a Servlet 2.2 Web Archive file using XPath.
* </p>
*
* @author Jason Hunter
* @version 1.0
*/
public class XPathReader {
public static void main(String[] args) throws IOException, JDOMException {
if (args.length != 1) {
System.err.println("Usage: java XPathReader web.xml");
return;
}
String filename = args[0];//浠庡懡浠よ杈撳叆web.xml
PrintStream out = System.out;
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(new File(filename));//寰楀埌Document瀵硅薄
// Print servlet information
XPath servletPath = XPath.newInstance("http://servlet");//,閫夋嫨浠繪剰璺緞涓?span xml:lang="EN-US">servlet鍏冪礌
List servlets = servletPath.selectNodes(doc);//榪斿洖鎵鏈夌殑servlet鍏冪礌銆?
out.println("This WAR has "+ servlets.size() +" registered servlets:");
Iterator i = servlets.iterator();
while (i.hasNext()) {//杈撳嚭servlet淇℃伅
Element servlet = (Element) i.next();
out.print(""t" + servlet.getChild("servlet-name")
.getTextTrim() +
" for " + servlet.getChild("servlet-class")
.getTextTrim());
List initParams = servlet.getChildren("init-param");
out.println(" (it has " + initParams.size() + " init params)");
}
// Print security role information
XPath rolePath = XPath.newInstance("http://security-role/role-name/text()");
List roleNames = rolePath.selectNodes(doc);//寰楀埌鎵鏈夌殑瑙掕壊鍚?
if (roleNames.size() == 0) {
out.println("This WAR contains no roles");
} else {
out.println("This WAR contains " + roleNames.size() + " roles:");
i = roleNames.iterator();
while (i.hasNext()) {//杈撳嚭瑙掕壊鍚?
out.println(""t" + ((Text)i.next()).getTextTrim());
}
}
}
}
杈撳嚭緇撴灉:
C:"java>java XPathReader web.xml
This WAR has 2 registered servlets:
snoop for SnoopServlet (it has 0 init params)
file for ViewFile (it has 1 init params)
This WAR contains 3 roles:
manager
director
president