Java中XML解析工具范例

1.直接代码部分:

 1 import java.io.File;
 2 import 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  * 作用: XML解析工具类,其中的属性根据自己需要另行添加或者更改
11  * 
12  */
13 public class ReadFileContent
14 {
15     static FileInputStream ins;
16 
17     public static String trxId;              //文件id
18     public static String trxBank;            //银行编码
19     public static String trxOper;            //
20     public static String trxDate;            //数据日期
21     public static String PkgNo;              //包号
22     public static String fileCode;           //文件编码
23     public static String fileName;           //文件类型
24     public static String fileContent;        //文件内容
25     
26     
27     public static void PullConfigXml(String path)
28     {
29         Log4jBean.logger.info("开始读取配置文件...");
30         try {   
31             File file=null;
32             //本地测试路径  /home/ngpcom/dfgz/config
33             //String path=System.getProperty("user.home")+file.separator+"dfgz"+file.separator+"config"+file.separator+"config1.xml";            
34             //String path =System.getProperty("user.home")+file.separator+"config"+file.separator+"config1.xml";
35             Log4jBean.logger.info("配置文件的路径["+path+"]");
36             ins = new FileInputStream(new File( path));
37         } catch (Exception e) {
38             Log4jBean.logger.error("读取配置文件异常,异常信息为:【" + e.getMessage() + "】");
39         }
40         Log4jBean.logger.info("读取配置文件成功,开始解析xml文档");
41 
42         // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML输入,此处为文件流
43         InputSource source = new InputSource(ins);
44         // 创建一个新的SAXBuilder
45         SAXBuilder saxbBuilder = new SAXBuilder();
46         try {
47             // 通过输入源构造一个Document
48             Document doc = saxbBuilder.build(source);
49             // 取得xml根元素
50             Element root = doc.getRootElement();
51             // 取得根元素的子元素
52             List<?> node = root.getChildren();
53             for (int i = 0; i < node.size(); i++) {
54                 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         } catch (Exception e) {
85             Log4jBean.logger.error("解析xml配置文件异常,异常信息为:【" + e.getMessage() + "】");
86         }
87 
88     }
89     public static void main(String[] args)
90     {
91         //PullConfigXml();
92     }

猜你喜欢

转载自www.cnblogs.com/q580syp/p/11734775.html