`
lee123lee123
  • 浏览: 9503 次
社区版块
存档分类
最新评论

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);  
                }  
            }  
        }  
    }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics