본문 바로가기
XML

[본문스크랩] 3.4 DOM Level2

by 사우람 2010. 7. 12.

1.DOM Level2는 Level1에  네임스페이스 등에 대한 지원이 추가된 확장버전이다.

  Level1의 인터페이스나 메소드를 새로 정의 한것이 아니라 지원되지 않았던

  기능만 추가되었다.

 

  1) Document

    

      ① Element getElementByID(String elementId);

          DOM 구현에 ID 타입의 속성이 있을 경우 작동한다.

          이 속성은 DTD나 Schema에 ID가 정의 되어있어야 한다.

          속성이 정의 되어있지 않을 경우 null이 반환 된다.

 

   2) Node

 

       ① boolean isSupported(String feature, String version)

          

           - 메소드의 인자로 명시된 feature와 version이 지원되는 지 확인할 수 있다.

          

           - JAXP와 같은 파서에 중립적인 인터페이스를 사용하기 위해서는

              꼭 필요한 기능이다.

          

           - DOM을 구현한 파서라면 반듯이 구현해야 한다.

 

           - 파서가 런타임 도중에 로드되기 때문에 어떤 파서를 사용하게

             될지 모르기 때문에 이 매소드는 중요성을 가진다.

 

       ② boolean hasAttributes()

           노드에 속성이 있는지 알려주는 메소드 이다.

 

       ③ normalize()

           문서를 DOM View로 보는 형태가 원래 저장 매체에 저장되어 있던 때의 모습과

           같은지 확인할때 사용한다.

           엘리먼트들 사이의 공백을 제거해 주고, 비어있는 엘리먼트는 빈 태그로 바꿔준다.

 

           <Document>

                <p>Some text</p>

                <Date></Date>

           <Document>

           ==>

            <Document><p>Some text</p></Date></Document>

 

     3) Trabersal

         

         1) 트리노드를 탐색,처리하는 부분을 간소화 한다.

 

         2) TreeWalker

             문서의 위계성을 인정하여 부모와 자식을 탐색이 가능하다.

 

        3) NodeIterator

            위례성을 인정하지 않으며 평면적으로 위에서 아래로 쭉 읽어드리며

            앞뒤로 이동이 가능하다.

2. 네임스페이스 사용하기

    <?xml version="1.0" encoding="UTF-8"?>
    <DOMExample>
        <book xmlns="http://www.wrox.com/cppk-title">
            <title price="$59">Professional Java XML</title>
            <chapter title="The DOM">
            <author title="Mr." name="John Davies"/>
            </chapter>
        </book>
        <order xmlns:html="http://www.c24solutions.com">
            <name html:class="h1"></name>
            <payment type="credit" html:class="H3">Paid</payment>
            <html:a href="/jsp/prebookings?order-ref=0527658">Check order</html:a>
            <data location="London" html:class="H3">2001-07-21</data>
        </order>
    </DOMExample>

 

     1) payment , data 어느 네임 스페이스에도 속해 있지 않은 적법하지 않은

         엘리먼트 이름이다.

 

package dom_level2;

import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class NamespaceDemo {
 
 public NamespaceDemo(String filename){
  
  Document doc = null;
  
  try{
   DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
   dbf.setNamespaceAware(true);
   DocumentBuilder db = dbf.newDocumentBuilder();
   doc = db.parse(filename);
   
   String wrox = "http://www.wrox.com/book-titles";
   String local = "a";
   String c24 = "http://c24soutions.com";
   
   System.out.println("Element in the "+wrox+" namspace");
   
   NodeList n1 = doc.getElementsByTagNameNS(wrox,"*");
   
   for(int i=0; i<n1.getLength(); i++){
    Node n = n1.item(i);
    System.out.println(n.getNodeName());
   }
   
   System.out.println("\nElements with a local name of "+local+"...");
   
   n1 = doc.getElementsByTagNameNS("*",local);
   
   for(int i=0; i<n1.getLength(); i++){
    Node n = n1.item(i);
    System.out.println(n.getNodeName());
   }
   
   System.out.println("\nAttributes in the "+c24+" namespace...");
   
   n1 = doc.getElementsByTagName("*");
   
   for(int i=0; i<n1.getLength(); i++){
    if(n1.item(i) instanceof Element){
     Text t = (Text)n1.item(i).getFirstChild();
     Element e = (Element)n1.item(i);
     Attr a =e.getAttributeNodeNS(c24,"class");
     if(a != null){
      String val = a.getNodeValue();
      System.out.println(val);
     }
    }
   }
  }catch(Exception e){
   e.printStackTrace();
  }
  
 }
 
 public static void main(String[] args){
  new NamespaceDemo("dom_level2\\dom_level2.xml");
 }
}

 

1) 팩토리에서 객체를 얻어올때 네임스페이스를 사용한다고

    설정한후 네임스페스에 관한 매소드를 사용한다.