1、接口
import org.dom4j.Document; public interface NgsnConvertor { public Object Convert2Bean(Document ngsnBody); public Document Convert2Xml(Object obj); }
2、公共方法
import java.util.List; import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.Node; public abstract class NgsnConvertorAbstract implements NgsnConvertor { //根据xpath路径得到节点值,ex. /Document/FIToFICstmrCdtTrf/CdtTrfTxInf/CdtrAcct/Id/SubAcct/Id/IBAN protected static String getBeanAttributeValue(Document doc, String xpath){ String value = null; if(StringUtils.isBlank(xpath)){ return value; } Element element = (Element) doc.selectSingleNode(xpath); if(element!=null){ value = element.getText(); } return value; } //设置节点的值,/Document/FIToFICstmrCdtTrf/CdtTrfTxInf/CdtrAcct/Id/SubAcct/Id/IBAN = abc public static void setBeanAttributeValue(Document doc, String xpath, String value){ if(StringUtils.isNotBlank(xpath)){ String[] nodes = xpath.substring(1, xpath.length()).split("/"); Node firstNode = doc.selectSingleNode(nodes[0]); Element element = doc.getRootElement(); if(firstNode==null){ element = doc.addElement(nodes[0]); } String nodePath = new String(); for(String node : nodes){ nodePath += "/"+node; Node n = element.selectSingleNode(nodePath); if(n == null){ element = element.addElement(node); }else { element = (Element) element.selectSingleNode(nodePath); } } element.setText(value == null ? "" : value); } } //增加节点的属性,ex. /Document/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrSttlm/IntrBkSttlmAmt Ccy=CNY public static void setBeanAttribute(Document doc, String xpath, String name, String value, int number){ if(StringUtils.isNotBlank(xpath)){ Element element = (Element) doc.selectNodes(xpath).get(number); element.addAttribute(name, value); } } //增加List节点,ex. /Document/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrSttlm/IntrBkSttlmAmt protected static void setBeanListAttribute(Document doc, String xpath){ if(StringUtils.isNotBlank(xpath)){ String[] nodes = xpath.substring(1, xpath.length()).split("/"); Node firstNode = doc.selectSingleNode(nodes[0]); Element element = doc.getRootElement(); if(firstNode==null){ element = doc.addElement(nodes[0]); } String nodePath = new String(); for(String node : nodes){ nodePath += "/"+node; Node n = element.selectSingleNode(nodePath); if(n == null || node.equals(nodes[nodes.length-1])){ element = element.addElement(node); }else { element = (Element) element.selectSingleNode(nodePath); } } } } //增加多层List节点,ex. /Document/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrSttlm/IntrBkSttlmAmt protected static void setBeanListAttribute(Document doc, String xpath1, int i, String xpath2){ Element element = (Element) getBeanListAttribute(doc,xpath1).get(i); if(StringUtils.isNotBlank(xpath2)){ String[] nodes = xpath2.substring(1, xpath2.length()).split("/"); String nodePath = new String(); nodePath = xpath1; for(String node : nodes){ nodePath += "/"+node; Node n = element.selectSingleNode(nodePath); if(n == null || node.equals(nodes[nodes.length-1])){ element = element.addElement(node); }else { element = (Element) element.selectSingleNode(nodePath); } } } } //增加List节点属性,ex. /Document/FIToFICstmrCdtTrf/CdtTrfTxInf/DbtrSttlm/IntrBkSttlmAmt protected static void setBeanListAttributeValue(Document doc, String xpath, String xpathList, String value, int number){ if(StringUtils.isNotBlank(xpathList)){ String[] nodes = xpathList.substring(1, xpathList.length()).split("/"); List<Element> elements = doc.selectNodes(xpath); Element element = elements.get(number); for(String node : nodes){ Element ele = element.element(node); if(ele != null){ element = ele; }else{ element = element.addElement(node); } } if(element!=null){ element.setText(value == null ? "" : value); } } } //根据xpath得到list节点中节点值 protected static String getBeanListAttributeValue(Document doc, String xpath, int number){ String value = null; if(StringUtils.isBlank(xpath)){ return value; } Element element = (Element) doc.selectNodes(xpath).get(number); if(element != null){ value = element.getText(); } return value; } //获取list节点 protected static List<Element> getBeanListAttribute(Document doc, String xpath){ List<Element> elementList = null; if(StringUtils.isBlank(xpath)){ return elementList; } elementList = doc.selectNodes(xpath); return elementList; } //获取节点属性值 public static String getBeanAttribute(Document doc, String xpath, String name, int number){ String value = null; if(StringUtils.isNotBlank(xpath)){ Element element = (Element) doc.selectNodes(xpath).get(number); value= element.attributeValue(name); } return value; } }
3.1、简单 单循环 用法(样例)
import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; public class Convertor_Ngsn_916_001_01_Demo extends NgsnConvertorAbstract { @Override public Object Convert2Bean(Document ngsnBody) { ClearBookClearJournal clearBookClearJournal = new ClearBookClearJournal(); clearBookClearJournal.setClearMessageId(getBeanAttributeValue(ngsnBody,"//NGSNClrRpt/MsgHdr/MsgId")); clearBookClearJournal.setClearAgentBic(getBeanAttributeValue(ngsnBody,"//NGSNClrRpt/MsgHdr/CreDtTm")); List<ClearTrxClearDetailRet> clearDetailList = new ArrayList<ClearTrxClearDetailRet>(); List<Element> elementList = getBeanListAttribute(ngsnBody, "//NGSNClrRpt/Rpt/ClrInf/TxsSummry"); for(int i=0;i<elementList.size();i++){ ClearTrxClearDetailRet clearDetail = new ClearTrxClearDetailRet(); clearDetail.setClearResultId(getBeanListAttributeValue(ngsnBody, "//NGSNClrRpt/Rpt/ClrInf/TxsSummry/SttlmDt", i)); clearDetail.setClearBookClearJournalId(getBeanListAttributeValue(ngsnBody, "//NGSNClrRpt/Rpt/ClrInf/TxsSummry/DbtrSttlm/IntrBkSttlmAmt", i)); clearDetail.setClearDate(getBeanListAttributeValue(ngsnBody, "//NGSNClrRpt/Rpt/ClrInf/TxsSummry/DbtrSttlm/IntrBkSttlmDt", i)); clearDetailList.add(clearDetail); } clearBookClearJournal.setClearDetailList(clearDetailList); return clearBookClearJournal; } @Override public Document Convert2Xml(Object obj) { Document ngsnBody = DocumentHelper.createDocument(); ClearBookClearJournal clearBookClearJournal = (ClearBookClearJournal)obj; setBeanAttributeValue(ngsnBody,"/Document/NGSNClrRpt/MsgHdr/MsgId",clearBookClearJournal.getClearMessageId()); setBeanAttributeValue(ngsnBody,"/Document/NGSNClrRpt/MsgHdr/CreDtTm",clearBookClearJournal.getClearAgentBic()); List<ClearTrxClearDetailRet> clearDetailList = clearBookClearJournal.getClearDetailList(); if(clearDetailList!=null && clearDetailList.size()>0){ for(int i=0;i<clearDetailList.size();i++){ ClearTrxClearDetailRet clearDetail = clearDetailList.get(i); //增加List父节点 setBeanListAttribute(ngsnBody, "/Document/NGSNClrRpt/Rpt/ClrInf/TxsSummry"); //增加List节点中的叶子节点并赋值 setBeanListAttributeValue(ngsnBody,"/Document/NGSNClrRpt/Rpt/ClrInf/TxsSummry", "/SttlmDt", clearDetail.getClearResultId(), i); setBeanListAttributeValue(ngsnBody,"/Document/NGSNClrRpt/Rpt/ClrInf/TxsSummry","/DbtrSttlm/IntrBkSttlmAmt",clearDetail.getClearBookClearJournalId(), i); //给叶子节点增加属性 setBeanAttribute(ngsnBody, "/Document/NGSNClrRpt/Rpt/ClrInf/TxsSummry/DbtrSttlm/IntrBkSttlmAmt", "Ccy", "CNY", i); setBeanListAttributeValue(ngsnBody,"/Document/NGSNClrRpt/Rpt/ClrInf/TxsSummry","/DbtrSttlm/IntrBkSttlmDt",clearDetail.getClearDate(), i); } } return ngsnBody; } }
3.2、多层嵌套循环 用法(样例)
import java.math.BigDecimal; import org.dom4j.Document; import org.dom4j.DocumentHelper; /** * 交易汇总报文 * * @author * @Date 2016-11-03 */ public class Convertor_Ngsn_167_001_01_Example extends NgsnConvertorAbstract { // 报文标识号 private static final String MESSAGE_ID = "//TxSummaryNtfctn/MsgHdr/MsgId"; // 报文发送时间 private static final String CREATION_DATETIME = "//TxSummaryNtfctn/MsgHdr/CreDtTm"; // 发起机构BIC private static final String INSTRUCTING_AGENT_BIC = "//TxSummaryNtfctn/MsgHdr/InstgAgt/FinInstnId/BICFI"; // 发起机构LEI private static final String INSTRUCTING_AGENT_LEI = "//TxSummaryNtfctn/MsgHdr/InstgAgt/FinInstnId/LglNttId"; // 发起机构号 private static final String INSTRUCTING_AGENT_ID = "//TxSummaryNtfctn/MsgHdr/InstgAgt/FinInstnId/Othr/Id"; // 接收机构BIC private static final String INSTRUCTED_AGENT_BIC = "//TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/BICFI"; // 接收机构LEI private static final String INSTRUCTED_AGENT_LEI = "//TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/LglNttId"; // 接收机构号 private static final String INSTRUCTED_AGENT_ID = "//TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/Othr/Id"; // 汇总日期 private static final String FROM_DATETIME = "//TxSummaryNtfctn/Rpt/AggtdDt"; // 汇总机构BIC private static final String SUMMARY_AGENT_BIC = "//TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/BICFI"; // 汇总机构LEI private static final String SUMMARY_AGENT_LEI = "//TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/LglNttId"; // 汇总机构号 private static final String SUMMARY_AGENT_ID = "//TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/Othr/Id"; // 汇总币种 private static final String SUMMARY_CURRENCY = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/Ccy"; // 汇总报文类型 private static final String SUMMARY_MSGTYPE = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgNmId"; // 汇总报文状态 private static final String SUMMARY_MSGSTATUS = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/MsgSts"; // 汇总发送报文总笔数 private static final String SENDMSG_COUNT = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlDlvrgNtries/NbOfNtries"; // 汇总发送报文总金额 private static final String SEND_AMOUNT = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlDlvrgNtries/Sum"; // 汇总接收报文总笔数 private static final String RECMSG_COUNT = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlRcvgNtries/NbOfNtries"; // 汇总接收报文总金额 private static final String REC_AMOUNT = "//TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlRcvgNtries/Sum"; /** * xml转Bean * * @author */ @Override public Object Convert2Bean(Document ngsnBody) { // 报文业务信息登记簿 CoreBookMessageBus msgBus = new CoreBookMessageBus(); msgBus.setMessageId(getBeanAttributeValue(ngsnBody, MESSAGE_ID));// 报文标识号 msgBus.setInstructingAgentBic(getBeanAttributeValue(ngsnBody, INSTRUCTING_AGENT_BIC));// 发起机构BIC msgBus.setInstructingAgentId(getBeanAttributeValue(ngsnBody, INSTRUCTING_AGENT_ID));// 发起机构号 msgBus.setInstructedAgentBic(getBeanAttributeValue(ngsnBody, INSTRUCTED_AGENT_BIC));// 接收机构BIC msgBus.setInstructedAgentId(getBeanAttributeValue(ngsnBody, INSTRUCTED_AGENT_ID));// //接收机构号 msgBus.setCreationDatetime(getBeanAttributeValue(ngsnBody, CREATION_DATETIME));// 报文发送时间 // 交易汇总登记簿 ClearBookTransSummary clearBookTransSummaryBean = new ClearBookTransSummary(); clearBookTransSummaryBean.setMessageId(getBeanAttributeValue(ngsnBody, MESSAGE_ID)); // 报文标识号 clearBookTransSummaryBean.setCreationDateTime(getBeanAttributeValue(ngsnBody, CREATION_DATETIME)); // 报文发送时间 clearBookTransSummaryBean.setInstructingAgentBic(getBeanAttributeValue(ngsnBody, INSTRUCTING_AGENT_BIC));// 发起机构BIC clearBookTransSummaryBean.setInstructingAgentLei(getBeanAttributeValue(ngsnBody, INSTRUCTING_AGENT_LEI));// 发起机构LEI clearBookTransSummaryBean.setInstructingAgentId(getBeanAttributeValue(ngsnBody, INSTRUCTING_AGENT_ID)); // 发起机构号 clearBookTransSummaryBean.setInstructedAgentBic(getBeanAttributeValue(ngsnBody, INSTRUCTED_AGENT_BIC)); // 接收机构BIC clearBookTransSummaryBean.setInstructingAgentLei(getBeanAttributeValue(ngsnBody, INSTRUCTED_AGENT_LEI)); // 接收机构LEI clearBookTransSummaryBean.setInstructedAgentId(getBeanAttributeValue(ngsnBody, INSTRUCTED_AGENT_ID)); // 接收机构号 clearBookTransSummaryBean.setFromDatetime(getBeanAttributeValue(ngsnBody, FROM_DATETIME)); // 汇总日期 clearBookTransSummaryBean.setSummaryAgentBic(getBeanAttributeValue(ngsnBody, SUMMARY_AGENT_BIC)); // 汇总机构BIC clearBookTransSummaryBean.setSummaryAgentLei(getBeanAttributeValue(ngsnBody, SUMMARY_AGENT_LEI)); // 汇总机构LEI clearBookTransSummaryBean.setSummaryAgentId(getBeanAttributeValue(ngsnBody, SUMMARY_AGENT_ID)); // 汇总机构号 clearBookTransSummaryBean.setSummaryCurrency(getBeanAttributeValue(ngsnBody, SUMMARY_CURRENCY)); // 汇总币种 clearBookTransSummaryBean.setSummaryMsgType(getBeanAttributeValue(ngsnBody, SUMMARY_MSGTYPE)); // 汇总报文类型 clearBookTransSummaryBean.setSummaryMsgStatus(getBeanAttributeValue(ngsnBody, SUMMARY_MSGSTATUS)); // 汇总报文状态 clearBookTransSummaryBean.setSendMsgCount(Long.parseLong(getBeanAttributeValue(ngsnBody, SENDMSG_COUNT))); // 汇总发送报文总笔数 BigDecimal bdsendCount = new BigDecimal(getBeanAttributeValue(ngsnBody, SEND_AMOUNT)); clearBookTransSummaryBean.setSendAmount(bdsendCount.setScale(2, BigDecimal.ROUND_HALF_UP)); // 汇总发送报文总金额 clearBookTransSummaryBean.setRecMsgCount(Long.parseLong(getBeanAttributeValue(ngsnBody, RECMSG_COUNT))); // 汇总接收报文总笔数 BigDecimal bdRecvCount = new BigDecimal(getBeanAttributeValue(ngsnBody, REC_AMOUNT)); clearBookTransSummaryBean.setRecAmount(bdRecvCount.setScale(2, BigDecimal.ROUND_HALF_UP)); // 汇总接收报文总金额 return msgBus; } /** * beab转xml * * @author */ @Override public Document Convert2Xml(Object obj) { Document ngsnBody = DocumentHelper.createDocument(); // CoreBookMessageBus bus = (CoreBookMessageBus) obj; setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/MsgId","M234567890123451"); //报文标识号 setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/CreDtTm","2016-09-01T09:15:05.000+08:00");//"2016-09-01T09:15:05.000+08:00"); //报文发送时间 setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/InstgAgt/FinInstnId/Othr/Id","CN04000001JJ"); //发起机构号 setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/BICFI","ICBKCNBJ");//"ICBKCNBJ");//bus.getInstructedAgentBic()); //接收机构BIC setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/LglNttId","A0000000000000000011");//"A0000000000000000011");//bus.getInstructedAgentLei()); //接收机构LEI setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/MsgHdr/InstdAgt/FinInstnId/Othr/Id","CN01123456AA"); //接收机构号 setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/AggtdDt","2016-08-01T00:00:00.000+08:00");//"2016-08-01T00:00:00.000+08:00"); setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/BICFI","ICBKCNBJ");//"ICBKCNBJ");//clearBookTransSummaryBean.getInstructingAgentBic());//发起机构BIC setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/LglNttId","A0000000000000000011");//"A0000000000000000011");//clearBookTransSummaryBean.getInstructingAgentLei());//发起机构LEI setBeanAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/FinInstnId/Othr/Id","CN01123456AA");//"CN01123456AA");//clearBookTransSummaryBean.getInstructingAgentId()); //发起机构号 int flagJ = 0; int flagK = 0; int flagX = 0; for(int i=0;i<2;i++){ setBeanListAttribute(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls"); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls", "/Ccy", "CNY", i); for(int j=0;j<2;j++){ setBeanListAttribute(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls",i,"/MsgSummry"); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry","/MsgNmId", "ngsn.167.001.01",flagJ); for(int k=0;k<2;k++){ setBeanListAttribute(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry",flagJ,"/MsgStsSummry"); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry","/MsgSts", "CLRD",flagK); for(int x=0;x<2;x++){ setBeanListAttribute(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry",flagK,"/TxsSummry"); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry","/TtlDlvrgNtries/NbOfNtries", "1",flagX); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry","/TtlDlvrgNtries/Sum", "20000",flagX); setBeanAttribute(ngsnBody, "/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlDlvrgNtries/Sum", "Ccy","CNY",flagX); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry","/TtlRcvgNtries/NbOfNtries", "1",flagX); setBeanListAttributeValue(ngsnBody,"/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry","/TtlRcvgNtries/Sum", "20000",flagX); setBeanAttribute(ngsnBody, "/Document/TxSummaryNtfctn/Rpt/InstnInf/ChngDtls/MsgSummry/MsgStsSummry/TxsSummry/TtlRcvgNtries/Sum", "Ccy","CNY",flagX); flagX++; } flagK++; } flagJ++; } } System.out.println(ngsnBody.asXML()); return ngsnBody; } public static void main(String[] args) { Convertor_Ngsn_167_001_01_Example example = new Convertor_Ngsn_167_001_01_Example(); Object obj = new Object(); example.Convert2Xml(obj); } }
相关推荐
Dom4J作为一款优秀的Java XML API,以其简洁、高效的特点,在生成、解析和操作XML文档方面表现突出。本文将深入探讨如何使用Dom4J库来创建XML文档,包括初始化文档、添加元素、设置属性、插入文本以及最终保存文档至...
DOM4J是一个强大的Java库,专门用于处理...总结,DOM4J是一个强大且易用的XML处理库,适合在Java项目中进行XML解析和生成。通过学习和实践提供的代码示例,你将能够熟练掌握DOM4J的各种操作,从而高效地处理XML数据。
DOM4J是一个由dom4j.org开发的开源XML解析包,专为Java平台设计,它不仅支持DOM、SAX和JAXP标准,还巧妙地融入了Java集合框架,使其成为Java开发者在处理XML数据时的强大工具。DOM4J的最大亮点在于其简洁易用的API...
Java中的DOM4J库是一个非常流行的、开源的Java XML API,它提供了强大的XML处理功能,包括XML的读取、创建、修改以及写入。DOM4J是基于DOM模型的,但相比标准的Java DOM,它提供了更加简洁和高效的API,使得开发者在...
DOM4J是一个强大的Java库,专门用于处理XML文档。它是一个灵活且高性能的XML解析器,同时提供了丰富的API,使得在Java应用程序中读取、写入、修改和操作XML文件变得简单。DOM4J这个名字来源于Document Object Model ...
本文将详细介绍生成和解析XML的四种主要方法:DOM、DOM4J、SAX和JDOM。 1. DOM(Document Object Model) DOM是W3C组织推荐的一种解析XML的标准方式。它将XML文档视为一个树形结构,每个节点代表XML文档的一部分。...
在本示例中,我们将探讨如何利用DOM4J来读取和解析XML文件,以及如何通过`XMLTest.java`这个示例来理解DOM4J的基本用法。 首先,`XMLTest.java`是一个包含DOM4J操作XML的代码实例。通常,这样的程序会包含以下步骤...
在Java领域,XML常用于数据交换、配置存储以及文档生成等场景,因此理解并掌握DOM4J对于Java开发者来说至关重要。 DOM4J的核心概念包括元素(Element)、属性(Attribute)、文本(Text)和命名空间(Namespace)等...
在Java中,读取XML文件有多种方法,本文将介绍四种常见的方法:DOM、DOM4J、JDOM和SAX。 DOM(Document Object Model) DOM是W3C组织制定的XML文档对象模型,它提供了对XML文档的树形结构表示。使用DOM方法读取XML...
Java DOM4J XML是一个在Java环境中处理XML文档的库,它提供了一套高效、灵活的API,使得开发者能够轻松地进行XML的解析、创建、修改和查询。DOM4J是Java社区中的一个流行选择,因为它相比标准的DOM API更加轻量级且...
总结一下,Java DOM提供了一套全面的API来处理XML文档,包括创建、修改和读取XML结构。尽管DOM操作相对消耗资源,但其灵活性和强大功能使其成为处理XML的常用工具。对于复杂的XML操作,可以结合使用其他库如JAXB或...
Java DOM4J解析XML是一种常见的处理XML文档的技术,它提供了灵活且高效的API,使得开发者能够方便地读取、写入、修改以及操作XML文件。DOM4J是Java中一个非常强大的XML处理库,它结合了DOM、SAX和JDOM的优点,同时也...
DOM4J提供了通过其API和标准DOM接口并行访问文档的能力,这意味着开发者可以在不同线程中安全地读取和修改XML文档,增强了多线程环境下程序的并发性能。 **5. 高度可定制的API** 尽管DOM4J的API相对复杂,但它提供...
本文将深入探讨如何使用DOM4J库来生成和读取`hibernate.cfg.xml`,这是一个强大的Java XML处理库。 首先,让我们了解DOM4J的基本概念。DOM4J是一个开源的Java库,它提供了一套灵活的API来处理XML文档,包括读取、...
在Java编程中,DOM4J是一个非常流行的XML处理库,它提供了一套强大的API来创建、解析和操作XML文档。本教程将详细讲解如何利用DOM4J进行XML文件的创建和读取。首先,我们需要了解DOM4J的核心概念,如Element、...
Java DOM4J是一个非常流行的Java库,专门用于处理XML文档。它提供了强大的API来解析、创建、修改XML文档,使得开发者能够高效地处理XML数据。DOM4J这个名字来源于Document Object Model (DOM) 和 Java,而4J则表示它...
DOM4j则是一个强大的Java库,用于处理XML文档,提供了丰富的API来实现XML的读取、写入、修改等操作。在这个示例中,我们将深入探讨如何利用Maven管理和DOM4j来操作XML文件。 首先,让我们了解一下Maven。Maven是...
在实际开发中,DOM4J常用于XML配置文件的读取、XML数据的交换、XML文件的生成等场景。例如,在Web服务开发中,它可以用来解析WSDL文件;在Spring框架中,用于读取和处理配置文件;在数据持久化领域,如Hibernate,它...