JDOM 을 이용하여 읽어올 XML 파일(catalog.xml)
JDOM Library (http://www.jdom.org)
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<category>
Small chamber ensembles - 2-4 Players by New York Women Composers
</category>
<cataloging_info>
<abstract>Compositions by the members of New York Women Composers</abstract>
<keyword>music publishing</keyword>
<keyword>scores</keyword>
<keyword>women composers</keyword>
<keyword>New York</keyword>
</cataloging_info>
<last_updated>July 28, 1999</last_updated>
<copyright>1999 New York Women Composers</copyright>
<maintainer email="elharo@metalab.unc.edu"
url="http://www.macfaq.com/personal.html">
<name>
<first_name>Elliotte</first_name>
<middle_name>Rusty</middle_name>
<last_name>Harold</last_name>
</name>
</maintainer>
<composer id="c1">
<name>
<first_name>Julie</first_name>
<middle_name><![CDATA[CDATA String]]></middle_name>
<last_name>Mandel</last_name>
</name>
</composer>
<composer id="c2">
<name>
<first_name>Margaret</first_name>
<middle_name>De</middle_name>
<last_name>Wys</last_name>
</name>
</composer>
<composer id="c3">
<name>
<first_name>Beth</first_name>
<middle_name></middle_name>
<last_name>Anderson</last_name>
</name>
</composer>
<composer id="c4">
<name>
<first_name>Linda</first_name>
<middle_name></middle_name>
<last_name>Bouchard</last_name>
</name>
</composer>
<composition composer="c1">
<title>Trio for Flute, Viola and Harp</title>
<date><year>(1994)</year></date>
<length>13'38"</length>
<instruments>fl, hp, vla</instruments>
<description>
<p>Premiered at Queens College in April, 1996 by Sue Ann Kahn,
Christine Ims, and Susan Jolles. In 3 movements :</p>
<ul>
<li>mvt. 1: 5:01</li>
<li>mvt. 2: 4:11</li>
<li>mvt. 3: 4:26</li>
</ul>
</description>
<publisher>Theodore Presser</publisher>
</composition>
<composition composer="c2">
<title>Charmonium</title>
<date><year>(1991)</year></date>
<length>9'</length>
<instruments>2 vln, vla, vc</instruments>
<description>
Commissioned as quartet for the Meridian String Quartet.
Sonorous, bold. Moderate difficulty. Tape available.
</description>
<publisher></publisher>
</composition>
<composition composer="c1">
<title>Invention for Flute and Piano</title>
<date><year>(1994)</year></date>
<length></length>
<instruments>fl, pn</instruments>
<description>3 movements</description>
<publisher></publisher>
</composition>
<composition composer="c3">
<title>Little Trio</title>
<date><year>(1984)</year></date>
<length>4'</length>
<instruments>fl, guit, va</instruments>
<description></description>
<publisher>ACA</publisher>
</composition>
<composition composer="c3">
<title>Dr. Blood's Mermaid Lullaby</title>
<date><year>(1980)</year></date>
<length>3'</length>
<instruments>fl or ob, or vn, or vc, pn</instruments>
<description></description>
<publisher>ACA</publisher>
</composition>
<composition composer="c3">
<title>Trio: Dream in D</title>
<date><year>(1980)</year></date>
<length>10'</length>
<instruments>fl, pn, vc, or vn, pn, vc</instruments>
<description>
Rhapsodic. Passionate. Available on CD
<cite><a href="http://www.amazon.com/exec/obidos/ASIN/B000007NMH/qid%3D913265342/sr%3D1-2/">Two by Three</a></cite>
from North/South Consonance (1998).
</description>
<publisher></publisher>
</composition>
<composition composer="c4">
<title>Propos II</title>
<date><year>(1985)</year></date>
<length>11'</length>
<instruments>2 tpt</instruments>
<description>Arrangement from Propos</description>
<publisher></publisher>
</composition>
<composition composer="c4">
<title>Rictus En Mirroir</title>
<date><year>(1985)</year></date>
<length>14'</length>
<instruments>fl, ob, hpschd, vc</instruments>
<description></description>
<publisher></publisher>
</composition>
</catalog>
JDOM 을 이용하여 XML문서의 특정 요소를 찾아서 출력하는 예제
import java.io.*;
import org.jdom.*;
import org.jdom.filter.ElementFilter;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
public class JDOMTest {
public static void main(String[] args) throws Exception {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build("catalog.xml");
//특정 요소(Element)만 가져올 경우
Iterator itr = doc.getDescendants(new ElementFilter("composer"));
while (itr.hasNext()) {
Content c = (Content) itr.next();
//System.out.println(c.getValue());
if(c instanceof Element){
//System.out.println("요소(Element)발견");
Element e = (Element)c;
Attribute at = e.getAttribute("id");
if(at!=null && at.getValue().equals("c1")){ // id가 c1인 Element를 찾는다
System.out.println("ID c1 인 요소 발견");
// 발견된 composer 요소의 자식요소들 중에서 middle_name요소만 선택한다
Iterator itr2 = e.getDescendants(new ElementFilter("middle_name"));
// 선택된 "middle_name"을 출력한다
while(itr2.hasNext()){
Element e2 = (Element)itr2.next();
System.out.println(e2.getValue());
}
}
}
}
//XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
//outputter.setExpandEmptyElements(true);
//outputter.output(doc, System.out);
}
}
다른 웹서버에 저장되어 있는 XML 문서를 읽고 원하는 특정 요소의 값을 출력하는 예제
http://micropilot.tistory.com/attachment/cfile2.uf@113941284A1659CB9A1806.xml
위의 경로에 처리하고자 하는 XML문서가 저장되어 있다고 가정한다(실제로 존재함)
import java.io.*;
import org.jdom.*;
import org.jdom.filter.ElementFilter;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
import java.net.*;
/* 다른 웹서버에 저장되어 있는 XML문서를 읽어서 특정 요소의 값을 출력하는 예제 */
public class JDOMTest {
public static void main(String[] args) throws Exception {
// 네트워크를 연결하는 부분
URL url = new URL("http://micropilot.tistory.com/attachment/cfile2.uf@113941284A1659CB9A1806.xml");
URLConnection uc = url.openConnection();
InputStream is = uc.getInputStream();
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(is);
// 나머지 소스는 위의 예제와 동일함
//특정 요소(Element)만 가져올 경우
Iterator itr = doc.getDescendants(new ElementFilter("composer"));
while (itr.hasNext()) {
Content c = (Content) itr.next();
//System.out.println(c.getValue());
if(c instanceof Element){
//System.out.println("요소(Element)발견");
Element e = (Element)c;
Attribute at = e.getAttribute("id");
if(at!=null && at.getValue().equals("c1")){ // id가 c1인 Element를 찾는다
System.out.println("ID c1 인 요소 발견");
// 발견된 composer 요소의 자식요소들 중에서 middle_name요소만 선택한다
Iterator itr2 = e.getDescendants(new ElementFilter("middle_name"));
// 선택된 "middle_name"을 출력한다
while(itr2.hasNext()){
Element e2 = (Element)itr2.next();
System.out.println(e2.getValue());
}
}
}
}
//XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
//outputter.setExpandEmptyElements(true);
//outputter.output(doc, System.out);
}
}