import java.io.*; import org.w3c.dom.*; import org.apache.xerces.dom.*; import org.apache.xml.serialize.*; /** * XMLファイルを生成するサンプルプログラムです。 * 実行には * * コンパイルおよび実行方法は下記のような感じになります。(Windowsの場合) * * set XERCES_PATH=C:\apache\bin\xerces-1_1_1 * javac -classpath .;%XERCES_PATH%\xerces.jar *.java * * pause * * java -classpath .;%XERCES_PATH%\xerces.jar Test */ public class Test { //public static final String ENCODE="SHIFT_JIS"; public static final String ENCODE="UNICODE"; public static final void main(String[] args) { try{ new Test().processMain(); }catch(IOException ex){ System.out.println(ex.toString()); ex.printStackTrace(); } } /** * 主処理 */ public void processMain() throws IOException { DocumentImpl document=new DocumentImpl(); Element elementRoot=document.createElement("よく見るウェブページ"); addLeaf(document,elementRoot,"What's New about Java","http://www.nk-exa.co.jp/~andoh/java/javanew.html","安藤さんのJavaのWhat's Newページ"); addLeaf(document,elementRoot,"日本の Linux 情報","http://www.linux.or.jp/","日本の Linux 情報の根っこのページ"); addLeaf(document,elementRoot,"Apache XML Project","http://xml.apache.org/","Apache XML Projectのページ"); addLeaf(document,elementRoot,"The Source for Java Technology","http://java.sun.com/","サン・マイクロシステムズのJavaのページ"); addLeaf(document,elementRoot,"中村正三郎のホットコーナー","http://www.asahi-net.or.jp/~ki4s-nkmr/","中村正三郎さんのウェブページ"); addLeaf(document,elementRoot,"みどりちゃんソフト のページ","http://www01.u-page.so-net.ne.jp/db3/midori/midosoft.html","みどりちゃんのページ"); document.appendChild(elementRoot); BufferedWriter writer=new BufferedWriter(new OutputStreamWriter( new FileOutputStream("Test.xml") ,ENCODE)); OutputFormat format=new OutputFormat(document); format.setDoctype(null,"Test.dtd"); format.setEncoding(ENCODE); XMLSerializer serial=new XMLSerializer(writer,format); serial.asDOMSerializer(); serial.serialize(document.getDocumentElement()); writer.flush(); writer.close(); } /** * 内部で利用する idカウンター */ private int g_iCounter=1; /** * エレメントを追加するルーチン */ public void addLeaf(Document document,Element elementRoot,String strTitle,String strAddress,String strComment) { Element elementURL=document.createElement("URL"); elementURL.setAttribute("id",""+(g_iCounter++)); Element elementTitle=document.createElement("題名"); elementTitle.appendChild(document.createTextNode(strTitle)); elementURL.appendChild(elementTitle); Element elementAddress=document.createElement("アドレス"); elementAddress.appendChild(document.createTextNode(strAddress)); elementURL.appendChild(elementAddress); Element elementComments=document.createElement("コメント"); elementComments.appendChild(document.createTextNode(strComment)); elementURL.appendChild(elementComments); elementRoot.appendChild(elementURL); } }