레이블이 xml인 게시물을 표시합니다. 모든 게시물 표시
레이블이 xml인 게시물을 표시합니다. 모든 게시물 표시

2012-10-31

java Framework/java]Bean to XML



package org.kodejava.example.bean;

import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;

public class BeanToXML {
    private Long id;
    private String itemName;
    private String itemColour;
    private Integer itemQuantities;

    public static void main(String[] args) {
        BeanToXML bean = new BeanToXML();
        bean.setId(new Long(1));
        bean.setItemName("T-Shirt");
        bean.setItemColour("Dark Red");
        bean.setItemQuantities(new Integer(100));
       
        try {
            XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
                    new FileOutputStream("Bean.xml")));

            //
            // Write an XML representation of the specified object to the output.
            //
            encoder.writeObject(bean);
            encoder.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public String getItemColour() {
        return itemColour;
    }

    public void setItemColour(String itemColour) {
        this.itemColour = itemColour;
    }

    public Integer getItemQuantities() {
        return itemQuantities;
    }

    public void setItemQuantities(Integer itemQuantities) {
        this.itemQuantities = itemQuantities;
    }
}


The XML persistence will be like:
<?xml version="1.0" encoding="UTF-8"?> 

<java version="1.6.0_02" class="java.beans.XMLDecoder"> 

 <object class="org.kodejava.example.bean.BeanToXML"> 

  <void property="id"> 

   <long>1</long> 

  </void> 

  <void property="itemColour"> 

   <string>Dark Red</string> 

  </void> 

  <void property="itemName"> 

   <string>T-Shirt</string> 

  </void> 

  <void property="itemQuantities"> 

   <int>100</int> 

  </void> 

 </object> 

</java>

2012-10-26

Java] post 방식으로 header를 설정한 후 xml 데이터를 서버에 전달



apache http-client 3.x.jar필요.

출처 : http://danhobak.textcube.com/22



http post 방식으로 header를 설정한 후 xml 데이터를 서버에 전달 후

그 결과를 받는다.

첨부된 파일을 압축을 풀면 lib 디렉토리 안의 jar 파일들을

%CATALINA_HOME%\common\lib 에 위치시키거나

CALSSPASS를 잡아준 후 이용하면 된다.


<%@ page import="org.apache.commons.httpclient.HttpClient" %>
<%@ page import="org.apache.commons.httpclient.methods.PostMethod" %>
<%@ page import ="java.net.*" %>
<%@ page import ="java.io.*" %>

<%

    String orgMsg="abcdefg";
    String encMsg = null;
    String header1 = "header1";
    String header2 = "header2";
    String header3 = "header3";
    String ContentsType="text/xml";
  

    AES256 aes = new AES256();
    encMsg = aes.encrypt(msg);

      String xmldata = "<? xml version=\"1.0\" encoding=\"euc-kr\"?> "
                            +"<! DOCTYPE lgtpims:schema>"
                            +"<schema targetns=\"http://www.aaa.com\""
                    +"version=\"1.0\" xmlns=\"http://www.w3.org/2000/10/XMLSchema\">"
                        +"<parameter method=\"add\">"
                            +"<element-value name=\"addelement\">aaaaa</element-value>"
                            +"<element-value name=\"addelementtype\">bbbbb</element-value>"
                        +"</parameter>"
                    +"</schema>" ;

        String strURL = "http://www.aaa.com/aaa";
        PostMethod post = new PostMethod(strURL);
        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
        post.setRequestHeader("header1","header1");
        post.setRequestHeader("header2",header2);
        post.setRequestHeader("header3",header3");
        post.setRequestHeader("Content-Length",Integer.toString(xmldata.length()));
        post.setRequestBody(xmldata);

        HttpClient httpclient = new HttpClient();          

        try {
            int result = httpclient.executeMethod(post);
            // Display status code
            out.println("<br>Response status code: " + result);
            // Display response
             out.println("<br>Response header: "+post.getResponseHeader("Status-Line"));
            out.println("<br>Response body: ");
            out.println(post.getResponseBodyAsString());
        } finally {
            // Release current connection to the connection pool once you are done
            post.releaseConnection();
        }

%>