Please Enable JavaScript!
Gon[ Enable JavaScript ]

Velocity 에서 xml 을 사용하여 표현하기

자바(JAVA)
반응형

Velocity 에서 xml 을 사용하기 위해서는 jdom-1.0.jar 사용한다.

import org.jdom.Document;

import org.jdom.input.SAXBuilder;

 

org.apache.xerces.parsers.SAXParser 사용해서 생성한

Document root 객체를 velocity 넘겨주면 이것을 활용하여 xml 내용을 분해해서

사용할 수가 있다. 그리고 위의 클래스를 사용하기 위해선 xerces.jar 가 필요하다.

첨부하지 않는다면 다음과 같은 에러가 날것이다.

org.jdom.JDOMException: Could not load org.apache.xerces.parsers.SAXParser: SAX2 driver class org.apache.xerces.parsers.SAXParser not found: org.apache.xerces.parsers.SAXParser

 

======== 실행 파일 ===========

import java.io.StringWriter;

 

import org.apache.velocity.Template;

import org.apache.velocity.VelocityContext;

import org.apache.velocity.app.Velocity;

import org.jdom.Document;

import org.jdom.input.SAXBuilder;

 

public class IteratorClass {

 

  public static void main(String[] args) throws Exception {

         try {

          Velocity.init();

 

          SAXBuilder builder;

          Document root = null;

          try {

              builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");

              root = builder.build("./src/handling.xml");

          } catch (Exception e) {

              System.out.println(e);

          }

 

          VelocityContext context = new VelocityContext();

          context.put("root", root);

 

          Template template = Velocity.getTemplate("./src/handling.vm");

 

          StringWriter writer = new StringWriter();

          template.merge(context, writer);

 

          System.out.println(writer);

      } catch (Exception e) {

          System.out.println(e);

      }

       }

}

 

 =========  handling.xml ===========

<?xml version="1.0" encoding="UTF-8"?>

<document>

    <student>

        <rno>1</rno>

        <name>Komal</name>

    </student>

    <student>

        <rno>2</rno>

        <name>Sandy</name>

    </student>

    <student>

        <rno>3</rno>

        <name>Ajay</name>

    </student>

</document>

 

======= handling.vm =========

#macro ( recursive $e $indent )    

    #if( $e.getChildren().size() > 0 )

        $indent <$e.getName()>

        #foreach ($child in $e.getChildren() )

            #recursive( $child "$indent  " )

        #end

        $indent </$e.getName()>

    #else

        $indent <$e.getName()>

        $indent    $e.getTextTrim()

        $indent </$e.getName()>

    #end

#end

 

#set($i = " ")

 

Handling.xml

 

#recursive( $root.getRootElement() $i )

 

Data directly :

 

Rno  : $root.getRootElement().getChild("student").getChild("rno").getText()

Name : $root.getRootElement().getChild("student").getChild("name").getText()

 

======= 출력 값 =========

Handling.xml

<document>

  <student>

    <rno>1</rno>

    <name> Komal   </name>

</student>

<student>  

  <rno>2 </rno>

  <name>Sandy</name>

</student>

<student>

  <rno>3</rno>

  <name>Ajay</name>

</student>

</document>

   

Data directly :

 

Rno  : 1

Name : Komal

 

반응형
Posted by 녹두장군1
,