마크업(MarkUp)언어란?
마크업 언어는 "마크(Mark)"로 둘러싸인 언어로 "태크(Tag)"로 둘러싸였다고도 표현한다. HTML, XML 등의 마크업 언어들은 문서의 구조를 정의하고 쉽게 설명하면 문서의 골격에 해당하는 부분을 작성하는데 사용한다.
book
, employee
등등 이와 같은 태그들을 자유롭게 정의root
아래에서 여러 자식 태그가 포함server.xml
, Spring의 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
...
</root>
<name>King-Dong-Gun</name>
key = "value"
의 형식으로 작성<book title="XML Study" author="King-Dong-Gun" />
<name>King-Dong-Gun</name>
<!-- Hi Hello Bye GoodBye -->
<?xml version="1.0" encoding="UTF-8"?>
<library>
<!-- 루트 요소: 도서관 전체 -->
<book id="1">
<!-- 책 제목 -->
<title>XML Fundamentals</title>
<!-- 책 저자 -->
<author>John Doe</author>
<!-- 책 가격 및 통화 단위 -->
<price currency="USD">29.99</price>
</book>
<book id="2">
<!-- 두 번째 책 정보 -->
<title>Advanced XML</title> <!-- 책 제목 -->
<author>Jane Smith</author> <!-- 책 저자 -->
<price currency="EUR">35.00</price> <!-- 책 가격 및 통화 단위 -->
</book>
</library>
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
public class XMLParserExample {
public static void main(String[] args) {
try {
// XML 파일 로드
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("library.xml");
// 루트 요소 가져오기
Element root = doc.getDocumentElement();
System.out.println("Root Element: " + root.getNodeName());
// 모든 <book> 요소 가져오기
NodeList bookList = doc.getElementsByTagName("book");
for (int i = 0; i < bookList.getLength(); i++) {
Element book = (Element) bookList.item(i);
System.out.println("Book ID: " + book.getAttribute("id"));
System.out.println("Title: " + book.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Author: " + book.getElementsByTagName("author").item(0).getTextContent());
Element price = (Element) book.getElementsByTagName("price").item(0);
System.out.println("Price: " + price.getTextContent() + " " + price.getAttribute("currency"));
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Root Element: library
Book ID: 1
Title: XML Fundamentals
Author: John Doe
Price: 29.99 USD
Book ID: 2
Title: Advanced XML
Author: Jane Smith
Price: 35.00 EUR
요새 누가 XML로 데이터 전송하나요? JSON 이면 main() 함수 코드 절반넘게 줄일꺼같은데 창조 손해 보시네요 ㅋㅋ