先简单说下前三种方式:

DOM方式:个人理解类似.net的XmlDocument,解析的时候效率不高,占用内存,不适合大XML的解析;

SAX方式:基于事件的解析,当解析到xml的某个部分的时候,会触发特定事件,可以在自定义的解析类中定义当事件触发时要做得事情;个人感觉一种很另类的方式,不知道.Net体系下是否有没有类似的方式?

StAX方式:个人理解类似.net的XmlReader方式,效率高,占用内存少,适用大XML的解析;

不过SAX方式之前也用过,本文主要介绍JAXB,这里只贴下主要代码:import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class ConfigParser extends DefaultHandler {

private String currentConfigSection;

public SysConfigItem sysConfig;

public List interfaceConfigList;

public List ftpConfigList;

public List adapterConfigList;

public void startDocument() throws SAXException {

sysConfig = new SysConfigItem();

interfaceConfigList = new ArrayList();

ftpConfigList = new ArrayList();

adapterConfigList = new ArrayList();

}

public void endDocument() throws SAXException {

}

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

if (qName.equalsIgnoreCase("Item") && attributes.getLength() > 0) {

if (currentConfigSection.equalsIgnoreCase("SysConfigItem")) {

sysConfig = new SysConfigItem(attributes);

} else if (currentConfigSection.equalsIgnoreCase("InterfaceConfigItems")) {

interfaceConfigList.add(new InterfaceConfigItem(attributes));

} else if (currentConfigSection.equalsIgnoreCase("FtpConfigItems")) {

ftpConfigList.add(new FtpConfigItem(attributes));

} else if (currentConfigSection.equalsIgnoreCase("AdapterConfigItems")) {

adapterConfigList.add(new AdapterConfigItem(attributes));

}

} else {

currentConfigSection = qName;

}

}

public void endElement(String uri, String localName, String qName) throws SAXException {

}

public void characters(char ch[], int start, int length) throws SAXException {

}

}import java.lang.reflect.Field;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.xml.sax.Attributes;

public class ConfigItemBase {

private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

public ConfigItemBase() {

}

/**

* 目前只支持几种常用类型 如果需要支持其他类型,请修改这里的代码

*

* @param attributes

*/

public ConfigItemBase(Attributes attributes) {

Class> cls = this.getClass();

Field[] fields = cls.getDeclaredFields();

for (Field field : fields) {

String fieldType = field.getType().getSimpleName();

for (int i = 0; i < attributes.getLength(); i++) {

if (attributes.getQName(i).equalsIgnoreCase(field.getName())) {

field.setAccessible(true);

try {

if (fieldType.equalsIgnoreCase("String")) {

field.set(this, attributes.getValue(attributes.getQName(i)));

} else if (fieldType.equalsIgnoreCase("Integer")) {

field.set(this, Integer.valueOf(attributes.getValue(attributes.getQName(i))));

} else if (fieldType.equalsIgnoreCase("Double")) {

field.set(this, Double.valueOf(attributes.getValue(attributes.getQName(i))));

} else if (fieldType.equalsIgnoreCase("Date")) {

field.set(this, GetDate(attributes.getValue(attributes.getQName(i))));

} else {

System.out.println("Warning:Unhandler Field(" + field.getName() + "-" + fieldType + ")");

}

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

break;

}

}

}

}

public String toString() {

String result = "";

Class> cls = this.getClass();

String classNameString = cls.getName();

result += classNameString.substring(classNameString.lastIndexOf('.') + 1, classNameString.length()) + ":";

Field[] fields = cls.getDeclaredFields();

for (Field field : fields) {

try {

result += field.getName() + "=" + field.get(this) + ";";

} catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

return result;

}

/**

* 处理时间类型属性(时间格式要求为:yyyy-MM-dd hh:mm:ss)

*

* @param dateString

* @return

*/

private static Date GetDate(String dateString) {

Date date = null;

try {

date = dateFormat.parse(dateString);

} catch (ParseException e) {

e.printStackTrace();

}

return date;

}

}

下面重点介绍一下最方便的:JAXB(Java Architecture for XML Binding)

这里用比较复杂的移动BatchSyncOrderRelationReq接口XML做为示例(感觉能解这个大家基本上够用了),报文格式如下(SvcCont里的CDATA内容是报文体,太恶心了):<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>

0100

0

BIP2B518

T2101518

0

BOSS

routeType

XXXX

routeValue

2013041017222313925676

2013041017222313925676

20130410172223

rspType

rspCode

rspDesc

210001BIP2B518130410172223651627

2

210001BIP2B518130410172224341871

oprTime1

actionId1

brand1

effTime1

expireTime1

feeUserId1

destUserId1

actionId1

servType1

subServType1

spId1

spServId1

accessMode1

0

feeType1

210001BIP2B518130410172224420909

oprTime2

actionId2

brand2

effTime2

expireTime2

feeUserId2

destUserId2

actionId2

servType2

subServType2

spId2

spServId2

accessMode2

0

feeType2

]]>

解码代码如下:@XmlRootElement(name = "batchSyncOrderRelationReq")

@XmlAccessorType(XmlAccessType.FIELD)

public class BatchSyncOrderRelationReq extends BossMessage {

@XmlElement(name = "msgTransactionID")

private String msgTransactionId = "";

@XmlElement(name = "reqNum")

private String reqNum = "";

@XmlElement(name = "reqBody")

private List reqBodyList;

public BatchSyncOrderRelationReq() {

}

public String getMsgTransactionId() {

return this.msgTransactionId;

}

public void setMsgTransactionId(String msgTransactionId) {

this.msgTransactionId = msgTransactionId;

}

public String getReqNum() {

return this.reqNum;

}

public void setReqNum(String reqNum) {

this.reqNum = reqNum;

}

public List getReqBodyList() {

return this.reqBodyList;

}

public void setReqBodyList(List reqBodyList) {

this.reqBodyList = reqBodyList;

}

@Override

public BatchSyncOrderRelationReq Deserialized(String interBossXmlContent) throws BusinessException {

try {

// deserialized for head

JAXBContext jaxbCxt4Head = JAXBContext.newInstance(MessageHead.class);

Unmarshaller unmarshaller4Head = jaxbCxt4Head.createUnmarshaller();

MessageHead head = (MessageHead) unmarshaller4Head.unmarshal(new StringReader(interBossXmlContent));

// deserialized for SyncOrderRelationReq body

JAXBContext jaxbCxt4Body = JAXBContext.newInstance(BatchSyncOrderRelationReq.class);

Unmarshaller unmarshaller4Body = jaxbCxt4Body.createUnmarshaller();

BatchSyncOrderRelationReq batchSyncOrderRelationReq = (BatchSyncOrderRelationReq) unmarshaller4Body.unmarshal(new StringReader(head.getSvcCont().trim()));

batchSyncOrderRelationReq.setHead(head);

return batchSyncOrderRelationReq;

} catch (JAXBException e) {

throw new BusinessException("SyncOrderRelationReq.Deserialized() Error!(" + interBossXmlContent + ")", e);

}

}

}@XmlAccessorType(XmlAccessType.FIELD)

public class BatchSyncOrderRelationReqBody {

@XmlElement(name = "oprNumb")

private String oprNumb = "";

@XmlElement(name = "subscriptionInfo")

private SubscriptionInfo subscriptionInfo;

public BatchSyncOrderRelationReqBody(){

}

public BatchSyncOrderRelationReqBody(String oprNumb, SubscriptionInfo subscriptionInfo) {

this.oprNumb = oprNumb;

this.subscriptionInfo = subscriptionInfo;

}

public String getOprNumb() {

return this.oprNumb;

}

public void setOprNumb(String oprNumb) {

this.oprNumb = oprNumb;

}

public SubscriptionInfo getSubscriptionInfo() {

return this.subscriptionInfo;

}

public void setSubscriptionInfo(SubscriptionInfo subscriptionInfo) {

this.subscriptionInfo = subscriptionInfo;

}

}@XmlAccessorType(XmlAccessType.FIELD)

public class SubscriptionInfo {

@XmlElement(name = "oprTime")

private String oprTime = "";

@XmlElement(name = "actionID")

private String actionId = "";

@XmlElement(name = "brand")

private String brand = "";

@XmlElement(name = "effTime")

private String effTime = "";

@XmlElement(name = "expireTime")

private String expireTime = "";

@XmlElement(name = "feeUser_ID")

private String feeUserId = "";

@XmlElement(name = "destUser_ID")

private String destUserId = "";

@XmlElement(name = "actionReasonID")

private String actionReasonId = "";

@XmlElement(name = "servType")

private String servType = "";

@XmlElement(name = "subServType")

private String subServType = "";

@XmlElement(name = "SPID")

private String spId = "";

@XmlElement(name = "SPServID")

private String spServId = "";

@XmlElement(name = "accessMode")

private String accessMode = "";

@XmlElement(name = "feeType")

private String feeType = "";

public SubscriptionInfo() {

}

public SubscriptionInfo(

String oprTime,

String actionId,

String brand,

String effTime,

String expireTime,

String feeUserId,

String destUserId,

String actionReasonId,

String servType,

String subServType,

String spId,

String spServId,

String accessMode,

String feeType) {

this.oprTime = oprTime;

this.actionId = actionId;

this.brand = brand;

this.effTime = effTime;

this.expireTime = expireTime;

this.feeUserId = feeUserId;

this.destUserId = destUserId;

this.actionReasonId = actionReasonId;

this.servType = servType;

this.subServType = subServType;

this.spId = spId;

this.spServId = spServId;

this.accessMode = accessMode;

this.feeType = feeType;

}

public String getOprTime() {

return this.oprTime;

}

public void setOprTime(String oprTime) {

this.oprTime = oprTime;

}

public String getActionId() {

return this.actionId;

}

public void setActionId(String actionId) {

this.actionId = actionId;

}

public String getBrand() {

return this.brand;

}

public void setBrand(String brand) {

this.brand = brand;

}

public String getEffTime() {

return this.effTime;

}

public void setEffTime(String effTime) {

this.effTime = effTime;

}

public String getExpireTime() {

return this.expireTime;

}

public void setExpireTime(String expireTime) {

this.expireTime = expireTime;

}

public String getFeeUserId() {

return this.feeUserId;

}

public void setFeeUserId(String feeUserId) {

this.feeUserId = feeUserId;

}

public String getDestUserId() {

return this.destUserId;

}

public void setDestUserId(String destUserId) {

this.destUserId = destUserId;

}

public String getActionReasonId() {

return this.actionReasonId;

}

public void setActionReasonId(String actionReasonId) {

this.actionReasonId = actionReasonId;

}

public String getServType() {

return this.servType;

}

public void setServType(String servType) {

this.servType = servType;

}

public String getSubServType() {

return this.subServType;

}

public void setSubServType(String subServType) {

this.subServType = subServType;

}

public String getSpId() {

return this.spId;

}

public void setSpId(String spId) {

this.spId = spId;

}

public String getSpServId() {

return this.spServId;

}

public void setSpServId(String spServId) {

this.spServId = spServId;

}

public String getAccessMode() {

return this.accessMode;

}

public void setAccessMode(String accessMode) {

this.accessMode = accessMode;

}

public String getFeeType() {

return this.feeType;

}

public void setFeeType(String feeType) {

this.feeType = feeType;

}

}

更多Java中对XML的解析详解相关文章请关注PHP中文网!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

java bip-39_Java中对XML的解析详解相关推荐

  1. Spring boot 中pom.xml 各个节点详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  2. AndroidManifest.xml文件解析(详解)

    转自:http://www.jb51.net/article/73731.htm 一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须 ...

  3. java ==和===_java中==和equals的区别详解

    分析前基础了解: 一)JVM把内存划分成两种:一种是栈内存,一种是堆内存. ①在函数中定义的一些基本类型的变量和对象的引用变量(变量名)都在函数的栈内存中分配. ②当在一段代码块定义一个变量时,Jav ...

  4. java jxl mergecells_java 中JXL操作Excel实例详解

    JXL操作Excel 前言: jxl是一个韩国人写的java操作excel的工具, 在开源世界中,有两套比较有影响的API可 供使用,一个是POI,一个是jExcelAPI.其中功能相对POI比较弱一 ...

  5. Android中measure过程、WRAP_CONTENT详解以及xml布局文件解析流程浅析(下)

       本文原创, 转载请注明出处:http://blog.csdn.net/qinjuning 上篇文章<<Android中measure过程.WRAP_CONTENT详解以及xml布局文 ...

  6. XML格式文件详解及Java解析XML文件内容方法

    XML格式文件详解 1.概述 XML,即可扩展标记语言,XML是互联网数据传输的重要工具,它可以跨越互联网任何的平台,不受编程语言和操作系统的限制,可以说它是一个拥有互联网最高级别通行证的数据携带者. ...

  7. java读取request中的xml

    java读取request中的xml 答: // 读取xml InputStream inputStream; StringBuffer sb = new StringBuffer(); inputS ...

  8. java html转换xml文件,使用Java在HTML中转换XML + XSL

    我们将数据作为XML使用,并且存在多种格式化XSL样式.在IE中它一直工作得很好.使用Java在HTML中转换XML + XSL 然后,我们需要在Chrome中显示与HTML相同的内容.所以,我们在服 ...

  9. java crossdomin.xml_crossdomain.xml的配置详解

    目录 1 简介 2 crossdomain.xml的配置详解 3 总结 1 简介 flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及 ...

最新文章

  1. 探秘IntelliJ IDEA 13测试版新功能——调试器显示本地变量
  2. string是线程安全的么_Java-21 多线程 - 是阿凯啊
  3. c#解析json字符串数组_在C#中解析Json字符串
  4. 开源一个WEB版本GEF,基于SVG的网页流程图框架
  5. 【无标题】外网访问esxi虚拟主机使用VMRC需要映射端口
  6. 你们要的Android计算器,今天它来了~
  7. [5-20]绿色精品软件每天更新[uc23整理]
  8. UE5 预览版载具模板工程车不能移动的问题
  9. BLE - 连接时触发配对
  10. 重磅!《中国DevOps现状调查报告(2021年)》正式发布!(附报告获取方式)
  11. SAP中使用SE91更改消息短文本
  12. Linux下使用游戏手柄
  13. 红绿灯代码 摘抄抖音 渡一前端的
  14. oracle 日期函数
  15. 手把手教你使用NBS
  16. win7系统服务print spooler 无法启动解决方法(开启及关闭方法)
  17. c语言中格式符号错误,C语言中符号格式说明
  18. java 数学测试_Java实现小学数学练习
  19. 哈喽!广袤的世界,这是来自我的第一篇博客。大家一起加油!
  20. BUC冰川算法的python实现

热门文章

  1. 653B. Bear and Compressing
  2. python可以开多少线程_python多线程详解
  3. svm预测结果为同一个值_SVM算法总结
  4. python显示文件夹图片_如何显示文件夹中的随机图片(Python)
  5. 对代理商的评价怎么写_简历中的自我评价怎么写才能更吸引人?
  6. 和lua的效率对比测试_Unity游戏开发Lua更新运行时代码!
  7. java 高飞_高飞(土木与水利工程学院)老师 - 合肥工业大学
  8. android人脸识别的背景图_Android 图片人脸识别剪切
  9. 解决 avformat_alloc_context无法识别的问题
  10. windows mobile 编译(生成镜像)提速