网上都说DOM4J加载大型的xml开销很大, 这里来做一个实验
package xml.dom4j;
public class Test {
public static void main(String[] args) {
// operlog.xml 10万行 4MB 消耗JVM内存 11M
// operlog2.xml 100行 5k 消耗JVM内存 0
Test.loadLogXML("C:\\WKP_HP1\\TestCaseHp1\\src\\operlog2.xml");
}
public static void loadLogXML(String file) {
Runtime runtime = Runtime.getRuntime();
System.out.println("当前虚拟机最大可用内存为: " + runtime.maxMemory() / 1024 / 1024 + "M");
System.out.println("当前虚拟机已占用内存: " + runtime.totalMemory() / 1024 / 1024 + "M");
try {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new File(file));
} catch (DocumentException e) {
System.out.println("读取配置文件错误");
return;
}
System.out.println("当前虚拟机已占用内存: " + runtime.totalMemory() / 1024 / 1024 + "M");
Element root = doc.getRootElement();
List poNodes = root.elements("po");
for (Iterator it = (Iterator) poNodes.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
String beanName = elm.attributeValue("name");
String moduleName = elm.attributeValue("moduleName");
String sysId = elm.attributeValue("sysId");
List nodes = elm.elements("field");
List<String> listField = new ArrayList<String>();
for (Iterator it2 = (Iterator) nodes.iterator(); it2.hasNext();) {
Element elm2 = (Element) it2.next();
String fieldName = elm2.attributeValue("name");
String CNName = elm2.getText();
//System.out.println((fieldName + "," + CNName));
}
}
} catch (Exception e) {
System.out.println("加载日志配置文件时,碰到运行时异常: " + e.toString());
}
}
}
最后发现 加载 10万行数据的 jvm也只用了11M, 完全可以接受的。
附xml文件
<doc>
<po name="cia.common.db.orm.po.TblBkeMchntUsrInf" moduleName="MMG" sysId="2">
<field name="usrId">用户ID</field>
<field name="mchntCd">所属机构号</field>
</po>
<po name="cia.common.db.orm.po.TblBkeMchntUsrpwdInf" moduleName="MMG" sysId="2">
<field name="mchntCd">商户号</field>
<field name="usrId">用户ID</field>
<field name="usrPwd">用户密码</field>
</po>
<!-- PPP机构信息管理 -->
<po name="cia.common.db.orm.po.TblBkePppInsInfo" moduleName="CIM" sysId="2">
<field name="insTp">机构类型</field>
<field name="pppInsCd">机构号</field>
<field name="insNm">机构名称</field>
</po>
<!-- 全卡bin管理 -->
<po name="cia.common.db.orm.po.TblBkeBin" moduleName="BIM" sysId="2">
<field name="cardBin">卡BIN</field>
<field name="panLen">卡号长度</field>
<field name="issInsCd">发卡机构代码</field>
<field name="issInsCnNm">发卡机构名称</field>
<field name="cardAttr">卡性质</field>
<field name="cardBrand">卡品牌</field>
<field name="cardCata">卡类别</field>
</po>
</doc>
DOM4J 用来加载系统的配置文件还是OK的。 但用来接收外系统的数据就不合适了
1, 因为数据量的问题
2, DOM4J 适合解析固定tab name的xml, 对于处理下列的XML就杯具了。
<?xml version="1.0" encoding="utf-8"?>
<!--XML file generated by Excel Input Tool-->
<ConfigMessage xmlns="http://schemas.hp.com/CfM/ucmdb/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.hp.com/CfM/ucmdb/ ConfigMessage.xsd">
<ConfigMessageHeader>
<cmdbContext>cis</cmdbContext>
<dataSource>CIS DERIVED VALUE - will be a valid MDM Company Code</dataSource>
<dataSourceContact/>
<dataSourceContactEmail>CPLUS001@cplus.com</dataSourceContactEmail>
<simulate>false</simulate>
</ConfigMessageHeader>
<CIsAndRelations>
<CIsForUpdate>
<CI>
<netdevice>
<data_adminstate>0</data_adminstate>
<data_externalid>CPLUS_consoleswitch_002</data_externalid>
<data_note>ConsoleSwitch_Note</data_note>
<data_operationstate>0</data_operationstate>
<data_origin>ESL</data_origin>
<description>Description_ConsoleSwitch</description>
<document_list>DocumentsDocumentsDocumentsDocumentsDocumentsDocum</document_list>
<name>ConsoleSwitch_Name_Judy_002</name>
<TenantOwner>ACT</TenantOwner>
<TenantsUses>CPLUS</TenantsUses>
<user_label>ConsoleSwitch_UserLable_Judy_002</user_label>
<related_ciexternalid></related_ciexternalid>
<related_cit></related_cit>
<hp_envrmt_type_nm>Pending</hp_envrmt_type_nm>
<hp_user_defined_attribute1_txt>Attribute1</hp_user_defined_attribute1_txt>
<hp_user_defined_attribute2_txt>Attribute2</hp_user_defined_attribute2_txt>
<hp_user_defined_attribute3_txt>Attribute3</hp_user_defined_attribute3_txt>
<hp_user_defined_attribute4_txt>Attribute4</hp_user_defined_attribute4_txt>
<hp_user_defined_attribute5_txt>Attribute5</hp_user_defined_attribute5_txt>
<hp_regulatory_status_flg>true</hp_regulatory_status_flg>
<sacm_lifecycle_status>Pending</sacm_lifecycle_status>
<hp_inventory_id>InventoryId123</hp_inventory_id>
<codepage>CodePage</codepage>
<hp_cust_req_excpn_flg>true</hp_cust_req_excpn_flg>
<hp_location_txt>VIA|3F</hp_location_txt>
<hp_ci_alias_nm>AliasName123</hp_ci_alias_nm>
<bios_asset_tag>BiosAssetTag01</bios_asset_tag>
<bios_serial_number>N123456</bios_serial_number>
<bios_uuid>BiosUuid001</bios_uuid>
<calculated_location>CalculatedLocation001</calculated_location>
<domain_name>HP.com</domain_name>
<host_iscomplete>true</host_iscomplete>
<host_isdesktop>true</host_isdesktop>
<host_isroute>true</host_isroute>
<host_isvirtual>true</host_isvirtual>
<host_key>HostKey1234</host_key>
<host_nnm_uid>123456</host_nnm_uid>
<host_osaccuracy>HostOSA</host_osaccuracy>
<host_osrelease>HostOSRelease123</host_osrelease>
<host_servertype>HostServerType123</host_servertype>
<memory_size>1024</memory_size>
<net_bios_name>NetBiosName1213</net_bios_name>
<node_family>NodeFamily123</node_family>
<node_model>AIX 6.1</node_model>
<node_role>ConsoleSwitch</node_role>
<os_family>baremetal_hypervisor</os_family>
<primary_dns_name>PrimaryDnsName123</primary_dns_name>
<serial_number>SerialNo123</serial_number>
<swap_memory_size>100</swap_memory_size>
<sys_object_id>SysObjectId123</sys_object_id>
<vendor>IBM</vendor>
<hp_product_code_txt>ProductNo123</hp_product_code_txt>
<hp_approval_group_txt>StringVal456</hp_approval_group_txt>
<hp_approval_sequence_nbr>59</hp_approval_sequence_nbr>
</netdevice>
</CI>
</CIsForUpdate>
<relationsForUpdate>
</relationsForUpdate>
</CIsAndRelations>
</ConfigMessage>
这时候适合用SAX来解析
package com.hp.ucmdb.util;
import java.io.FileInputStream;
import java.io.IOException;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class ParseFileUtils extends DefaultHandler {
public static void main(String[] args) throws SAXException, IOException {
ParseFileUtils instance = new ParseFileUtils();
instance.doParsing("c:\\222.xml");
}
// method for file validation and parsing.
public void doParsing(String fileName) throws SAXException,
IOException {
XMLReader parser = XMLReaderFactory.createXMLReader();
ContentHandler cHandler = this;
parser.setContentHandler(cHandler);
FileInputStream input = null;
try {
input = new FileInputStream(fileName);
parser.parse(new InputSource(input));
} catch (SAXParseException ex) {
throw ex;
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
}
}
}
}
String v = "";
StringBuffer sb = null;
public void startElement(String namespace, String localName,
String qualifiedName, Attributes atts) throws SAXException {
sb = new StringBuffer();
System.out.println(localName);
}
public void characters(char[] ch, int start, int length)
throws SAXException {
sb.append(ch, start, length);
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
String strValue = sb.toString();
if(strValue != null && !"".equals(strValue) && !"".equals(strValue.trim())){
System.out.println(" v: " + strValue);
}
sb = new StringBuffer();
}
}
另外, DOM4J 和 SAX 都可以进行schema验证
分享到:
相关推荐
项目下包含dom4j的包 是里用dom4j的sax解析方式 sax解析打文件比dom速度快,该项目为测试项目
dom4j下sax解析xml文件 sax解析大文件比dom更快 更节省内存,xml测试文件在src下,可直接运行应用jar包在lib下,无需导入
一个完整的详细的Dom4JSAX解析实例和SAX解析实例例子!
dom,dom4j,sax解析xml 文件经典实例,内置详细注解
本程序实现了实现dom+dom4J创建XML到解析XML,并实行了SAX 解析XML,大家值得学习!
DOM4J_SAX_DOM_XML的实例DOM4J_SAX_DOM_XML的实例
有四种方法 DOM DOM4j JDOM SAX 和相对路径的写法 ,代码是有注释的,通用型的……应该对大家有帮助
4种最主流、最全面、最详细的生成和解析xml的4种方式,包括Dom,Dom4j,Sax,JDom,读懂了绝对好用xml,非常好的入门项目,而且精心排版,逻辑清晰
用用4中方式(dom4j、jdom、dom、sax)解析xml
Java 四种方式操作xml,包括xml,dom,sax,jdom这四种方式的一个简单例子。
dom4j基础入门文档,与 W3C DOM API 相比,使用 dom4j 所包含的解析器的好处是 dom4j 拥有本地的 XPath 支持。
xml 三种解析方式dom,sax,dom4j 内涵jar包,xml我默认放在本地D盘的。 三种方式的代码,我自己看别人代码总结。
XML的四种解析器(dom,sax,jdom,dom4j)原理及性能比较,超详细XML的四种解析器原理及性能比较,超详细
对于xml&dom&sax&dom4j 文件解析的代码总结
使用环境eclipse4.8,jdk1.8 使用sax,pull,dom4j解析XML,对xml的简单解析,sax,pull,dom4j的简单使用
java中四种操作(dom、sax、jdom、dom4j)xml方式详解与
Java使用sax、dom、dom4j解析xml文档的代码,包含dom4j的jar包。
这个文件夹里边的代码是三种方式对xml进行解析,其中dom,dom4j,对小米进行了增删改查,sax对xml进行了解析
Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP. Dom4j是一个易用...
org.dom4j.io Provides input and output via SAX and DOM together with writing dom4j objects to streams as XML text. org.dom4j.jaxb org.dom4j.rule A Pattern based XML rule engine which implements the ...