java解析XML dom方式_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java解析XML dom方式

java解析XML dom方式

 2013/8/21 0:52:55  lee123lee123  程序员俱乐部  我要评论(0)
  • 摘要:/***@paramargs*/publicstaticvoidmain(String[]args){//try{//HelloStubstub=newHelloStub();//GetUseruser=newGetUser();//user.setId("22");//stub.getUser(user);//}catch(AxisFaulte){////TODOAuto-generatedcatchblock//e.printStackTrace();//}catch
  • 标签:Java 方式 XML 解析
class="java" name="code">
    /** 
     * @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);  
                }  
            }  
        }  
    }  
上一篇: mybatis生成代码方式 下一篇: 没有下一篇了!
发表评论
用户名: 匿名