xml文件解析org.dom4j

<?xml version="1.0" encoding="GBK" ?>
<root>

<ZXQKZT>
    <ROW DM="0" MC="未处理"/>
    <ROW DM="1" MC="处理中"/>
    <ROW DM="2" MC="处理完毕"/>
  </ZXQKZT>

<root> 代码表.xml

String filename = "D://work//代码表.xml";
    SAXReader saxreader = new SAXReader();
    try {
      saxreader.setEncoding("GBK");//
      Document document = saxreader.read(new File(filename));
      String bdsjXML = document.asXML();
      List list = document.selectNodes("//ROW/")[0];/r/n");
      Iterator iter = list.iterator();
      String temp = "";
      int i=0;
      while (iter.hasNext()) {
        i++;
        System.out.println("导入类型:"+i);
        Element element = (Element) iter.next();
        String parentName = element.getParent().getName();
        // 获得row元素的所有属性列表
        List elementList = element.attributes();
        Iterator iter1 = elementList.iterator();
        // 将每个属性转化为一个抽象属性,然后获取其名字和值
        AbstractAttribute dmAttribute = (AbstractAttribute) iter1.next();
        String dm = dmAttribute.getValue();
        AbstractAttribute mcAttribute = (AbstractAttribute) iter1.next();
        String mc = mcAttribute.getValue();
        System.out.println("parentName:" + parentName + ";DM:" + dm + ";MC:"
            + mc);
        
        if (!parentName.equals(temp)) {
          temp = parentName;
          String sqldel = "delete dm where groupname='" + parentName + "'";
          DAO.executeUpdateSqlClauseByDatabase(Constants.DATABASE_NAME, "",
              sqldel);
        }
        String sql = "insert into dm (groupname,id,name) values('"
            + parentName + "','" + dm + "','" + mc + "')";
        DAO.executeUpdateSqlClauseByDatabase(Constants.DATABASE_NAME, "",
            sql);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

你可能感兴趣的:(xml,exception,String,list,iterator,encoding)