JAVA 解析xml 读取里面数据

阅读更多

附 代码   有基础的就看得懂

 

package org.bean.pub;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

 

 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import skin.UnitAdd_ImportTXDialog;
import skin.UnitAdd_PricingDialog;
import bean.Fxspa;
import bean.Fxspd;
import bean.Fxspe;
import bean.Fxxta;

public  class ImportXml {
 private  String path;
 private UnitAdd_ImportTXDialog paneltp;
 private UnitAdd_PricingDialog pp;
 private List speObjs = new ArrayList();
 public   ImportXml(UnitAdd_ImportTXDialog zhi,UnitAdd_PricingDialog pt,String dizhi){
  super();
  if(dizhi!=null&&pt!=null){
   path=dizhi;
   pp=pt;
  }else{
   paneltp=zhi;
   path=FileChooser();
  }
 
  
  try {
  if(path!=null){
    File file = new File(path);
     SAXReader reader = new SAXReader();
                 Document document = reader.read(file);// 读取XML文件 
                 Element root = document.getRootElement();// 得到根节点 
                 PriceSystem ps;
                 for ( Iterator i = root.elementIterator("CP"); i.hasNext();){
                     Element foo = (Element)i.next(); 
                     ps=new PriceSystem();
                     ps.setCpid(Integer.valueOf(foo.attributeValue("ID")));
                     ps.setCplbid(Integer.valueOf(foo.element("CPLB").attributeValue("ID")));
                     ps.setCplb(foo.elementText("CPLB"));
                     ps.setCpmc(foo.elementText("CPMC"));
                     ps.setGg(foo.elementText("GG"));
                     ps.setGgid(Integer.valueOf(foo.element("GG").attributeValue("ID")));
                     ps.setDwid(Integer.valueOf(foo.element("DW").attributeValue("ID")));
                     ps.setDw(foo.elementText("DW"));
                     ps.setJg(foo.elementText("JG"));
                     ps.setCpjc(foo.elementText("CPJC"));
      speObjs.add(ps);
                 } 
                 if(dizhi!=null&&pt!=null){
                  pp.setImportcp(speObjs);
           }else{
            paneltp.setImportob(speObjs);
           }
  }
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
 private String FileChooser(){
  JFileChooserDemo  jf=new JFileChooserDemo(GenerateXML.path);
  jf.setMultiSelectionEnabled(false);
  jf.setFileSelectionMode(JFileChooser.FILES_ONLY);
  jf.setFileHidingEnabled(true);
  jf.setAcceptAllFileFilterUsed(false);
  jf.setFileFilter(new FileNameExtensionFilter("Xml文件(*.xml)", "xml"));
  int rtvalue=jf.showOpenDialog(null);
  if(rtvalue==JFileChooser.APPROVE_OPTION){
   return jf.getSelectedFile().getPath();
  }else{
   return null;
  }
 }
 
   class JFileChooserDemo extends javax.swing.JFileChooser{
 public JFileChooserDemo(String currentDirectoryPath){
  super(currentDirectoryPath);
 }
 @Override
 public void approveSelection(){
  File file = new File(getSelectedFile(), ".xml");
  if(file.exists()){
   JOptionPane.showMessageDialog(getParent(), "文件名已被占用");
   return;
  }else{
   super.approveSelection();
  }
 }
 
}
 
 
}

你可能感兴趣的:(JAVA 解析xml 读取里面数据)