BLOG ARTICLE JDOM | 1 ARTICLE FOUND

  1. 2008/04/10 JDOM을 이용한 XML Recursive Code

개똥도 약에 쓰려면 없습니다.

import java.io.IOException;
import java.io.StringReader;
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;

import weblogic.wtc.jatmi.TypedFML32;

public class XMLHelper {

 public static void getXmltoFML(String xml) {
  // TODO Auto-generated method stub
  SAXBuilder builder = new SAXBuilder();

  Document doc = null;
  try {
   doc = builder.build(new StringReader(xml));
  } catch (IOException ioe) {
   System.out.println("IOException : " + ioe.fillInStackTrace());
  } catch (JDOMException jdome) {
   System.out.println("JDOMException : " + jdome.fillInStackTrace());
  }

  Element root = doc.getRootElement();
  System.out.println(root);

  nodeParser(root);
 }

 /**
  * XML노드를 파싱.
  *
  * @param p_el
  * XML Node Element
  */
 
 public static void nodeParser(Element parent) {
  List nodeList = parent.getChildren();
  Iterator children = nodeList.iterator();
  String parentName = "";
 
  TypedFML32 data = new TypedFML32();
  String previousName = "";
  while (children.hasNext()) {
  
   Element child = (Element) children.next();
   /*
    * 아래의 attribute 순회는 필요한 경우에만 사용하세요.
    */
//   List att = child.getAttributes();
//   if (att.size() != 0) {
//    Iterator i = att.iterator();
//
//    while (i.hasNext()) {
//     /** Attribute 파싱 * */
//     Attribute at = (Attribute) i.next();
//     System.out.println("node : " + child.getName());
//     System.out.println("attribute : " + at.getName()
//       + "   attribute value : " + at.getValue());
//    }
//   }
  
   /*
    * Child Node들에 대한 순회후 FMLBuffer로 담는다.
    */
   List li = child.getChildren();
   if (li.size() != 0) {
    nodeParser(child);
    /** recursive call * */
   } else {
    /** 노드의 값이 있는 경우에만 출력 * */
    if (!child.getValue().trim().equals("")) {
     /*
      * 아래의 코드 부분이 CPU를 상승시킬 수 있으므로 먼저 테스트 후  Request를 만날 경우
      * Sibling을 순회하는 코드로 변경하여도 됨.
      */
    
     parentName = child.getParentElement().getName();
     if( (parentName.indexOf("Request")) != -1 ) {
      System.out.println(" current  node : " + child.getName());
      System.out.println("value : " + child.getValue());
     }
    }
   }
  }
 }

}

크리에이티브 커먼즈 라이센스
Creative Commons License
2008/04/10 16:12 2008/04/10 16:12