java解析xml工具_Java中XML解析工具范例

1 importjava.io.File;2 importjava.io.FileInputStream;3 importjava.util.List;4 importorg.jdom.Document;5 importorg.jdom.Element;6 importorg.jdom.input.SAXBuilder;7 importorg.xml.sax.InputSource;8

9 /**

10 * 作用: XML解析工具类,其中的属性根据自己需要另行添加或者更改11 *12 */

13 public classReadFileContent14 {15 staticFileInputStream 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 voidPullConfigXml(String path)28 {29 Log4jBean.logger.info("开始读取配置文件...");30 try{31 File file=null;32 //本地测试路径 /home/ngpcom/dfgz/config33 //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(newFile( 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 = newInputSource(ins);44 //创建一个新的SAXBuilder

45 SAXBuilder saxbBuilder = newSAXBuilder();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 voidmain(String[] args)90 {91 //PullConfigXml();

92 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; /** * 本类是专门解析XML文件的,主要用于为系统读取自己的配置文件时提供最方便的解析操作 * @author HX * */ public class XmlManager { /** * 得到某节点下某个属性的值 * @param element 要获取属性的节点 * @param attributeName 要取值的属性名称 * @return 要获取的属性的值 * @author HX_2010-01-12 */ public static String getAttribute( Element element, String attributeName ) { return element.getAttribute( attributeName ); } /** * 获取指定节点下的文本 * @param element 要获取文本的节点 * @return 指定节点下的文本 * @author HX_2010-01-12 */ public static String getText( Element element ) { return element.getFirstChild().getNodeValue(); } /** * 解析某个xml文件,并在内存创建DOM树 * @param xmlFile 要解析XML文件 * @return 解析某个配置文件后的Document * @throws Exception xml文件不存在 */ public static Document parse( String xmlFile ) throws Exception { // 绑定XML文件,建造DOM树 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document domTree = db.parse( xmlFile ); return domTree; } /** * 获得某节点下的某个子节点(指定子节点名称,和某个属性的值) * 即获取parentElement下名字叫childName,并且属性attributeName的值为attributeValue的子结点 * @param parentElement 要获取子节点的那个父节点 * @param childName 要获取的子节点名称 * @param attributeName 要指定的属性名称 * @param attributeValue 要指定的属性的值 * @return 符合条件的子节点 * @throws Exception 子结点不存在或有多个符合条件的子节点 * @author HX_2008-12-01 */ public static Element getChildElement( Element parentElement, String childName, String attributeName, String attributeValue ) throws Exception { NodeList list = parentElement.getElementsByTagName( childName ); int count = 0; Element curElement = null; for ( int i = 0 ; i < list.getLength() ; i ++ ) { Element child = ( Element )list.item( i ); String value = child.getAttribute( attributeName ); if ( true == value.equals( attributeValue ) ) { curElement =

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值