Read and Write XML files in jaxb Mode

Source: Internet
Author: User
Import Java. io. file; import Java. util. arraylist; import Java. util. list; import javax. XML. BIND. jaxb; import Org. apache. log4j. logger; import Org. dom4j. document; import Org. dom4j. element; import Org. dom4j. io. saxreader; import cn.com. starit. ts. NMS. collect. pon. SNMP. beans. configinfo; public class configxml {Private Static logger = logger. getlogger (configxml. class); string filepath = ""; Public configxml (){} Public configxml (string filepath) {This. filepath = filepath;}/*** read XML files in jaxb mode * @ Param file * @ Param clazz * @ return */public object getconfigxmlbyjaxb (File file, class <?> Clazz) {return jaxb. unmarshal (file, clazz);}/*** write XML file in jaxb mode * @ Param clazz * @ Param file */Public void setconfigxmlbyjaxb (Object OBJ, file) {jaxb. marshal (OBJ, file);}/*** load loads an XML document * @ return returns the Document Object, return NULL * @ Param file path */public document load (string filename) {document = NULL; try {saxreader = new saxreader (); document = saxreader. read (new file (filename);} catch (exception ex) {logger. error ("failed to parse the configuration file:" + ex. getmessage ();} return document;} // * address is changed to the configured @ suppresswarnings ("unchecked") public list <element> getprocesslist () {string home = system. getenv ("tspon_home"); // environment variable if (home = NULL) {home = "D:/snmp ";} filepath = home + "/collect/config/snmp_config.xml"; document DOC = load (filepath); List <element> process_list = new arraylist <element> (); if (Doc = NULL) {return process_list;} element define = Doc. getrootelement (). element ("process_list"); List <element> definenodes = define. elements ("process"); Return definenodes;} public configinfo getcollwayconfig () {string home = system. getenv ("tspon_home"); If (home = NULL) {home = "D:/snmp";} string collwayfile = home + "/collect/config/snmp_collecter.xml "; return (configinfo) getconfigxmlbyjaxb (new file (collwayfile), configinfo. class); // combine an XML file with a JavaBean object} public static void main (string [] A) {// configxml xml = new configxml ("D: \ snmp_collect_config.xml "); // XML. getprocesslist (); configxml xml = new configxml (); configinfo Config = new configinfo (); // write list <configinfo> List = new arraylist <configinfo> (); for (INT I = 1; I <5; I ++) {configinfo configfactory = new configinfo (); configfactory. setfactoryid (I); configfactory. setcolltype (1); configfactory. setoltcollmode (1); configfactory. setonucollmode (1); configfactory. setsendlength (5); list. add (configfactory);} config. setconfig (list); XML. setconfigxmlbyjaxb (config, new file ("D:/. XML "); // read Config = (configinfo) XML. getconfigxmlbyjaxb (new file ("D:/. XML "), configinfo. class); For (configinfo con: config. getconfig () {system. out. println (con. getoltcollmode ());}}}


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.