JDK1.6 DOM을 이용한 간단 XML 파싱

import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.log4j.Logger;


public class XMLReader {
    private static final Logger logger = Logger.getLogger(com.msc.sdm.ic.pub.SmgrDbObjectReader.class);
    private String szXMLFileName;
    private File xmlFile;
    private Document doc = null;
    public SmgrDbObjectReader(){
        szXMLFileName = “”;
        xmlFile = null;               
    }
   
    public void setXMLFile(File file){
        xmlFile = file;
    }
   
    public void readXMLFile(){
       
        if(xmlFile != null && xmlFile.exists() == true){
           try{
           DocumentBuilderFactory docBuilderFactory =
   DocumentBuilderFactory.newInstance();
           DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
           doc = docBuilder.parse(xmlFile);
           Element elem = doc.getDocumentElement();
           String root_node = elem.getNodeName();
           NodeList node_list = elem.getChildNodes();
          
           int leng = node_list.getLength();
  for (int i = 0; i < leng; i++) {
                    Node node = node_list.item(i);
                    if(node.getNodeType() == node.ELEMENT_NODE){
                        //logger.warn(“Node Type : ” + node.getNodeType());
                        NodeList childList = node.getChildNodes();                   
                        logger.warn(“Node Name :”+node.getNodeName());
                        for( int  j = 0 ;  j < childList.getLength(); j++){
                            Node child = childList.item(j);
                            if(child.getNodeType() == child.ELEMENT_NODE){
                                //logger.warn(“Child Type   : “+ child.getNodeType());
                                logger.warn(“Child Node   : “+ child.getNodeName());
                                logger.warn(“Child Content: “+ child.getTextContent());
                                NamedNodeMap attrmap = childList.item(j).getAttributes();
                                if(attrmap != null)
                                    for( int  k = 0 ;  k < attrmap.getLength(); k++){
                                        Node attr_node = attrmap.item(k);
                                        logger.warn(“Attribute Node   : “+ attr_node.getNodeName());
                                        logger.warn(“Attribute Content: “+ attr_node.getTextContent());
                                    }
                            }
                        }
                    }
  }
           }catch(Exception e){
               e.printStackTrace();
           }
        }
    }                  
}

Table용 Page Util

package kr.or.struts.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>Title: PageUtil </p>
 * <p>Description: Table Page Util </p>
 * <p>Copyright: Copyright (c) 2007 Maxoft.Inc</p>
 * <p>Company: Maxoft.Inc </p>
 * @author Kim sooki
 * @version 1.0
 */
public class PageUtil {
 // 객체없이 사용하는 utitlity클래스
 private PageUtil(){
 }
 
 private static Log logger = LogFactory.getLog(PageUtil.class);
 
// 글의 갯수만큼 입력받아 모든페이지의 링크를 생성
// String page : page이름
// int total : 전체 글 갯수
// int showLine : 한페이지에 표시될 글의 갯수
// int current : 현재페이지
 public static String getAllPageLink(String page,int total, int showLine,int current){
  if(total == 0)
   return “”;
  StringBuffer buffer = new StringBuffer();  
  int count = 1;  
  for(int i = 1; i <= total; i=i+showLine){
   buffer.append(“<a href=”);
   buffer.append(page);
   buffer.append(“PageNo=”);  
   buffer.append(Integer.toString(count));
   buffer.append(“>”);
   if(count == current){
     buffer.append(“<strong>”);
    buffer.append(Integer.toString(count));
     buffer.append(“</strong>”);  
   } else

   buffer.append(Integer.toString(count));
   buffer.append(“</A>&nbsp;&nbsp;”);  
   count++;
  }
  return buffer.toString();
 }
 
 // 전체 글수와 현재페이지수를 입력하여 전체 페이지수를 구함
 public static int getMaxPage(int total, int showLine){
  if(total <= 0){
   return 0;
  }
  int temp = total / showLine;
  int mode = total % showLine;
  if(mode != 0){
   temp++;
  }
  return temp;
 }
 
 // 페이지에서 시작되는 가장 나중번호를 구한다.
 public static int getRowStart(int total, int showLine,int currentPage){
  if(total <= showLine){
   return total;
  }
  int temp = showLine * (currentPage-1);
  temp = total – temp;
  if(temp < 0){
   temp = 0;
  }
  return temp;
 }
 // 페이지에서 시작되는 가장 적은 번호를 구한다.
 public static int getRowEnd(int total, int showLine,int currentPage){
  if(total <= showLine){
   return 0;
  }
  int temp = (showLine * (currentPage -1)) + showLine;
  temp = total – temp;
  if(temp < 0 ){
   temp = 0;
  } 
  return temp;
 }
}

Property Facade

Property Facade를 작성하여 여러 Property설정파일을 읽어들여  세팅하도록 해봤다.


public class PropertiesFacade
{


    public PropertiesFacade()
    {
    }


    public void loadProperties(String fileName)
    {
        if(properties == null)
            try
            {
                String localFilePath = “resources/config/”;
                String jarFilePath = “/config/”;
                URL url = getClass().getResource(jarFilePath + fileName);
                InputStream fileIn;
                if(url == null)
                    fileIn = new FileInputStream(localFilePath + fileName);
                else
                if(url.getFile().toString().indexOf(“!”) != -1)
                {
                    JarURLConnection jarConn = (JarURLConnection)url.openConnection();
                    fileIn = jarConn.getInputStream();
                } else
                {
                    fileIn = new FileInputStream(url.getPath());
                }
                properties = new Properties();
                properties.load(fileIn);
                fileIn.close();
            }
            catch(Exception e)
            {


                e.printStackTrace();
               


            }
    }


    public Properties getProperties()
    {
        return properties;
    }


    public String getProperty(String name)
    {
        if(properties.getProperty(name) != null)
            return properties.getProperty(name);
        else
            return “null”;
    }



    private Properties properties;


}


  getProperties()나 getProperty(name)을 이용하여 설정된 프로퍼티를 가져올수 있다.



 A class extends PropertiesFacade 처럼 상속하여 추가기능으로 사용하도록 만들었다..

String localFilePath = “resources/config/”;
String jarFilePath = “/config/”;
의 path를 적절히 바꾸어 사용하면 된다.

Axis 에서 Map사용하기







 Axis에서는 배열형태나 Vector형태는 기본적으로 전송을 지원하지만 Map같은 Collection형태는 지원해주지 않는다.


wsdl2java를 이용하면 Map에 대응하는 java파일을 얻을수 있다.


– service.wsdl –


..


<complexType name=”mapItem”>
    <sequence>
     <element name=”key” nillable=”true” type=”xsd:anyType”/>
     <element name=”value” nillable=”true” type=”xsd:anyType”/>
    </sequence>
   </complexType>
   <complexType name=”Map”>
    <sequence>
     <element maxOccurs=”unbounded” minOccurs=”0″ name=”item” type=”apachesoap:mapItem”/>
    </sequence>
   </complexType>



– wsdl2java 로 얻어진 MapItem.java –


/**
 * MapItem.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis 1.3 Oct 05, 2005 (05:23:37 EDT) WSDL2Java emitter.
 */


package org.apache.xml.xml_soap;


public class MapItem  implements java.io.Serializable {
    private java.lang.Object key;


    private java.lang.Object value;


    public MapItem() {
    }


    public MapItem(
           java.lang.Object key,
           java.lang.Object value) {
           this.key = key;
           this.value = value;
    }



    /**
     * Gets the key value for this MapItem.
     *
     * @return key
     */
    public java.lang.Object getKey() {
        return key;
    }



    /**
     * Sets the key value for this MapItem.
     *
     * @param key
     */
    public void setKey(java.lang.Object key) {
        this.key = key;
    }



    /**
     * Gets the value value for this MapItem.
     *
     * @return value
     */
    public java.lang.Object getValue() {
        return value;
    }



    /**
     * Sets the value value for this MapItem.
     *
     * @param value
     */
    public void setValue(java.lang.Object value) {
        this.value = value;
    }



}


– deploy.wsdl에 추가된 beanMapping –


<beanMapping
        xmlns:ns=”http://xml.apache.org/xml-soap
        qname=”ns:mapItem”
        languageSpecificType=”java:org.apache.xml.xml_soap.MapItem” 
       ENCODINGStyle=”http://schemas.xmlsoap.org/soap/ENCODING/
      />


– wsdl에서 Map의 참조방법 –


<element name=”customFields” nillable=”true” type=”apachesoap:Map”/>

Vector는 Object의 배열로 처리할수 있는것처럼 Map은 key와 value의 Pair인 MapItem배열을 통해 전송할 수있다.