??xml version="1.0" encoding="utf-8" standalone="yes"?>
预备 试环境Q? AMD毒龙1.4G OC 1.5G?56M DDR333、Windows2000 Server SP4、Sun JDK 1.4.1+Eclipse 2.1+Resin 2.1.8Q在Debug模式下测试? XML文g格式如下Q? Q?xml version="1.0" encoding="GB2312"?Q<RESULTQ<VALUEQ? QNOQA1234Q?NOQ? QADDRQ四川省XX县XX镇XX路XDXXP/ADDRQ</VALUEQ<VALUEQ? QNOQB1234Q?NOQ? QADDRQ四川省XX?jng)XX乡XX村XXl</ADDRQ</VALUEQ</RESULTQ? 试Ҏ(gu)Q? 采用JSP端调用Bean(至于Z么采用JSP来调用,请参考:(x)http://blog.csdn.net/rosen/archive/2004/10/15/138324.aspx)Q让每一U方案分别解?0K?00K?000K?0000K的XML文gQ计其消耗时?单位:毫秒)? JSP文gQ? Q?@ page contentType="text/html; charset=gb2312" %Q<%@ page import="com.test.*"%Q? QhtmlQ<bodyQ<%String args[]={""};MyXMLReader.main(args);%Q</bodyQ</htmlQ? 试 首先出场的是DOM(JAXP Crimson解析? DOM是用与^台和语言无关的方式表CXML文档的官方W3C标准。DOM是以层次l构l织的节Ҏ(gu)信息片断的集合。这个层ơ结构允许开发h员在?wi)中L特定信息。分析该l构通常需要加载整个文档和构造层ơ结构,然后才能做Q何工作。由于它是基于信息层ơ的Q因而DOM被认为是Z?wi)或Z对象的。DOM以及(qing)q义的基于树(wi)的处理具有几个优炏V首先,׃?wi)在内存中是持久的,因此可以修改它以便应用程序能?gu)据和l构作出更改。它q可以在M时候在?wi)中上下DQ而不是像SAX那样是一ơ性的处理。DOM使用h也要单得多? 另一斚wQ对于特别大的文档,解析和加载整个文档可能很慢且很耗资源,因此使用其他手段来处理这L(fng)数据?x)更好。这些基于事件的模型Q比如SAX? Bean文gQ? package com.test; import java.io.*;import java.util.*;import org.w3c.dom.*;import javax.xml.parsers.*; public class MyXMLReader{ public static void main(String arge[]){ long lasting =System.currentTimeMillis(); try{ File f=new File("data_10k.xml"); DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); DocumentBuilder builder=factory.newDocumentBuilder(); Document doc = builder.parse(f); NodeList nl = doc.getElementsByTagName("VALUE"); for (int i=0;iQnl.getLength();i++){ System.out.print("车牌L(fng):" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); } }catch(Exception e){ e.printStackTrace(); } System.out.println("q行旉Q?+(System.currentTimeMillis() - lasting)+"毫秒");}} 10k消耗时_(d)(x)265 203 219 172 100k消耗时_(d)(x)9172 9016 8891 9000 1000k消耗时_(d)(x)691719 675407 708375 739656 10000k消耗时_(d)(x)OutOfMemoryError 接着是SAX q种处理的优炚w常类g媒体的优点。分析能够立卛_始,而不是等待所有的数据被处理。而且Q由于应用程序只是在d数据时检查数据,因此不需要将数据存储在内存中。这对于大型文档来说是个巨大的优炏V事实上Q应用程序甚至不必解析整个文档;它可以在某个条g得到满时停止解析。一般来_(d)SAXq比它的替代者DOM快许多? 选择DOMq是选择SAXQ? 对于需要自q写代码来处理XML文档的开发h员来_(d) 选择DOMq是SAX解析模型是一个非帔R要的设计决策? DOM采用建立?wi)Şl构的方式访问XML文档Q而SAX采用的事件模型? DOM解析器把XML文档转化Z个包含其内容的树(wi)Qƈ可以Ҏ(gu)(wi)q行遍历。用DOM解析模型的优Ҏ(gu)~程Ҏ(gu)Q开发h员只需要调用徏?wi)的指o(h)Q然后利用navigation APIs讉K所需的树(wi)节点来完成Q务。可以很Ҏ(gu)的添加和修改?wi)中的元素。然而由于用DOM解析器的时候需要处理整个XML文档Q所以对性能和内存的要求比较高,其是遇到很大的XML文g的时候。由于它的遍历能力,DOM解析器常用于XML文档需要频J的改变的服务中? SAX解析器采用了(jin)Z事g的模型,它在解析XML文档的时候可以触发一pd的事Ӟ当发现给定的tag的时候,它可以激zM个回调方法,告诉该方法制定的标签已经扑ֈ。SAX对内存的要求通常?x)比较低Q因为它让开发h员自己来军_所要处理的tag。特别是当开发h员只需要处理文档中所包含的部分数据时QSAXq种扩展能力得到?jin)更好的体现。但用SAX解析器的时候编码工作会(x)比较困难Q而且很难同时讉K同一个文档中的多处不同数据? Bean文gQ? 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("q行旉Q? + (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("车牌L(fng)Q? + 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消耗时_(d)(x)110 47 109 78 100k消耗时_(d)(x)344 406 375 422 1000k消耗时_(d)(x)3234 3281 3688 3312 10000k消耗时_(d)(x)32578 34313 31797 31890 30328 然后是JDOM http://www.jdom.org/ JDOM的目的是成ؓ(f)Java特定文档模型Q它化与XML的交互ƈ且比使用DOM实现更快。由于是W一个Java特定模型QJDOM一直得到大力推q和?j)进。正在考虑通过“Java规范hJSR-102”将它最l用作“Java标准扩展”。从2000q初已l开始了(jin)JDOM开发? JDOM与DOM主要有两斚w不同。首先,JDOM仅用具体类而不使用接口。这在某些方面简化了(jin)APIQ但是也限制?jin)灵zL。第二,API大量使用?jin)Collectionsc,化了(jin)那些已经熟?zhn)q些cȝJava开发者的使用? JDOM文档声明其目的是“?0%(或更?的精力解?0%(或更?Java/XML问题?Ҏ(gu)学习(fn)曲线假定?0%)。JDOM对于大多数Java/XML应用E序来说当然是有用的Qƈ且大多数开发者发现API比DOMҎ(gu)理解得多。JDOMq包括对E序行ؓ(f)的相当广泛检查以防止用户做Q何在XML中无意义的事。然而,它仍需要?zhn)充分理解XML以便做一些超出基本的工作(或者甚至理解某些情况下的错?。这也许是比学习(fn)DOM或JDOM接口都更有意义的工作? JDOM自n不包含解析器。它通常使用SAX2解析器来解析和验证输入XML文档(管它还可以以前构造的DOM表示作ؓ(f)输入)。它包含一些{换器以将JDOM表示输出成SAX2事g、DOM模型或XML文本文档。JDOM是在Apache许可证变体下发布的开放源码? Bean文gQ? 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;iQallChildren.size();i++) { System.out.print("车牌L(fng):" + ((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("q行旉Q? + (System.currentTimeMillis() - lasting) + "毫秒");}} 10k消耗时_(d)(x)125 62 187 94 100k消耗时_(d)(x)704 625 640 766 1000k消耗时_(d)(x)27984 30750 27859 30656 10000k消耗时_(d)(x)OutOfMemoryError 最后是DOM4J http://dom4j.sourceforge.net/ 虽然DOM4J代表?jin)完全独立的开发结果,但最初,它是JDOM的一U智能分支。它合ƈ?jin)许多超出基本XML文档表示的功能,包括集成的XPath支持、XML Schema支持以及(qing)用于大文档或化文档的基于事件的处理。它q提供了(jin)构徏文档表示的选项Q它通过DOM4J API和标准DOM接口hq行讉K功能。从2000下半q开始,它就一直处于开发之中? 为支持所有这些功能,DOM4J使用接口和抽象基本类Ҏ(gu)。DOM4J大量使用?jin)API中的Collectionsc,但是在许多情况下Q它q提供一些替代方法以允许更好的性能或更直接的编码方法。直接好处是Q虽然DOM4J付出?jin)更复杂的API的代P但是它提供了(jin)比JDOM大得多的灉|性? 在添加灵zL、XPath集成和对大文档处理的目标ӞDOM4J的目标与JDOM是一L(fng)Q针对Java开发者的易用性和直观操作。它q致力于成ؓ(f)比JDOM更完整的解决Ҏ(gu)Q实现在本质上处理所有Java/XML问题的目标。在完成该目标时Q它比JDOM更少防止不正的应用E序行ؓ(f)? DOM4J是一个非帔R怼U的Java XML APIQ具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的Y件。如今你可以看到来多的Java软g都在使用DOM4J来读写XMLQ特别值得一提的是连Sun的JAXM也在用DOM4J? Bean文gQ? 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("车牌L(fng):" + foo.elementText("NO")); System.out.println("车主地址:" + foo.elementText("ADDR")); } } catch (Exception e) { e.printStackTrace(); } System.out.println("q行旉Q? + (System.currentTimeMillis() - lasting) + "毫秒");}} 10k消耗时_(d)(x)109 78 109 31 100k消耗时_(d)(x)297 359 172 312 1000k消耗时_(d)(x)2281 2359 2344 2469 10000k消耗时_(d)(x)20938 19922 20031 21078 JDOM和DOM在性能试时表C佻I在测?0M文档时内存溢出。在文档情况下q值得考虑使用DOM和JDOM。虽然JDOM的开发者已l说明他们期望在正式发行版前专注性能问题Q但是从性能观点来看Q它实没有值得推荐之处。另外,DOM仍是一个非常好的选择。DOM实现q泛应用于多U编E语a。它q是许多其它与XML相关的标准的基础Q因为它正式获得W3C推荐(与基于非标准的Java模型相对)Q所以在某些cd的项目中可能也需要它(如在JavaScript中用DOM)? SAX表现较好Q这要依赖于它特定的解析方式。一个SAX(g)即到来的XML,但ƈ没有载入到内?当然当XML被dӞ?x)有部分文档暂时隐藏在内存?? 无疑QDOM4J是这场测试的莯者,目前许多开源项目中大量采用DOM4JQ例如大名鼎鼎的Hibernate也用DOM4J来读取XML配置文g。如果不考虑可移植性,那就采用DOM4J吧!(?rosenQ?/td> |
<?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文档的Java 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);
}