CollHostXmlHelper.java 3.53 KB
package com.sitech.ismp.coll.tivoli;

import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class CollHostXmlHelper {
	private Logger logger = Logger.getLogger("COLL");
	
	/**
	 * 按指定编码格式解析content,并返回List
	 * @param content
	 * @param strCharSet
	 * @return
	 */
	public static  List parseSoapResponse(String content,String strCharSet)
	{
        List list_tmp = new java.util.Vector();
        java.util.Map  map_tmp = null;

        try{

        	String str_reponse = content;

        	Document doc_response = XMLUtil.parseDocumentFromString(str_reponse,strCharSet);
        	NodeList nl = doc_response.getElementsByTagName("ROW");

        	if(nl.getLength()==0){
        		//throw new SoapAccessException("----------no data response!!!----------");
        	}

        	for ( int i=0; i<nl.getLength(); i++ ){

        		Element rowElement = (Element) nl.item(i);
        		map_tmp = new HashMap();
        		
        		//得到所有的有key-value的node
        		NodeList vlaueNodes = rowElement.getChildNodes();
        		for(int j=0; j<vlaueNodes.getLength(); j++)
        		{
            		Node valueNode =  vlaueNodes.item(j);
            		
            		if("#text".equals(valueNode.getNodeName())){
            			continue;
            		}   
            		
        			String s_key = valueNode.getNodeName();
        			
        			String s_value = "";
        			Node first = valueNode.getFirstChild();
        			if(first == null){
        				s_value="";
        			}else{
        				s_value = first.getNodeValue();
        			}
        				
        			map_tmp.put( s_key,  s_value ) ;
        			
        			//System.out.println(s_key+":"+s_value);
        		}
 
        		
        		list_tmp.add(map_tmp);
        	}

        }catch(Exception e){

        	e.printStackTrace();
        }

        return list_tmp;
	}
	
	public static  List parseSoapResponse(String content)
	{
        List list_tmp = new java.util.Vector();
        java.util.Map  map_tmp = null;

        try{

        	String str_reponse = content;

        	Document doc_response = XMLUtil.parseDocumentFromString(str_reponse);
        	NodeList nl = doc_response.getElementsByTagName("ROW");

        	if(nl.getLength()==0){
        		//throw new SoapAccessException("----------no data response!!!----------");
        	}

        	for ( int i=0; i<nl.getLength(); i++ ){

        		Element rowElement = (Element) nl.item(i);
        		map_tmp = new HashMap();
        		
        		//得到所有的有key-value的node
        		NodeList vlaueNodes = rowElement.getChildNodes();
        		for(int j=0; j<vlaueNodes.getLength(); j++)
        		{
            		Node valueNode =  vlaueNodes.item(j);
            		
            		if("#text".equals(valueNode.getNodeName())){
            			continue;
            		}   
            		
        			String s_key = valueNode.getNodeName();
        			
        			String s_value = "";
        			Node first = valueNode.getFirstChild();
        			if(first == null){
        				s_value="";
        			}else{
        				s_value = first.getNodeValue();
        			}
        				
        			map_tmp.put( s_key,  s_value ) ;
        			
        			//System.out.println(s_key+":"+s_value);
        		}
 
        		
        		list_tmp.add(map_tmp);
        	}

        }catch(Exception e){
        	System.out.println("xmlError = " + content);
        	//e.printStackTrace();
        }

        return list_tmp;
	}
}