Java, XML parsing tools example

1. Direct code section:

. 1  Import java.io.File;
 2  Import a java.io.FileInputStream;
 . 3  Import java.util.List;
 . 4  Import org.jdom.Document;
 . 5  Import org.jdom.Element;
 . 6  Import org.jdom.input.SAXBuilder;
 . 7  Import org.xml.sax.InputSource;
 . 8  
. 9  / ** 
10  * role: XML parsing tools, which add or otherwise change the properties according to their needs
 . 11  * 
 12 is   * / 
13 is  public  class ReadFileContent
 14  {
 15      static the FileInputStream INS;
 16 
. 17      public  static String trxId;               // file ID 
18 is      public  static String trxBank;             // bank coding 
. 19      public  static String trxOper;             //
 20 is      public  static String trxDate;             // Data Date 
21 is      public  static String PkgNo;               // packet number 
22 is      public  static String fileCode;            // file encoding 
23      public  static String fileName;            //File type 
24-      public  static String fileContent;         // file contents 
25      
26      
27      public  static  void PullConfigXml (String path)
 28      {
 29          Log4jBean.logger.info ( "began to read the configuration file ..." );
 30          the try {   
 31              File File = null ;
 32              // local test path / Home / ngpcom / dfgz / config
 33 is              // String the System.getProperty path = ( "user.home") + + File.separator "dfgz" File.separator + + "config" + File. + Separator "config1.xml";            
 34 is              //The System.getProperty path = String ( "user.home") File.separator + + "config" + + File.separator "config1.xml"; 
35              Log4jBean.logger.info ( "path to the configuration file [" path + + "]" ) ;
 36              INS = new new the FileInputStream ( new new file (path));
 37 [          } the catch (exception E) {
 38 is              Log4jBean.logger.error ( "read configuration file abnormality, the abnormality information: [" + e.getMessage () + " ] " );
 39          }
 40          Log4jBean.logger.info (" reads the configuration file successfully, start parsing xml document " );
 41  
42          // create a new input source SAX parser will use InputSource object to determine how to read XML input ,Here is a file stream 
43          InputSource Source =new new the InputSource (INS);
 44 is          // Create a new SAXBuilder 
45          SAXBuilder saxbBuilder = new new SAXBuilder ();
 46 is          the try {
 47              // configured by an input source Document 
48              Document DOC = saxbBuilder.build (Source);
 49              // obtain xml root element 
50              the element = the root doc.getRootElement ();
 51 is              // acquired child element of the root element 
52 is              List Node = <?> root.getChildren ();
 53 is              for ( int I = 0; I <node.size () ; I ++ ) {
 54 is                 Element element = (Element) node.get(i);
55                 if (element.getName().equals("trxId")) {
56                     trxId = element.getValue();
57                 } else if (element.getName().equals("trxBank")) {
58                     trxBank = element.getValue();
59                 } else if (element.getName().equals("trxOper")) {
60                     trxOper = element.getValue();
61                 } else if (element.getName().equals("trxDate")) {
62                     trxDate = element.getValue();
63                 } else if(element.getName().equals("PkgNo")){
64                     PkgNo=element.getValue();
65                 } else if(element.getName().equals("fileCode")){
66                     fileCode=element.getValue();
67                 } else if(element.getName().equals("fileName")){
68                     fileName=element.getValue();
69                 }else if(element.getName().equals("fileContent")){
70                     fileContent=element.getValue();
71                 }
72             }
73             Log4jBean.logger.info("                                      解析xml配置文件成功");
74             Log4jBean.logger.info("*****************************************************************************");
75             Log4jBean.logger.info("    trxId:[" + trxId + "]");
76             Log4jBean.logger.info("    trxBank:[" + trxBank + "]");            
77             Log4jBean.logger.info("    trxOper:[" + trxOper + "]");
78             Log4jBean.logger.info("    trxDate:[" + trxDate + "]");
79             Log4jBean.logger.info("    PkgNo:[" + PkgNo + "]");
80             Log4jBean.logger.info("    fileCode:[" + fileCode + "]");
81             Log4jBean.logger.info("    fileName:[" + fileName + "]");
82             Log4jBean.logger.info("    fileContent:[" + fileContent + "]");
83             Log4jBean.logger.info("*****************************************************************************");
84          } the catch (Exception E) {
 85              Log4jBean.logger.error ( "parsed xml configuration file abnormality, the abnormality information: [" + e.getMessage () + "]" );
 86          }
 87  
88      }
 89      public  static  void main (String [] args)
 90      {
 91 is          // PullConfigXml (); 
92      }

 

Guess you like

Origin www.cnblogs.com/q580syp/p/11734775.html