Application 1:
<? 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. Import the jar packages of dom4j and jaxen
2. Reading is easier than pure 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 ");
}
}
Application 2:
<? 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>
Read the information under the/item-Dao-facotry node in XML format.
List daofactorylist = Doc. selectnodes ("/config/Dao-factory/*"); // * is the XPath syntax, indicating all child nodes of Dao-factory.
For (INT I = 0; I <daofactorylist. Size (); I ++ ){
Element daofactoryelt = (element) daofactorylist. Get (I );
String tagname = daofactoryelt. getname (); // getname (): Obtain the tag name.
String tagtext = daofactoryelt. gettext (); // gettext (): Get the text content