zoukankan      html  css  js  c++  java
  • dom4j 解析 xml标签属性


    重写onEnd()和onStart()方法

    public class XmlElementHandler implements ElementHandler {

    @Override
    public void onEnd(ElementPath arg0) {
    Element ele = arg0.getCurrent();
    String name = ele.getName();
    try 
    {
    //保存"MT"标签的属性到内存
    if("MT".equals(name))
    {
    String mTvalue = ele.getTextTrim();
    ParaCollect.getInstance().put("MT", mTvalue);
    }
    //保存"SubNo"标签的属性到内存
    if("SubNo".equals(name))
    {
    String subNovalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localPeriods", subNovalue);
    }
    //保存"Freq"标签的属性到内存
    if("Freq".equals(name))
    {
    String Freqvalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localFreq", Freqvalue);
    }
    //保存"SubCode"标签的属性到内存
    if("SubCode".equals(name))
    {
    String SubCodevalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localSubCode", SubCodevalue);
    }
    } catch (Exception e) {
    }
    }
    @Override
    public void onStart(ElementPath arg0) { }
    }

    获取标签属性

    String xmlps = “xml文件路径”;

    /*************解析xml中标签SubNo的属性 begin************************/
    SAXReader reader;
    try
    {
    InputStream in= new FileInputStream(new File(xmlps));
    ElementHandler manElementHandler=(ElementHandler) new XmlElementHandler();
    reader=new SAXReader();
    reader.addHandler("/Document/SubNo", manElementHandler);
    reader.addHandler("/Document/Freq", manElementHandler);
    reader.addHandler("/Document/SubCode", manElementHandler);
    reader.read(in);
    }
    catch (Exception e) 
    {
    log.error("公共检查解析xml异常", e);
    }

  • 相关阅读:
    shell-用户权限操作
    Python 库列表
    【random】模块运用,随机数实例
    Python 原生文件读写
    Python 运用pymysql+pandas 完成连接 MySQL 数据库读
    MySQL命名、设计及使用规范
    测试for循环计算耗时
    正则表达式速查表
    MySQL8.0.21下载安装详细教程
    MySQL 修改目录重置
  • 原文地址:https://www.cnblogs.com/baiduligang/p/4247002.html
Copyright © 2011-2022 走看看