Java learning notes (2) -- parsing XML files in web projects

Source: Internet
Author: User


It is said that there are three ways to parse XML files in Java, but I don't know either. I have found a lot of materials on the Internet, but most of them are in the Java project, and I have successfully parsed them. However, the Java Web project created using eclipse or myeclipse is different from the Java project. One point is that there is no webroot root directory under the Java project, and there is a webroot root directory under the Java Web project, the class file compiled by Java is put under the WEB-INF/classes by default, this makes me not know how to obtain the server root path in a common class. I am depressed for a long time because there is no "webroot" path under the server. However, with the guidance of Song Ge, he solved the problem and expressed his gratitude.


Now let's get down to the point. Let's first look at the XML file mailconfig. xml:


<?xml version="1.0"encoding="UTF-8"?><content>    <mail>       <mailServerHost>smtp.163.com</mailServerHost>       <mailServerPort>25</mailServerPort>       <validate>true</validate>       <userName>lindf123of@163.com</userName>       <password>*********</password>       <fromAddress>lindf123of@163.com</fromAddress>    </mail></content>

The path is as follows:




Start parsing the XML file and create a Java class xmlutil. java. The Code is as follows:

 

Package COM. ldfsoft. util; import Java. io. file; import Java. io. ioexception; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. nodelist; import Org. XML. sax. saxexception; import COM. ldfsoft. dto. mailsenderdto; public class xmlutil {private mailsenderdto mailsenderinfodto; Public mailsenderdto getmailmess (string nodename, string filename) {documentbuilderfactory = documentbuilderfactory. newinstance (); string Path = This. getclass (). getclassloader (). getresource ("XML "). getpath (); // obtain and store mailconfig. mailsenderinfodto = new mailsenderdto (); try {documentbuilder = documentbuilderfactory. newdocumentbuilder (); document = documentbuilder. parse (path + file. separator + filename); nodelist = document. getelementsbytagname (nodename); If (nodelist. getlength ()> 0) {element = (element) nodelist. item (0); mailsenderinfodto. setmailserverhost (element. getelementsbytagname ("mailserverhost "). item (0 ). getfirstchild (). getnodevalue (); mailsenderinfodto. setmailserverport (element. getelementsbytagname ("mailserverport "). item (0 ). getfirstchild (). getnodevalue (); mailsenderinfodto. setvalidate (Boolean. valueof (element. getelementsbytagname ("Validate "). item (0 ). getfirstchild (). getnodevalue (); // note the method mailsenderinfodto to convert string type to boolean type. setusername (element. getelementsbytagname ("username "). item (0 ). getfirstchild (). getnodevalue (); mailsenderinfodto. setpassword (element. getelementsbytagname ("password "). item (0 ). getfirstchild (). getnodevalue (); mailsenderinfodto. setfromaddress (element. getelementsbytagname ("fromaddress "). item (0 ). getfirstchild (). getnodevalue () ;}} catch (parserconfigurationexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (saxexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} return mailsenderinfodto ;}}

Mailsenderdto is the DTO of the previous Article (Java Study Notes (1) ---- sending emails in Java), which is omitted in this article. In this class, getmailmess (stringnodename, string filename) has two parameters, nodename is the node name, and filename is the name of the XML file, encapsulate the obtained data in an instance of mailsenderdto and return it.

 

In the javautil. Java class, we use the following method to replace this class in the previous article. Some code is as follows:

 

/*** Send an activation account email */Public void sendemail (string email) {mailsenderdto = xmlutil. getmailmess ("mail", "mailconfig. XML "); try {string mailtitle =" [my online storage] Account Activation email "; stringmailcontent =" <br> Dear [my online storage] user: <br> "+" Hello! Thank you for registering your [my online storage] account. click the following link to complete registration: <br> "+" <ahref = \ "http: // localhost: 9080/mydisk/SWF/activateemail.html # email = "+ email +" \ "> http: // localhost: 9080/mydisk/SWF/activateemail.html # email = "+ email +" </a> <br> "+" (if the link cannot be clicked, copy and paste it to the address bar of your browser.) <br> "+" Your Email: "+ email +" <br> "+" email can be used as your account to log on to [my online storage ]. <Br> "+" This email is automatically sent by the system. Please do not reply directly! Thank you for your visit and wish you a pleasant stay! <Br> "; mailsenderdto. settoaddress (email); // the recipient's email address mailsenderdto. setsubject (mailtitle); // mail title mailsenderdto. setcontent (mailcontent); // The Mail content // This class is mainly used to send the mail mailsender MS = new mailsender (); Ms. sendhtmlmail (mailsenderdto); // sends the HTML format system. out. println ("--------------- sent successfully! ");} Catch (exception e) {system. Out. println (" failed! ");}}

The test results are the same. This greatly reduces the coupling between mail attributes and codes and facilitates management. In fact, I learned how to parse XML files.


This is my learning result. You can reprint it. You are welcome to share it with me. However, you must provide the link to this article at http://blog.csdn.net/youqishini/article/details/7906571,thank you ~

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.