XML解析SAX

XML解析SAX

public  List xmlElements(String xmlDoc)  {
        
//创建一个新的字符串
        StringReader read = new StringReader(xmlDoc);
        
//创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        
//创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();
        List list 
= new ArrayList();
        
try {
            
//通过输入源构造一个Document
            Document doc = sb.build(source);
            
//取的根元素
            Element root = doc.getRootElement();
            System.out.println(root.getName());
//输出根元素的名称(测试)
            
//得到根元素所有子元素的集合
            List jiedian = root.getChildren();
            
//获得XML中的命名空间(XML中未定义可不写)
            Namespace ns = root.getNamespace();
            Element et 
= null;
            DataModel dataModel 
= new DataModel();
            Collections c ;
            Ctnfee ctnfee 
= null;
            
for(int i=0;i<jiedian.size();i++){
                et 
= (Element) jiedian.get(i);//循环依次得到子元素
                ctnfee = new Ctnfee();
                
/** *//**//*
                 * 无命名空间定义时
                 * et.getChild("users_id").getText();
                 * et.getChild("users_address",ns).getText()
                 
*/

                
/**//*System.out.println(et.getChild("users_id",ns).getText());
                System.out.println(et.getChild("users_address",ns).getText());
*/

                System.out.println(et.getChild(
"p_id",ns).getText());
                System.out.println(et.getChild(
"ctnno",ns).getText());
                ctnfee.setP_id(et.getChild(
"p_id",ns).getText());
                ctnfee.setCtnno(et.getChild(
"ctnno",ns).getText());
                ctnfee.setFee_type(et.getChild(
"fee_type",ns).getText());
                ctnfee.setStart_time(et.getChild(
"start_time",ns).getText());
                ctnfee.setEnd_time(et.getChild(
"end_time",ns).getText());
                ctnfee.setFee(Double.parseDouble(et.getChild(
"fee",ns).getText()));
                ctnfee.setFee_count(Double.parseDouble(et.getChild(
"fee_count",ns).getText()));
                ctnfee.setCpid(et.getChild(
"cpid",ns).getText());
                ctnfee.setFee_name(et.getChild(
"fee_name",ns).getText());
                ctnfee.setFee_rate_id(Double.parseDouble(et.getChild(
"fee_rate_id",ns).getText()));
                ctnfee.setJffs(et.getChild(
"jffs",ns).getText());
                ctnfee.setIf_hand(et.getChild(
"if_hand",ns).getText());
                ctnfee.setSfid(et.getChild(
"sfid",ns).getText());
                ctnfee.setWt_company(et.getChild(
"wt_company",ns).getText());
                ctnfee.setOpid(et.getChild(
"opid",ns).getText());
                ctnfee.setCy(et.getChild(
"cy",ns).getText());
                ctnfee.setIf_bf(et.getChild(
"if_bf",ns).getText());
                ctnfee.setDays_count(et.getChild(
"days_count",ns).getText());
                ctnfee.setIf_collect(Double.parseDouble(et.getChild(
"if_collect",ns).getText()));
                ctnfee.setIf_dd(et.getChild(
"if_dd",ns).getText());
                ctnfee.setDd_fee_name(et.getChild(
"dd_fee_name",ns).getText());
                ctnfee.setSpec_sign(et.getChild(
"spec_sign",ns).getText());
                
                list.add(ctnfee);
                counttotal 
= counttotal +Double.parseDouble(et.getChild("fee_count",ns).getText());
            }

            
/** *//**//*
             * 如要取<row>下的子元素的名称
             
*/

            et 
= (Element) jiedian.get(0);
            List zjiedian 
= et.getChildren();
            
for(int j=0;j<zjiedian.size();j++){
                Element xet 
= (Element) zjiedian.get(j);
                System.out.println(xet.getName());
            }

        }
 catch (JDOMException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }
 catch (IOException e) {
            
// TODO 自动生成 catch 块
            e.printStackTrace();
        }

        
return list;
    }

你可能感兴趣的:(XML解析SAX)