dom解析xml字符串

来源:互联网 发布:网络里认识你 歌词 编辑:程序博客网 时间:2024/05/16 06:08

xmlstr=<?xml version="1.0" encoding="GB2312"?><departmentList><department><departmentId>2</departmentId><domainId>300240</domainId><departmentName>3010</departmentName><description>斜土路</description><depId>1</depId><rank>null</rank><type>null</type><top>0</top><resv1>0</resv1><resv2>3010</resv2></department><department><departmentId>1</departmentId><domainId>300240</domainId><departmentName>RZRQ</departmentName><description>信用业务部</description><depId>0</depId><rank>null</rank><type>null</type><top>0</top><resv1>0</resv1><resv2>rzrq</resv2></department><department><departmentId>4</departmentId><domainId>300240</domainId><departmentName>信用交易部</departmentName><description>null</description><depId>0</depId><rank>null</rank><type>null</type><top>1</top><resv1>0</resv1><resv2>xyjyb</resv2></department><department><departmentId>3</departmentId><domainId>300240</domainId><departmentName>3020</departmentName><description>武宁路</description><depId>1</depId><rank>null</rank><type>null</type><top>1</top><resv1>0</resv1><resv2>3020</resv2></department><department><departmentId>5</departmentId><domainId>300240</domainId><departmentName>102</departmentName><description>莲前西营业部</description><depId>0</depId><rank>null</rank><type>null</type><top>2</top><resv1>0</resv1><resv2>102</resv2></department><department><departmentId>6</departmentId><domainId>300240</domainId><departmentName>国海测试部门01</departmentName><description>描述文本</description><depId>0</depId><rank>null</rank><type>null</type><top>3</top><resv1>null</resv1><resv2>null</resv2></department></departmentList>

/解析xmlstring字符串

    public static List parseXml(String xmlstr){
        List dataList=new ArrayList();
        //1解析器 工厂 类
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        try
        {
            //通过 解析器工厂创建 一个 解析 器
            DocumentBuilder db=dbf.newDocumentBuilder();
            //解析xml文件
            //  Document dm=db.parse("f://person.xml");
            //解析xml字符串
            //将该字符串转为InputStream流
            InputStream iStream=new ByteArrayInputStream(xmlstr.getBytes());
            Document dm=db.parse(iStream);
            //得到 所有 department节点
            NodeList departments=dm.getElementsByTagName("department");
            for(int i=0;i<departments.getLength();i++){
                Map dataMap=new HashMap();
                //element 和 node 是 同一 概念
                //不同 的 是 element 提供 更多 方法
                Element departElement = (Element)departments.item(i);
                //部门ID
                String departmentId=departElement.getElementsByTagName("departmentId")//部门ID
                        .item(0).getFirstChild().getNodeValue();
                //部门名称
                String departmentName=departElement.getElementsByTagName("departmentName")//部门名称
                        .item(0).getFirstChild().getNodeValue();
                dataMap.put("departmentId", departmentId);
                dataMap.put("departmentName", departmentName);
                dataList.add(dataMap);
            }
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return dataList;
    }
0 0
原创粉丝点击