XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf ·...

43
XML DOM (Document Object Model) 2017. 9. 27

Transcript of XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf ·...

Page 1: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

XML DOM

(Document Object Model)

2017. 9. 27

Page 2: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Content

XML 문서처리

DOM 개요

DOM Tree

DOM Core Interfaces

– Node

– Document

– Element

– Attr

DOM 사용예

1

Page 3: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

XML 문서처리

XML 문서처리과정

XML Parser2

Page 4: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

XML 문서처리

XML Parsing

– XML 문서를읽고해석하여구성요소, 즉, element name, content,

attribute name, attribute value 등을식별하고그것들간의문법적인

관계를파악

– 응용프로그램이이용할수있도록메모리상에데이터구조생성

DOM(Document Object Model)

– XML 문서를 parsing한후메모리에객체들의트리구조를생성

XML 문서에대한메모리내표현

– 생성된트리를이용하여데이터검색, 추가, 수정, 삭제가능

– XML 문서의크기가크거나많은 XML 문서들을동시에처리하는경우

메모리공간을많이사용3

Page 5: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

What is the DOM?

– XML이나 HTML 문서를접근하고조작하기위한표준방법을정의

"W3C 문서객체모델(DOM; Document Object Model)은

프로그램과스크립트가동적으로문서의내용, 구조, 형식에

접근하고수정할수있게해주는플랫폼중립적이고언어

중립적인인터페이스 (The W3C Document Object Model (DOM)

is a platform and language-neutral interface that allows programs

and scripts to dynamically access and update the content,

structure, and style of a document.)" -- W3C DOM

– 문서의구성요소(element)들에대한객체(object)와속성(property)을

정의하고, 그것들을접근하는수단(interface)을정의함

문서의구성요소들을접근하고조작하기위해사용할수있는

표준적인응용프로그래밍인터페이스(API) 정의

4

Page 6: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

DOM 표준의종류

– Core DOM: 임의의문서에대한표준모델을정의

– HTML DOM: HTML 문서에대한트리구조와인터페이스정의

– XML DOM: XML 문서에대한트리구조와인터페이스정의

XML 문서를하나의트리로표현

DOM기반 parser에의해트리구조가생성됨

인터페이스를이용하여트리(문서)에속한엘리먼트와속성,

텍스트들을검색, 생성, 수정, 삭제가능

5

Page 7: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

6

Page 8: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

DOM을이용한 XML 문서의처리과정

① ② ④

① XML 문서를읽어들임② XML 문서를해석하여객체트리구조를생성③ DOM API를사용해서 XML 문서의구성요소들을접근및변경④변경된문서구조를새로운 XML파일로저장 7

Page 9: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

예 1: XML 문서에서첫번째 <title> element의값을찾아출력

8

Page 10: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 개요

예 2: HTML 문서에서 id가 "demo"인 element의값을변경

9

Page 11: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Tree

10

Page 12: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Tree

DOM 트리는 Node 타입의객체들로구성됨

– XML 문서와그것에포함된모든요소들에대해노드객체생성

– 노드의종류 (하위타입)

문서노드(Document node): 전체 XML 문서를나타냄

엘리먼트노드(Element node): XML 엘리먼트를나타냄

텍스트노드(Text node): XML 엘리먼트내의텍스트를나타냄.

속성노드(Attribute node): XML 엘리먼트내의속성을나타냄.

주석노드(Comment node): XML 문서내의주석을나타냄.

11

Page 13: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Tree

노드들간의관계

– 부모-자식관계, 형제관계

– 최상위노드를루트(root)라함

– 루트를제외한모든노드는하나의

부모(parent) 노드를가짐

– 노드는하나이상의자식(child) 노드들을

가질수있음

모든자식노드집합, 첫번째자식노드,

마지막자식노드를조회가능

– 같은부모노드를갖는노드들을형제(sibling)

노드라함

다음형제, 이전형제노드를접근가능

12

Page 14: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Core Interfaces

DOM 트리에포함된노드들을접근하기위한인터페이스집합을

정의

– 최상위인터페이스: Node

– Node의하위인터페이스: Element, Document, Attribute, Text 등

인터페이스종류

– 기본인터페이스 (Fundamental interfaces)

Node, Element, Attribute, Document, DocumentFragment, Text,

Comment

– 확장인터페이스 (Extended interfaces)

DocumentType, Notation, Entity, EntityReference,

ProcessigInstruction, CDATASection

13

Page 15: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Core Interfaces

DOM Interfaces 구성도

14

Page 16: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Core Interfaces

기본인터페이스(Fundamental Interfaces)

종류 용도

Node모든인터페이스의 부모인터페이스로, 다른인터페이스가 공통적으로갖는 method들이정의되어있음

DocumentDOM 트리의최상위노드의타입으로, XML 문서자체에해당됨. 동적으로다른노드를생성하는 method들을포함

Element XML 문서내의엘리먼트에해당되는인터페이스

Attr XML 문서내의속성에해당되는인터페이스

CharacterData XML 문서내의문자데이터에해당되는인터페이스

Text XML 엘리먼트의 텍스트콘텐트에해당되는인터페이스

NodeList 노드들의집합을유지하고관리할수있도록설계된인터페이스

DOMImplementation Document 객체를생성하는 method가정의되어있는인터페이스

DocumentFragment DOM 트리에서서브트리를분리할때사용되는인터페이스

15

Page 17: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM Core Interfaces

확장인터페이스(Extended Interfaces)

종류 용도

CDATASection XML 문서에서 CDATA 섹션에해당되는인터페이스

DocumentType XML 문서에서 DTD 선언에해당하는인터페이스

Notation DTD에서 Notation 선언에해당하는인터페이스

Entity DTD에서 Entity 선언에해당하는인터페이스

EntityReference XML 문서또는 DTD에서 Entity 참조에해당하는인터페이스

ProcessingInstruction XML 문서에서프로세싱지시자에해당하는인터페이스

16

Page 18: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

Node 인터페이스

– 기능

노드에대한정보를얻음

DOM 트리에노드를추가, 삭제, 변경가능

DOM 트리의노드들을순회(traversal) 할수있음

17

Page 19: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

속성(property)

속성이름 속성타입 설명

nodeName String 노드이름

nodeValue Text 노드값

nodeType Number 노드타입. 0부터시작하는정수로표현

parentNode Node 노드의부모노드

childNodes NodeList 노드의자식노드

fisrtChild Node 첫번째자식노드

lastChild Node 마지막자식노드

previousSibling Node 노드의형제노드중바로앞노드

nextSibling Node 노드의형제노드중바로다음노드

attributes NameNodeMap 노드속성

ownerDocument Document 노드를포함하는문서

namespaceURI String 노드의 namespace URI

prefix String 노드의 namespace prefix 18

Page 20: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

– 하위인터페이스(노드)에서의 nodeName, nodeValue, attributes의값

하위인터페이스 nodeName nodeValue attributes

Attr 속성의이름 속성의값 null

CDATASection #cdata-section CDATA Section의내용 null

Comment #comment 주석내용 null

Document #document null null

DocumentFragment #document-fragment null null

DocumentType 문서타입이름 null null

Element 태그이름 null NamedNodeMap

Entity 개체이름 null null

EntityReferencce 개체참조이름 null null

Notation notation이름 null null

ProcessingInstruction target target을제외한내용 null

Text #text 텍스트노드의내용 null19

Page 21: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

메소드(methods)

– 노드에관한정보를조회

method 기능

String getNodeName() 노드이름을반환

String getNodeValue() 노드값을반환

void setNodeValue(String nodeValue) 노드값을설정

short getNodeType() 노드타입을반환

NamedNodeMap getAttributes() 속성을반환

String getTextContent() 노드와그자손들의 text

content를반환

Document getOwnerDocument() 현재노드가속한문서를반환

20

Page 22: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

– 문서(DOM tree)조작: 새로운노드를추가하거나삭제, 복사함

method 기능

Node insertBefore(Node newChild,

Node refChild)

refChild노드앞에 newChild노드를삽입

refChild노드가 null이면 newChild노드는맨

마지막자식노드로삽입

Node replaceChild(Node newChild,

Node oldChild)

oldChild노드를 newChild노드로대체

Node removeChild(Node oldChild) 트리에서 oldChild 자식노드를제거한후그노드

객체를반환

Node appendChild(Node newChild) 트리에노드를추가. 추가하려는노드위치에다른

형제노드가있으면마지막에노드를추가함

Node cloneNode(boolean deep) 노드의복사본을만드는 method. deep 값이

true이면 deep copy, false면 shallow copy 수행

21

Page 23: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

– 부모, 자식, 형제노드를조회

method 기능

Node getParentNode() 부모노드를반환

NodeList getChildNodes() 자식노드를 NodeList타입으로반환

Node getFirstChild() 첫번째자식노드를반환

Node getLastChild() 마지막자식노드를반환

Node getPreviousSibling() 현재노드의바로앞에있는형제노드를반환

Node getNextSibling() 현재노드의바로뒤에있는형제노드를반환

boolean hasChildNodes() 단순히노드가자식을가지고있는지없는지

확인하는 method로, 자식의유무를 boolean

값으로반환. 자식이 Text node라도 true를

반환함

22

Page 24: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

– 자식노드들을순서대로처리하는방법

for (Node child = node.getFirstChild(); child != null;

child = child.getNextSibling()) {

process child …

}

NodeList childNodes = node.getChildNodes();

for (int i = 0; i < childNodes.getLength(); i++) {

Node child = childNodes.item(i);

process child …

}

또는

23

Page 25: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Node Interface

– 트리탐색 (Tree traversal)

재귀함수정의및호출

"pre-order depth-first traversal"

Public void processNode(Node node) {

process the current node …

for (Node child = node.getFirstChild(); child != null;

child = child.getNextSibling()) {

processNode(child); // process child

}

}

24

Page 26: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Document Interface

Document 인터페이스

– 문서전체에대한접근을제어하는인터페이스

– Node 인터페이스에서확장된인터페이스

Node 인터페이스의 method와속성을물려받아사용가능

– 가능한작업

노드트리검색및노드반환

새로운노드나속성을생성

25

Page 27: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Document Interface

– 문서트리내의엘리먼트를검색하기위한 methods

method 기능

Element getDocumentElement() 문서의 Root Element를반환

Element getElementByID

(String elementID)

문서에 elementID를 ID로가진엘리먼트를

찾아반환함.해당되는엘리먼트가없으면

null을반환

NodeList getElementsByTagName

(String tagName)

문서에 tagName이름을가진

엘리먼트들을모두찾아 NodeList객체에

저장하여반환함

26

Page 28: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Document Interface

– 새로운노드를생성하는 methods

method 기능

Element createElement(String

tagName)

tagName을이름으로갖는 Element

노드를생성

Attr createAttribute(String name) 지정된 name의속성노드를생성

Text createTextNode(String data) data의텍스트가있는 Text 노드를생성

Comment createComment(String data) data의텍스트가있는 Comment

노드를생성

DocumentFragment

createDocumentFragment()

비어있는 DocumentFragment객체를

생성

Node importNode(Node importNode,

boolean deep)

다른문서에서이문서로 importNode

노드를가져옴. 원래의노드는이전문서에서제거되지않고복사됨.

27

Page 29: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Element Interface

Element 인터페이스

– XML 문서에포함된하나의엘리먼트를표현하고접근하기위한

인터페이스

– 유일한속성은 tagName속성

엘리먼트의태그이름을반환하며, 읽기전용임

– Methods

엘리먼트의태그및속성에접근하기위한 methods

28

Page 30: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Element Interface

Methods

method 기능

String getTagName() 태그이름을반환

NodeList getElementsByTagName(

String name)

name의태그이름을갖는모든자손

노드들의 NodeList를반환 (문서순서대로)

boolean hasAttribute(String name) 주어진이름의속성이있는경우, true를

반환

String getAttribute(String name) 지정된 name 속성값을반환.

속성에지정된값이없거나, 기본값을

가지고있지않으면빈문자열을반환

Attr setAttribute(String name, String

value)

지정된 name을갖는속성값을 value

값으로변경. 해당속성이존재하지

않으면새로운속성을추가29

Page 31: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Element Interface

Methods

method 기능

void removeAttribute(String name) 지정된 name을갖는속성을제거.

기본값이정의된속성인경우새로운

속성으로대체

Attr getAttributeNode(String name) 지정된 name의속성을담고있는 Attr노드를

반환. 해당노드가없으면 null을반환

Attr setAttributeNode(Attr newAttr) 현재엘리먼트에새로운 Attr노드를추가.

같은이름의속성이이미존재하면대체함.

Attr이대체되면대체된값이반환되고,

그렇지않으면 null을반환

Attr removeAttributeNode(Attr

oldAttr)

지정된 Attr노드를제거하고반환.

기본값이정의된속성인경우새로운 Attr

속성으로대체 30

Page 32: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Element Interface

Example

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

// DOM 파서생성…

// XML 문서파싱하기…

// 루트엘리먼트참조얻기…

// 첫번째 book 엘리먼트의 kind속성값 변경Element eBook = (Element) eRoot.getFirstChild();

String strKind = fBook.getAttribute(“category");

fBook.setAttribute(“category", "computer");

// == eBook.getAttributeNode("kind").setNodeValue("computer“);

System.out.println(eBook.getAttribute("kind");

// 첫번째 book 엘리먼트에 새로운속성추가eBook.setAttribute("publishDate", “20130501");

System.out.println(eBook.getAttribute("publishDate");

}

31

Page 33: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Attr Interface

Attr인터페이스

– 엘리먼트에속한속성에대한인터페이스

– Node 인터페이스를확장한인터페이스

– 속성은 XML 트리구조의일부가아님

속성은관련된엘리먼트와자식및부모관계가아님

Node의속성인 parentNode, previousSibling, nextSibling은 null

값을갖음

– Attr의속성

name: 속성의이름을반환하는읽기전용속성

value: 속성의값을나타내는속성

ownerElement: 이속성을포함하는엘리먼트노드를가리킴

32

Page 34: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

Attr Interface

Methods

method 기능

String getName() 속성이름을반환

String getValue() 속성값을반환

void setValue(String value) 속성값을설정

Element getOwnerElement() 해당속성이속한 Element 노드를반환

33

Page 35: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 1

34

book.xml 문서에서<title> 엘리먼트들을모두찾아그것의자식노드인텍스트노드의값을출력

Page 36: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 2

35

book.xml 문서에서첫번째 <book>

엘리먼트의모든자식노드들의정보를출력

제목: Everyday Italian

저자: …

연도: …

가격: …

Page 37: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 3

36

book.xml 문서에서첫번째<book> 엘리먼트에대해새로운 <edition>

자식노드와그텍스트값을생성하여추가

<book category=“cooking” >

<title>…</title>

<author>…</author>

<year>…</year>

<price>…</price>

<edition>first</edition>

</book>

Page 38: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 4

37

book.xml 문서에서첫번째 <book>

엘리먼트에대해 <year>자식노드를찾아삭제

<book category=“cooking” >

<title>…</title>

<author>…</author>

<year>…</year> (삭제)

<price>…</price>

</book>

Page 39: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 5

38

book.xml 문서에서첫번째 <book>

엘리먼트를찾아 category 속성을삭제하고publishDate속성을값과함께추가

<book category="cooking”

publishDate="20160501">

<title>…</title>

<author>…</author>

<year>…</year>

<price>…</price>

</book>

Page 40: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

DOM 사용예 5

39

book.xml 문서에서첫번째 <book>

엘리먼트를찾아 category 속성을삭제하고publishDate속성을값과함께추가

<book category="cooking”

publishDate="20160501">

<title>…</title>

<author>…</author>

<year>…</year>

<price>…</price>

</book>

Page 41: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

HTML DOM

HTML 문서의구성요소들을하나의 DOM 트리로표현

– HTML 문서내의모든엘리먼트와속성들을접근및수정가능

40

Page 42: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

HTML DOM

41

link 주소설정

input 값설정

Page 43: XML DOM (Document Object Model) - SUWONvrlab.suwon.ac.kr/mwlee/data2/file/XML DOM (20170927).pdf · 2017-09-27 · XML 문서처리 XML Parsing – XML 문서를읽고해석하여구성요소,

References

W3C Document Object Model

– http://www.w3.org/DOM/

XML DOM Tutorial

– http://www.w3schools.com/xml/dom_intro.asp

HTML DOM Tutorial

– http://www.w3schools.com/js/js_htmldom.asp

Books

– 신민철, Java 개발자를위한 XML, 프리렉, 2006

– B. D. McLaughlin & J. Edelson, Java and XML, 3rd Ed., O'Reilly,

2006

– 손진현, 김명호, XML 개념및응용, 홍릉과학출판사, 2008

42