출처 : http://miriam.tistory.com/209
<?xml version="1.0" encoding="euc-kr"?>
<!DOCTYPE Computer [
<!ELEMENT Computer (CPU, Memory, GraphicCard)>
<!ELEMENT CPU (#PCDATA)>
<!ELEMENT Memory (#PCDATA)>
<!ELEMENT GraphicCard (#PCDATA)>
]>
XML 문서의 DTD 선언문(Document Type Declaration)
=> XML의 구조가 DTD에 의해 정의되어 있다는 것을 명시하기 위한 구문
1) <!DOCTYPE 문서타입이름 SYSTEM "DTD 파일이름">
2) <!DOCTYPE 문서타입이름 [ DTD 정의 ]>
3) 엘리먼트 선언 방법
<!ELEMENT 엘리먼트 이름 엘리먼트 내용>
4) 하위 엘리먼트를 갖는 엘리먼트 선언
<!ELEMENT Book (Title, Author, PubDate)>
5) 값을 갖는 엘리먼트 선언
<!ELEMENT Title (#PCDATA)>
6) 빈 엘리먼트 선언
<!ELEMENT PubDate EMPTY>
<!DOCTYPE BookList [
<!ELEMENT BookList (Book)>
<!ELEMENT Book (Title, Author, PubDate)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT PubDate (#PCDATA)>
]>
엘리먼트의 연결자
=> , : and 의 의미로 엘리먼트가 순차적으로 발생함 <!ELEMENT A (B, C)>
| : or 의 의미로 엘리먼트가 선택적으로 발생함 <!ELEMENT A (B | C)>
<xml version="1.0" encoding="euc-kr">
<!DOCTYPE BookList [
<!ELEMENT BookList (Book)>
<!ELEMENT Book (Title, Author, (PubDate | Author))>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT PubDate EMPTY>
<!ELEMENT Date EMPTY>
엘리먼트의 발생 지시자
=> <!ELEMENT BookList (Book)> : 반드시 한번 사용된다
<!ELEMENT BookList (Book?)> : 사용되지 않거나 한 번 사용된다
<!ELEMENT BookList (Book+)> : 한번 이상 사용된다
<!ELEMENT BookList (Book*)> : 사용되지 않거나 여러 번 사용된다
<!DOCTYPE BookList [
<!ELEMENT BookList (Book+)>
<!ELEMENT Book (Title, Author*, (PubDate | Date)?)>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT PubDate EMPTY>
<!ELEMENT Date EMPTY>
]>
어트리뷰트의 형
=> CDATA : 일반적인 값을 갖는 어트리뷰트
<!ATTLIST Date year CDATA #REQUIRED>
=> 열거형 : 열거된 값 중 하나를 선택적으로 갖는 어트리뷰트
<!ATTLIST Price unit (원 | 달러 | 엔) #REQUIRED>
=> ID 형 : XML 문서 내에 유일한 값을 갖는 어트리뷰트 선언
<?xml version="1.0" encoding="euc-kr"?>
<!DOCTYPE BookList [
<!ELEMENT BookList (Book+)>
<!ELEMENT Book (Title, Author*, Price, (PubDate | Date)?)>
<!ATTLIST Book ISBN ID #REQUIRED>
<!ELEMENT Title (#PCDATA)>
<!ELEMENT Author (#PCDATA)>
<!ELEMENT Price (#PCDATA)>
<!ATTLIST Price unit (원 | 달러 | 엔) #REQUIRED>
<!ELEMENT PubDate EMPTY>
<!ATTLIST PubDate year CDATA #REQUIRED month CDATA #REQUIRED>
<!ELEMENT Date EMPTY>
]>
어트리뷰트의 디폴트 값
=> #REQUIRED : 반드시 사용되어야 하는 어트리뷰트 선언
<!ATTLIST Book ISBN ID #REQUIRED>
=> #IMPLIED : 어트리뷰트가 사용될 수도 있고, 사용되지 않을 수도 있다
<!ATTLIST Date year CDATA #IMPLIED>
=> #FIXED "어트리뷰트 값" : 어트리뷰트가 사용될 경우에는 반드시 선언된
값으로 사용해야 함
<!ATTLIST Price unit CDATA #FIXED "won">
******* 54 Page 참고 ********
<?xml version="1.0"?>
<!DOCTYPE Person [
<!ELEMENT Person (#PCDATA)>
<!ENTITY name "이명진">
]>
<Person> &name </Person>
<?xml version="1.0"?>
<!DOCTYPE Person [
<!ELEMENT Person (#PCDATA)>
<!ENTITY name SYSTEM name.txt>
]>
<Person> &name </Person>
<?xml version="1.0" encoding="euc-kr"?>
<!DOCTYPE BookList [
<!ELEMENT BookList (Book+)>
<!ELEMENT Book (#PCDATA)>
<!ENTITY name "XML 프로그래밍">
<!ENTITY bookContent SYSTEM "book.xml">
]>
<?xml version="1.0" encoding="euc-kr"?>
<com:Computer xmlns:com="http://www.computer.com">
<com:CPU>Pentium 2.0</com:CPU>
<com:Memory>512MB</com:Memory>
<com:GraphicCard>Radeon 9200</com:GraphicCard>
</com:Computer>
<lan:name xmlns:lan="http://www.sun.com">Java</lan:name>
<food:name xmlns:food="http://www.food.net">Java</food:nae>
<Computer xmlns="http://www.computer.com">
<!ELEMENT title (#PCDATA)> :: <xsd:element name="title" type="xsd:string" />
<!ELEMENT number (#PCDATA)> :: <xsd:element name="number" type="xsd:integer" />
<!ELEMENT date (#PCDATA)> :: <xsd:element name="title" type="xsd:date" />
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsd:schema xmlns:xsd="http//www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:element name="car">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="CC" type="xsd:integer" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<?xml version="1.0" encoding="UTF-8" standalone="yes?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualifed">
<xsd:element name="car">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="CC" type="xsd:integer" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<!ELEMENT book (name, author?)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT authors (#PCDATA)>
<xsd:element name="book">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="authors" type="xsd:string" minOccurs="0" maxOccurs="1">
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="booklist">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="author" type="xsd:string" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="page" type="xsd:integer" />
<xsd:element name="price" type="xsd:integer" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
기본적으로 제공되는 데이터 타입
=> string / boolean / decimal / time / date / duration
integer / ID
Complex Type - <xsd:complexType>
=> <xsd:element> 엘리먼트의 하위 요소
엘리먼트가 하위 엘리먼트나 어트리뷰트를 갖을 경우에 사용
<element name="page">
<simpleType>
<restriction base="xsd:string">
<minInclusive value="100" />
<maxInclusive value="200" />
</restriction>
</simpleType>
</element>
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="booklist">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="book" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="part">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="XML" />
<xsd:enumeration value="XSLT" />
<xsd:enumeration value="Schema" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="name" type="xsd:string" />
<xsd:element name="page">
<xsd:simpleType>
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1" />
<xsd:maxInclusive value="1500" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Contents Model : 엘리먼트가 갖는 하위 엘리먼트들의 발생 관계를 나타낸다.
=> <xsd:sequence> : 각 엘리먼트들이 순서대로 발생
<xsd:all> : 순서에 상관없이 한 번씩 발생
<xsd:choice>
********* 88 / 89 페이지 ************
SAX의 7가지 이벤트 핸들러
=> ContentHandler / ErrorHandler / DTDHandler / EntityResolver
DeclHandler / LexicalHandler / DefaultHandler
class ImplHandler implements ContentHandler {
void startDocument() {
System.out.println("XML Document Start");
}
void endDocument() {
System.out.println("XML Document End");
}
}
ErrorHandler : XML 문서 처리 도중 오류 발생시 호출되는 이벤트
=> fatalError(SAXParseException exception)
error(SAXParseException exception)
warning(SAXParseException exception)
ImplHandler errorHandler = new ImplHandler();
xmlReader.setErrorHandler(errorHandler);
SAX 파서 사용 예제
=> SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(filename, new DefaultHandler());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = new factory.newDocumentBuilder();
Document document = builder.parse(filename);
/**
DOM 파서를 생성하고, XML 문서를 파싱하거나 새로운 XML 문서를 생성하는 예제
**/
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class CreateDOMParser {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = factory.newDocumentBuilder();
// 빈 Document 객체 생성
Document emptyDocument = dBuilder.newDocument();
System.out.println("빈 Document 객체 생성 완료");
// XML 파일을 로딩하여 Document 객체 생성
Document xmlDocument = dBuilder.parse("DOMSample.xml");
System.out.println("DOMSample.xml 문서를 입력으로 Document 객체 생성 완료");
} catch(Exception e) {
e.printStackTrace(System.err);
}
}
}