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

java创办api,通过document生成xml返回给外部

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

 

java创建API,通过document生成xml返回给外部
/**
 * Descriptions DoSomethingAPIサーブレット
 *
 * @version xxxx-xx-xx
 * @author xxxx
 * @since JDK1.6
 *
 */
public class DoSomethingAPI extends HttpServlet {

	private static final long serialVersionUID = -6628892825606366902L;

	protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

    	// リクエストパラメータ取得
    	String uid = request.getParameter( "userid" );
    	
    	// ユーザーステータスマップ取得
    	Map<String, String> userStatusMap = new HashMap<String, String>();
    	try {
    		// 応答用データ取得
    		userStatusMap = UserHelper.getUserStatus( uid );
    	} catch ( Exception e ) {
    		e.printStackTrace();
			response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
			return;
    	}
    	
    	// 応答XML生成
    	String responseString = createResponseXmlString( userStatusMap );
    	
    	if ( responseString == null ) {
			response.sendError( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
			return;
    	}
    	
    	// 応答返却
    	response.setContentType( "text/xml; charset=UTF-8" );
		PrintWriter printWriter = response.getWriter();
		printWriter.print( responseString );
    }

	/*-------------------------------------------------------------------------
    /  メソッド名 : createResponseXmlString
    /------------------------------------------------------------------------*/
    /**
     * アップロード通知応答用のXMLを生成し、文字列として返却します。
     * @return 変換後XML文字列
     * @param responseMap 応答情報Map
     */
	public String createResponseXmlString( Map<String, String> responseMap ) {

		String retString = null;

		// XMLデータ生成
		try {

			Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			Element responseElement = document.createElement( "result"  );

			String nickname = responseMap.get( "nickname" );
			
			// nickname生成
			Element nicknameElement = document.createElement( "nickname" );
			nicknameElement.appendChild( document.createTextNode( nickname ) );
			responseElement.appendChild( nicknameElement );
			
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
		
			transformer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
			transformer.setOutputProperty("version","1.0");
			transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
		
			DOMSource source = new DOMSource( responseElement );
			StringWriter strReturn = new StringWriter();
			transformer.transform( source, new StreamResult( strReturn ) );

			retString = strReturn.toString();

		} catch ( Exception e ) {

			e.printStackTrace();
		}
		return retString;
	}
}
上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

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