差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| study:java:xml:create_xml [2009/04/13 05:50] – banana | study:java:xml:create_xml [2012/06/11 04:17] (現在) – [source snippet] banana | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| ====== Geneate xml with dom ====== | ====== Geneate xml with dom ====== | ||
| - | Dom level 3を用いたXML生成方法を紹介する。 | + | Dom level 3を用いたXML生成方法を紹介します。\\ |
| + | 記事を書く時点での最新の**[[http:// | ||
| + | その為、exampleを実行する為にはxercesImpl.jarとserializer.jarが必要となります。 | ||
| - | ====== Example | + | ===== Example ===== |
| - | 例として、次の簡単なxmlを作成してみよう。 | + | 例として、次の簡単なxmlを作成してみましょう。 |
| <code xml> | <code xml> | ||
| <?xml version=" | <?xml version=" | ||
| 行 15: | 行 17: | ||
| ===== source snippet ===== | ===== source snippet ===== | ||
| - | 以下にソースの一部分を示す。 | + | 以下にソースの一部分を示します。 |
| <code java> | <code java> | ||
| - | public static void generateXmlWithDom(OutputStream out) throws Exception { | + | import org.apache.xerces.dom.DOMImplementationImpl; |
| + | import org.w3c.dom.DOMConfiguration; | ||
| + | import org.w3c.dom.DOMImplementation; | ||
| + | import org.w3c.dom.Document; | ||
| + | import org.w3c.dom.DocumentType; | ||
| + | import org.w3c.dom.Element; | ||
| + | import org.w3c.dom.Node; | ||
| + | import org.w3c.dom.bootstrap.DOMImplementationRegistry; | ||
| + | import org.w3c.dom.ls.DOMImplementationLS; | ||
| + | import org.w3c.dom.ls.LSOutput; | ||
| + | import org.w3c.dom.ls.LSSerializer; | ||
| + | ..............省略..................... | ||
| + | public static void generateXmlWithDom(OutputStream out) throws Exception { | ||
| - | DOMImplementation impl = DOMImplementationImpl.getDOMImplementation(); | + | |
| - | //set DOCTYPE ( dtd ) | + | //set DOCTYPE ( dtd ) |
| - | DocumentType doctype = impl.createDocumentType(" | + | DocumentType doctype = impl.createDocumentType(" |
| - | Document xmldoc = impl.createDocument(null, | + | Document xmldoc = impl.createDocument(null, |
| - | // Root element. | + | // Root element. |
| - | // Root element. | + | Element root = xmldoc.createElement(" |
| - | | + | String[] id = {" |
| - | | + | String[] type = {" |
| - | | + | String[] desc = {" |
| - | | + | for (int i=0; |
| - | | + | { |
| - | | + | |
| - | // Child i. | + | e = xmldoc.createElementNS(null, |
| - | e = xmldoc.createElementNS(null, | + | e.setAttributeNS(null, |
| - | e.setAttributeNS(null, | + | e.setAttributeNS(null, |
| - | e.setAttributeNS(null, | + | n = xmldoc.createTextNode(desc[i]); |
| - | n = xmldoc.createTextNode(desc[i]); | + | e.appendChild(n); |
| - | e.appendChild(n); | + | root.appendChild(e); |
| - | root.appendChild(e); | + | } |
| - | | + | |
| - | xmldoc.appendChild(root); | + | xmldoc.appendChild(root); |
| - | // Xml serialization | + | // Xml serialization |
| - | DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); | + | DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); |
| - | DOMImplementationLS ls = (DOMImplementationLS) registry.getDOMImplementation(" | + | DOMImplementationLS ls = (DOMImplementationLS) registry.getDOMImplementation(" |
| - | LSSerializer writer = ls.createLSSerializer(); | + | LSSerializer writer = ls.createLSSerializer(); |
| - | LSOutput output = ls.createLSOutput(); | + | LSOutput output = ls.createLSOutput(); |
| - | output.setByteStream(out); | + | output.setByteStream(out); |
| - | output.setEncoding(" | + | output.setEncoding(" |
| - | // set indent | + | // set indent |
| - | DOMConfiguration config = writer.getDomConfig(); | + | DOMConfiguration config = writer.getDomConfig(); |
| - | config.setParameter(" | + | config.setParameter(" |
| - | writer.write(xmldoc, | + | writer.write(xmldoc, |
| - | out.close(); | + | out.close(); |
| - | }// generateXmlWithDom | + | } |
| </ | </ | ||
| + | **Xerces2.9.0**から**XMLserializer**がdeprecatedになっている為、**LSSerializer**(XML)または**JAXP Transformer API**(HTML, XHTML, | ||
| + | 元々、Xercesプロジェクトで含まれていたserializationコードをXalanプロジェクトで取って、開発を進めたせいで、同期が取れなくなったらしいです。\\ | ||
| + | Xalanのserializationコードがパフォーマンスが良く、バグの修正など長所があるため、徐々にXalanコードに移行することに決まったわけです。\\ | ||
| + | このことが2004年の出来事なので、以降はXalanのserializationコードが利用される見込みです。 | ||
| ===== reference ===== | ===== reference ===== | ||
| - [[http:// | - [[http:// | ||
| - [[http:// | - [[http:// | ||
| + | - [[http:// | ||