应用一:
<?xml version="1.0" encoding="utf-8" ?>
- <ACCESOS>
- <item>
- <SOCIO>
<NUMERO>00045050</NUMERO>
<REPOSICION>0</REPOSICION>
<NOMBRE>MOISES MORENO</NOMBRE>
- <TURNOS>
<LU>T1</LU>
<MA>T2</MA>
<MI>T3</MI>
<JU>T4</JU>
<VI>T5</VI>
<SA>T6</SA>
<DO>T7</DO>
</TURNOS>
</SOCIO>
</item>
- <item>
- <SOCIO>
<NUMERO>00045051</NUMERO>
<REPOSICION>0</REPOSICION>
<NOMBRE>RUTH PENA</NOMBRE>
- <TURNOS>
<LU>S1</LU>
<MA>S2</MA>
<MI>S3</MI>
<JU>S4</JU>
<VI>S5</VI>
<SA>S6</SA>
<DO>S7</DO>
</TURNOS>
</SOCIO>
</item>
</ACCESOS>
1、导入dom4j与jaxen的jar包
2、读取时会比纯dom4j简单些:
Document doc = new SAXReader().read(new File("D:/share/JavaProjects/drp/test_xmlImport/xml/test01.XML"));
List itemList = doc.selectNodes("/ACCESOS/item/SOCIO"); //dom4j+xpath的写法
for (Iterator iter=itemList.iterator(); iter.hasNext();) {
Element el = (Element)iter.next();
String numero = el.elementText("NUMERO");
String reposicion = el.elementText("REPOSICION");
String nombre = el.elementText("NOMBRE");
List turnosList = el.elements("TURNOS");
for (Iterator iter1=turnosList.iterator(); iter1.hasNext();) {
Element turnosElt = (Element)iter1.next();
String lu = turnosElt.elementText("LU");
String ma = turnosElt.elementText("MA");
String mi = turnosElt.elementText("MI");
String ju = turnosElt.elementText("JU");
String vi = turnosElt.elementText("VI");
String sa = turnosElt.elementText("SA");
String doo = turnosElt.elementText("DO");
}
}
应用二:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:bjpowern</url>
<user-name>drp1</user-name>
<password>drp1</password>
</db-info>
<dao-factory>
<item-dao-facotry>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4Oracle</item-dao-facotry>
<!--
<item-dao-facotry>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4MySql</item-dao-facotry>
-->
<user-dao-facotry>com.bjpowernode.drp.basedata.dao.UserDaoFactory4Oracle</user-dao-facotry>
</dao-factory>
</config>
有如上xml,读取/item-dao-facotry节点下的信息
List daoFactorylist = doc.selectNodes("/config/dao-factory/*"); //*是xpath语法,表示dao-factory的所有子节点
for (int i=0; i<daoFactorylist.size(); i++) {
Element daoFactoryElt = (Element)daoFactorylist.get(i);
String tagName = daoFactoryElt.getName(); //getName():得到标签名
String tagText = daoFactoryElt.getText(); //getText():得到文本内容
<?xml version="1.0" encoding="utf-8" ?>
- <ACCESOS>
- <item>
- <SOCIO>
<NUMERO>00045050</NUMERO>
<REPOSICION>0</REPOSICION>
<NOMBRE>MOISES MORENO</NOMBRE>
- <TURNOS>
<LU>T1</LU>
<MA>T2</MA>
<MI>T3</MI>
<JU>T4</JU>
<VI>T5</VI>
<SA>T6</SA>
<DO>T7</DO>
</TURNOS>
</SOCIO>
</item>
- <item>
- <SOCIO>
<NUMERO>00045051</NUMERO>
<REPOSICION>0</REPOSICION>
<NOMBRE>RUTH PENA</NOMBRE>
- <TURNOS>
<LU>S1</LU>
<MA>S2</MA>
<MI>S3</MI>
<JU>S4</JU>
<VI>S5</VI>
<SA>S6</SA>
<DO>S7</DO>
</TURNOS>
</SOCIO>
</item>
</ACCESOS>
1、导入dom4j与jaxen的jar包
2、读取时会比纯dom4j简单些:
Document doc = new SAXReader().read(new File("D:/share/JavaProjects/drp/test_xmlImport/xml/test01.XML"));
List itemList = doc.selectNodes("/ACCESOS/item/SOCIO"); //dom4j+xpath的写法
for (Iterator iter=itemList.iterator(); iter.hasNext();) {
Element el = (Element)iter.next();
String numero = el.elementText("NUMERO");
String reposicion = el.elementText("REPOSICION");
String nombre = el.elementText("NOMBRE");
List turnosList = el.elements("TURNOS");
for (Iterator iter1=turnosList.iterator(); iter1.hasNext();) {
Element turnosElt = (Element)iter1.next();
String lu = turnosElt.elementText("LU");
String ma = turnosElt.elementText("MA");
String mi = turnosElt.elementText("MI");
String ju = turnosElt.elementText("JU");
String vi = turnosElt.elementText("VI");
String sa = turnosElt.elementText("SA");
String doo = turnosElt.elementText("DO");
}
}
应用二:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<db-info>
<driver-name>oracle.jdbc.driver.OracleDriver</driver-name>
<url>jdbc:oracle:thin:@localhost:1521:bjpowern</url>
<user-name>drp1</user-name>
<password>drp1</password>
</db-info>
<dao-factory>
<item-dao-facotry>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4Oracle</item-dao-facotry>
<!--
<item-dao-facotry>com.bjpowernode.drp.basedata.dao.ItemDaoFactory4MySql</item-dao-facotry>
-->
<user-dao-facotry>com.bjpowernode.drp.basedata.dao.UserDaoFactory4Oracle</user-dao-facotry>
</dao-factory>
</config>
有如上xml,读取/item-dao-facotry节点下的信息
List daoFactorylist = doc.selectNodes("/config/dao-factory/*"); //*是xpath语法,表示dao-factory的所有子节点
for (int i=0; i<daoFactorylist.size(); i++) {
Element daoFactoryElt = (Element)daoFactorylist.get(i);
String tagName = daoFactoryElt.getName(); //getName():得到标签名
String tagText = daoFactoryElt.getText(); //getText():得到文本内容