欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

java解析xml dom形式

更新时间: 2014-01-05 02:01:47 责任编辑: Author_N1

 

java解析XML dom方式
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        // try {  
        // HelloStub stub = new HelloStub();  
        // GetUser user = new GetUser();  
        // user.setId("22");  
        // stub.getUser(user);  
        // } catch (AxisFault e) {  
        // // TODO Auto-generated catch block  
        // e.printStackTrace();  
        // } catch (RemoteException e) {  
        // // TODO Auto-generated catch block  
        // e.printStackTrace();  
        // }  
        String xml = "<?xml version='1.0' encoding='utf-8'?>"  
                + "<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">"  
                + "<soapenv:Body>"  
                + "<ns:getUserResponse xmlns:ns="http://test.lvxh.com">"  
                + " <ns:return xmlns:ax21="http://test.lvxh.com/xsd""  
                + "     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Result">"  
                + "     <ax21:code>0</ax21:code>"  
                + "     <ax21:list>hello</ax21:list>" + "       </ns:return>"  
                + "</ns:getUserResponse>" + "</soapenv:Body>"  
                + "</soapenv:Envelope>";  
        Map<String, List<Node>> map = new HashMap<String, List<Node>>();  
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
        try {  
            DocumentBuilder db = dbf.newDocumentBuilder();  
            ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());  
            Document document = db.parse(is);  
            NodeList node = document.getChildNodes();  
            Test test = new Test();  
            for (int i = 0; i < node.getLength(); i++) {  
                   
                Node a = node.item(i);  
                if(a instanceof DeferredElementImpl)  
                {  
                    test.pars(a, map);  
                }  
            }  
        } catch (ParserConfigurationException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (SAXException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        //System.out.println(map.get("code").get(0).getTextContent());  
       
    }  
       
    public void pars(Node node, Map<String, List<Node>> map)  
    {  
        NodeList nodeList = node.getChildNodes();  
        if(node instanceof DeferredElementImpl)  
        {  
            String name = node.getNodeName().split(":")[1];  
            if (map.get(name) != null) {  
                map.get(name).add(node);  
                   
            } else {  
                List<Node> l = new ArrayList<Node>();  
                l.add(node);  
                map.put(name, l);  
            }  
        }  
        if (nodeList == null || nodeList.getLength() == 0) {  
            return;  
        } else {  
            for (int i = 0; i < nodeList.getLength(); i++) {  
                Node n = nodeList.item(i);  
                if(n instanceof DeferredElementImpl)  
                {  
                pars(n, map);  
                }  
            }  
        }  
    }  
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:01:47
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/4722/
本文WWW.DOC100.NET DOC100.NET版权所有。