'XML'에 해당되는 글 2건

  1. 2009.01.07 xmlParser

xmlParser

|

package filter;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class xmlParser {
 private static String filePath = "tmp.xml";
 
 public xmlParser(){}
 
 public static void main(String[] args) {
  SAXBuilder builder = new SAXBuilder();
  Document doc = null;
  try {
   doc = builder.build(new File(filePath));
  } catch (IOException ioe) {
   System.out.println("IOException : " + ioe.fillInStackTrace());
  } catch (JDOMException jdome) {
   System.out.println("JDOMException : " + jdome.fillInStackTrace());
  }
 
  Element root = doc.getRootElement();
 
  /** ROOT전체 파싱 **/
  nodeParser(root);
 
  /** 특정 노드 리스트를 파싱 **/
  //  List sqlElement = root.getChildren("sql");
  //  getSQL(root, "RETRIEVE_SAMEPLE2");
 }
 
 public static String getSQL(Element p_root, String p_sql) {
  List sqlElement = p_root.getChildren("sql");
  for (int i=0; i<sqlElement.size(); i++) {
  
   Element e = (Element)sqlElement.get(i);
   if(e.getAttributeValue("name").equals(p_sql)) {
    return e.getChildText("query");
   }
  }
  return null;
 }
 
 public static void nodeParser(Element p_el) {
  List list = p_el.getChildren();
  Iterator it = list.iterator();
  while(it.hasNext()) {
   Element e = (Element)it.next();
   List att = e.getAttributes();
   if (att.size() != 0) {
    Iterator i = att.iterator();
   
    while(i.hasNext()) {
     /** Attribute 파싱 **/
     Attribute at = (Attribute)i.next();
     System.out.println("node : " + e.getName());
     System.out.println("attribute : " + at.getName() +"   attribute value : " + at.getValue());
    }
   }
   List li = e.getChildren();
   if (li.size() != 0) {
    nodeParser(e); /** recursive call **/
   } else {
    /** 노드의 값이 있는 경우에만 출력 **/
    if (!e.getValue().trim().equals("")) {
     System.out.println("parent node : " + e.getParentElement().getName() + " current  node : " + e.getName());
     System.out.println("value : " + e.getValue());
    }
   }
  }
 }
}


 

'JAVA/JSP > Source' 카테고리의 다른 글

html tag filter  (0) 2009.01.09
test  (0) 2009.01.08
해당 url html 소스 가져오기  (0) 2009.01.07
게시판, 자료실, XML과 JSP, JDOM, JSTL, Custom TAG  (0) 2009.01.07
Javascript  (0) 2008.10.28
And
prev | 1 | 2 | next