Read XML files in Java

Source: Internet
Author: User

1. Import the JDOM class

/** <Br/> * read the SMS. xml configuration file information <br/> * @ Param file <br/> * @ throws ioexception <br/> * @ throws jdomexception <br/> */<br/> Public readsmsfile (File file) throws jdomexception, ioexception <br/>{< br/> If (file! = NULL) <br/>{< br/> saxbuilder sb = new saxbuilder (); // create a parser </P> <p> // construct a document, read the content of the XML file <br/> document DOC = sb. build (File); <br/> element root = Doc. getrootelement (); // get the root element <br/> JAVA. util. list list = root. getchildren (); // obtain the set of all child elements of the root element <br/> If (list. size () = 0) <br/>{< br/> logger. debug (root. getname () + "sms xml size = 0"); <br/> return; <br/>}< br/> // read element <br/> for (INT I = 0; I <list. size (); I ++) <br/>{< br/> element = (element) list. get (I); <br/> system. out. println (element); <br/> // Save the SMS list information <br/> smsparameter smsdata = new smsparameter (); <br/> // obtain the IP address and path <br/> string IP = element. getchildtext ("ip"); <br/> string Path = element. getchildtext ("path"); <br/> // determines whether the IP address is valid <br/> smsdata. setsmsip (IP); <br/> smsdata. setsmspath (PATH); <br/> smslist. add (smsdata); <br/>}< br/>}

<? XML version = "1.0" encoding = "UTF-8"?> <Br/>-<smslist> <br/>-<SMS> <br/> <ip> 10.1.1.3 </IP> <br/> <path>/home </path> <br/> </SMS> <br/>-<SMS> <br/> <ip> 10.1.1.4 </IP> <br/> <path>/home </ path> <br/> </SMS> <br/>-<SMS> <br/> <ip> 10.1.1.5 </IP> <br/> <path>/home </path> <br/> </SMS> <br/> </smslist>

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.