Java中XML解析工具范例
1.直接代码部分:
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;
/**
* 作用: XML解析工具类,其中的属性根据自己需要另行添加或者更改
*
*/
public class ReadFileContent
{
static FileInputStream ins;
public static String trxId; //文件id
public static String trxBank; //银行编码
public static String trxOper; //
public static String trxDate; //数据日期
public static String PkgNo; //包号
public static String fileCode; //文件编码
public static String fileName; //文件类型
public static String fileContent; //文件内容
public static void PullConfigXml(String path)
{
Log4jBean.logger.info("开始读取配置文件...");
try {
File file=null;
//本地测试路径 /home/ngpcom/dfgz/config
//String path=System.getProperty("user.home")+file.separator+"dfgz"+file.separator+"config"+file.separator+"config1.xml";
//String path =System.getProperty("user.home")+file.separator+"config"+file.separator+"config1.xml";
Log4jBean.logger.info("配置文件的路径["+path+"]");
ins = new FileInputStream(new File( path));
} catch (Exception e) {
Log4jBean.logger.error("读取配置文件异常,异常信息为:【" + e.getMessage() + "】");
}
Log4jBean.logger.info("读取配置文件成功,开始解析xml文档");
// 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML输入,此处为文件流
InputSource source = new InputSource(ins);
// 创建一个新的SAXBuilder
SAXBuilder saxbBuilder = new SAXBuilder();
try {
// 通过输入源构造一个Document
Document doc = saxbBuilder.build(source);
// 取得xml根元素
Element root = doc.getRootElement();
// 取得根元素的子元素
List<?> node = root.getChildren();
for (int i = 0; i < node.size(); i++) {
Element element = (Element) node.get(i);
if (element.getName().equals("trxId")) {
trxId = element.getValue();
} else if (element.getName().equals("trxBank")) {
trxBank = element.getValue();
} else if (element.getName().equals("trxOper")) {
trxOper = element.getValue();
} else if (element.getName().equals("trxDate")) {
trxDate = element.getValue();
} else if(element.getName().equals("PkgNo")){
PkgNo=element.getValue();
} else if(element.getName().equals("fileCode")){
fileCode=element.getValue();
} else if(element.getName().equals("fileName")){
fileName=element.getValue();
}else if(element.getName().equals("fileContent")){
fileContent=element.getValue();
}
}
Log4jBean.logger.info(" 解析xml配置文件成功");
Log4jBean.logger.info("*****************************************************************************");
Log4jBean.logger.info(" trxId:[" + trxId + "]");
Log4jBean.logger.info(" trxBank:[" + trxBank + "]");
Log4jBean.logger.info(" trxOper:[" + trxOper + "]");
Log4jBean.logger.info(" trxDate:[" + trxDate + "]");
Log4jBean.logger.info(" PkgNo:[" + PkgNo + "]");
Log4jBean.logger.info(" fileCode:[" + fileCode + "]");
Log4jBean.logger.info(" fileName:[" + fileName + "]");
Log4jBean.logger.info(" fileContent:[" + fileContent + "]");
Log4jBean.logger.info("*****************************************************************************");
} catch (Exception e) {
Log4jBean.logger.error("解析xml配置文件异常,异常信息为:【" + e.getMessage() + "】");
}
}
public static void main(String[] args)
{
//PullConfigXml();
} 相关推荐
与卿画眉共浮生 2020-10-14
xiyang 2020-08-21
XGQ 2020-07-04
Andrewjdw 2020-05-29
Yakamoz 2020-05-26
行吟阁 2020-05-18
88491874 2020-04-30
baijinswpu 2020-07-29
leonranri 2020-07-26
zhongliwen 2020-07-05
麋鹿麋鹿迷了路 2020-07-05
zengyu00 2020-07-05
CoderBoy 2020-06-28
whbing 2020-06-28
绝望的乐园 2020-06-27
wellfly 2020-06-26
菇星獨行 2020-06-25
草原孤狼 2020-06-25
坚持着执着 2020-06-16