博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xml解析
阅读量:5816 次
发布时间:2019-06-18

本文共 2459 字,大约阅读时间需要 8 分钟。

创建Dom4j的Document用于解析xml

protected Element createDocument(String xml) {

  try {
   Document document = new SAXReader().read(Thread.currentThread()
     .getContextClassLoader().getResourceAsStream(xml));
   // Document document = DocumentHelper.parseText(xml);
   Element root = document.getRootElement();

   return root;

  } catch (DocumentException e) {
   e.printStackTrace();
  }

  return null;

 }

@Override

 public void parseXML(String xml, List<AnnualTargetSummary> list) {
  Element element = createDocument(xml);
  // 提取统计月指标数据

  List<Element> l = element.selectNodes("//deptTargetSummaries"); // 指标数据

  if (null != l) {

   for (int i = 0; i < l.size(); i++) {

    Element __e = (Element) element.selectObject("//summarydate["

      + (i + 1) + "]");// 获取Summarydate节点

    Element _e = l.get(i); // deptTargetSummaries

    List<Element> infoList = _e

      .selectNodes("targets/targetSummaries");

    AnnualTargetSummary ats = new AnnualTargetSummary();

    List<DeptmentSummary> dslist = new ArrayList<DeptmentSummary>();
    ats.setDateStr(__e.getText()); // 设置月份
    ats.setMonth(i + 1);
    if (null != infoList) {
     for (int j = 0; j < infoList.size(); j++) {
      DeptmentSummary ds = new DeptmentSummary();
      Element deptElement = (Element) _e
        .selectObject("deptname[" + (j + 1) + "]");

      ds.setDeptName(deptElement.getText());// 设置部门

      Element summaries = infoList.get(j);

      List<Element> summariesList = summaries

        .selectNodes("name");
      if (null != summariesList) {
       List<SummaryInfo> summaryInfoList = new ArrayList<SummaryInfo>();
       for (int k = 0; k < summariesList.size(); k++) {
        SummaryInfo summaryInfo = new SummaryInfo();

        Element value = summariesList.get(k);// 名称

        summaryInfo.setName(value.getText());

        value = (Element) summaries

          .selectObject("unit[" + (k + 1) + "]");// 单位
        summaryInfo.setUnit(value.getText());

        value = (Element) summaries

          .selectObject("completed[" + (k + 1)
            + "]");
        summaryInfo.setCompleted(value.getText());

        value = (Element) summaries

          .selectObject("basiscompleted["
            + (k + 1) + "]");
        summaryInfo.setBasiscompleted(value.getText());

        value = (Element) summaries

          .selectObject("yearcompleted["
            + (k + 1) + "]");
        summaryInfo.setYearcompleted(value.getText());

        summaryInfoList.add(summaryInfo);

       }

       ds.setSummaryInfoList(summaryInfoList);

      }

      dslist.add(ds);

     }
     ats.setDeptments(dslist);

    }

    list.add(ats);
   }
  }
 }

解析

String xml = "GetAnnualTargetSummary.xml";

  
  Parser parser = XMLParseFactory.getParser("GetAnnualTargetSummary");

  List<AnnualTargetSummary> list = new ArrayList<AnnualTargetSummary>();

  parser.parseXML(xml, list);

转载地址:http://uzmbx.baihongyu.com/

你可能感兴趣的文章
通过vb.net 和NPOI实现对excel的读操作
查看>>
TCP segmentation offload
查看>>
java数据类型
查看>>
数据结构——串的朴素模式和KMP匹配算法
查看>>
FreeMarker-Built-ins for strings
查看>>
验证DataGridView控件的数据输入
查看>>
POJ1033
查看>>
argparse - 命令行选项与参数解析(转)
查看>>
一维数组
查看>>
Linux学习笔记之三
查看>>
CentOS 6.6 FTP install
查看>>
图解Ajax工作原理
查看>>
oracle导入导出小记
查看>>
聊一聊log4j2配置文件log4j2.xml
查看>>
NeHe OpenGL教程 第七课:光照和键盘
查看>>
修改上一篇文章的node.js代码,支持默认页及支持中文
查看>>
Php实现版本比较接口
查看>>
删除设备和驱动器中软件图标
查看>>
第四章 TCP粘包/拆包问题的解决之道---4.1---
查看>>
html语言
查看>>