Dom4j + XPath Application

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.