Top
- try {
- // DOMパーサの生成
- DOMParser parser = new DOMParser();
- // XML文書の取得
- parser.parse(ファイル名);
- // Documentインスタンスの取得
- Document doc = parser.getDocument();
- // ここにきたときには、整形式 XML 文書である。
- } catch (SAXParseException e) {
- ...
- } catch (Exception e) {
- ...
- }
□ XML 文書("emp.xml")
- import org.apache.xerces.parsers.DOMParser;
- import org.w3c.dom.Document;
- import org.xml.sax.SAXParseException;
- public class DomParser01 {
- public static void main(String[] args) {
- try {
- // DOMパーサの生成
- DOMParser parser = new DOMParser();
- // XML文書の取得
- parser.parse(args[0]);
- // Documentインスタンスの取得
- Document doc = parser.getDocument();
- System.out.println("整形式 XML 文書です。");
- } catch (SAXParseException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
□ DTD 文書("emp.dtd")
- <?xml version="1.0" encoding="Shift_JIS"?>
- <!DOCTYPE 社員 SYSTEM "emp.dtd">
- <社員>
- <名前 ID="A001">
- <名字 ふりがな="うえだ">上田</名字>
- <イニシャル>U</イニシャル>
- <給与>1000</給与>
- </名前>
- <名前 ID="A002">
- <名字 ふりがな="ながの">長野</名字>
- <イニシャル>N</イニシャル>
- <給与>1200</給与>
- </名前>
- <名前 ID="A003">
- <名字 ふりがな="まつもと">松本</名字>
- <イニシャル>M</イニシャル>
- <給与>1030</給与>
- </名前>
- <名前 ID="A004">
- <名字 ふりがな="えひめ">愛媛</名字>
- <イニシャル>E</イニシャル>
- <給与>1400</給与>
- </名前>
- </社員>
□実行結果
- <?xml version="1.0" encoding="Shift_JIS"?>
- <!ELEMENT 社員 (名前+)>
- <!ELEMENT 名前 (名字, イニシャル, 給与)>
- <!ELEMENT 名字 (#PCDATA)>
- <!ELEMENT イニシャル (#PCDATA)>
- <!ELEMENT 給与 (#PCDATA)>
- <!ATTLIST 名前 ID ID #REQUIRED>
- <!ATTLIST 名字 ふりがな CDATA #REQUIRED>
$ java DomParser01 emp.xml 整形式 XML 文書です。
- try {
- // DOMパーサの生成
- DOMParser parser = new DOMParser();
- // 妥当性検証の設定
- parser.setFeature("http://xml.org/sax/features/validation", true);
- // XML文書の取得
- parser.parse(ファイル名);
- // Documentインスタンスの取得
- Document doc = parser.getDocument();
- System.out.println("整形式 XML 文書です。");
- } catch (SAXParseException e) {
- ...
- } catch (Exception e) {
- ...
- }
フィーチャー | デフォルト値 | 意味 |
---|---|---|
http://xml.org/sax/features/namespaces/ | true | 名前空間を認識する |
http://xml.org/sax/features/validation/ | false | 妥当性検証を行う |
http://apache.org/xml/features/validation/dynamic/ | false | 文書型宣言が存在するときに限り,妥当性検証を行う |
http://apache.org/xml/features/validation/schema/ | false | XML Schema 文書を使った妥当性検証を行う |
http://apache.org/xml/features/continue-after-fatal-error/ | false | 整形式検証に違反していても,さらに解析を続ける |
http://apache.org/xml/features/dom/include-ignorable-whitespaces/ | false | 無視できる空白を DOM ツリーに含める |
- import org.apache.xerces.parsers.DOMParser;
- import org.w3c.dom.Document;
- import org.xml.sax.SAXParseException;
- public class DomParser02 {
- public static void main(String[] args) {
- try {
- // DOMパーサの生成
- DOMParser parser = new DOMParser();
- // 妥当性検証の設定
- parser.setFeature("http://xml.org/sax/features/validation", true);
- // XML文書の取得
- parser.parse(args[0]);
- // Documentインスタンスの取得
- Document doc = parser.getDocument();
- System.out.println("整形式 XML 文書です。");
- } catch (SAXParseException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
□XML 文書("emp.xml")
□DTD 文書("emp.dtd")
□実行結果
$ java DomParser02 emp.xml 整形式 XML 文書です。
- NodeList nl = document.getElementsByTagName("*"); // ドキュメントのすべてのタグのNodeListを取得する
- for (int i = 0; i < nl.getLength(); i++) {
- Node n = nl.item(i); // 個々のノードの取り出し
- String name = n.getNodeName(); // ノード名の取り出し
- String value = n.getNodeValue(); // ノード内容の取り出し
- }
- Element rootElement = document.getDocumentElement(); // ルート要素の取り出し
- NodeList nl = rootElement.getChildNodes(); // ルート要素直下の要素群の取り出し
- for (int i = 0; i < nl.getLength(); i++) {
- Node n = nl.item(i); // 個々の要素の取り出し
- String name = n.getFirstChild().getNodeName(); // 要素名の取り出し
- String value = n.getFirstChild().getNodeValue(); // 要素内容の取り出し
- }
- Node n = nl.item(i); // 要素の取り出し
- NamedNodeMap nnm = n.getAttributes();
- for (int i = 0; i < nnm.getLength(); i++) {
- Attr attr = (Attr)nnm.item(i); // 属性の取り出し
- String name = attr.getName(); // 属性名の取り出し
- String value = attr.getValue(); // 属性値の取り出し
- }
- import org.apache.xerces.parsers.DOMParser;
- import org.w3c.dom.Document;
- import org.w3c.dom.NamedNodeMap;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXParseException;
- public class DomParser11 {
- public static void main(String[] args) {
- System.out.println("document = " + toString(getDocument(args[0])));
- }
- public static Document getDocument(String fileName) {
- try {
- // DOMパーサの生成
- DOMParser parser = new DOMParser();
- // XML文書の取得
- parser.parse(fileName);
- // Documentインスタンスの取得
- return parser.getDocument();
- } catch (SAXParseException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- public static String toString(Document doc) {
- StringBuffer sb = new StringBuffer();
- StringBuffer indent = new StringBuffer("+");
- // ドキュメントのすべてのタグのNodeListを取得する
- NodeList nlist = doc.getElementsByTagName("*");
- NodeList childlist = null;
- NamedNodeMap map = null;
- // ノードリストをたどる
- for (int i = 0; i < nlist.getLength(); i++) {
- sb.append("\n" + indent + "<elem>" + nlist.item(i).getNodeName());
- sb.append(" = " + nlist.item(i).getNodeValue());
- indent.insert(0, " ");
- if (nlist.item(i).hasAttributes()) {
- map = nlist.item(i).getAttributes();
- // 属性情報を取り出す(アルファベット順)
- for (int j = 0; j < map.getLength(); j++) {
- sb.append("\n" + indent + "<attr>"
- + map.item(j).getNodeName());
- sb.append(" = " + map.item(j).getNodeValue());
- }
- }
- if (nlist.item(i).hasChildNodes()) {
- childlist = nlist.item(i).getChildNodes();
- for (int j = 0; j < childlist.getLength(); j++) {
- sb.append("\n" + indent + "<body>"
- + childlist.item(j).getNodeName());
- sb.append(" = " + childlist.item(j).getNodeValue());
- }
- }
- indent.delete(0, 2);
- }
- return sb.toString();
- }
- }
□XML 文書("emp.xml")
□DTD 文書("emp.dtd")
□実行結果
$ java DomParser11 emp.xml document = +<elem>社員 = null +<body>#text = +<body>名前 = null +<body>#text = +<body>名前 = null +< body>#text = +< body>名前 = null +<body>#text = +<body>名前 = null +<body>#text = +<elem>名前 = null +<attr>ID = A001 +<body>#text = +<body>名字 = null +<body>#text = +<body>給与 = null +<body>#text = +<body>イニシャル = null +<body>#text = +<elem>名字 = null +<attr>ふりがな = うえだ +<body>#text = 上田 +<elem>給与 = null +<body>#text = 1000 +< elem>イニシャル = null +< body>#text = U +<elem>名前 = null +<attr>ID = A002 +<body>#text = +<body>名字 = null +<body>#text = +<body>イニシャル = null +<body>#text = +<body>給与 = null +<body>#text = +<elem>名字 = null +<attr>ふりがな = ながの +<body>#text = 長野 +<elem>イニシャル = null +<body>#text = N +<elem>給与 = null +<body>#text = 1200 +< elem>名前 = null +< attr>ID = A003 +<body>#text = +<body>名字 = null +<body>#text = +<body>イニシャル = null +<body>#text = +<body>給与 = null +<body>#text = +<elem>名字 = null +<attr>ふりがな = まつもと +<body>#text = 松本 +<elem>イニシャル = null +<body>#text = M +<elem>給与 = null +<body>#text = 1030 +<elem>名前 = null +<attr>ID = A004 +<body>#text = +<body>名字 = null +<body>#text = +<body>イニシャル = null +<body>#text = +<body>給与 = null +<body>#text = +<elem>名字 = null +<attr>ふりがな = えひめ +<body>#text = 愛媛 +<elem>イニシャル = null +<body>#text = E +<elem>給与 = null +<body>#text = 1400
インタフェース / クラス | コンストラクター / メソッド |
---|---|
Document |
Attr createAttribute(String name)
Element createElement(String tagName) Text createTextNode(String data) Element getDocumentElement() NodeList getElementsByTagName(String tagname) |
Node |
Node appendChild(Node newChild)
NamedNodeMap getAttributes() NodeList getChildNodes() Node getFirstChild() Node getLastChild() Node getNextSibling() String getNodeName() short getNodeType() ... 戻り値 String getNodeValue() Document getOwnerDocument() Node getParentNode() Node getPreviousSibling() boolean hasAttributes() boolean hasChildNodes() Node insertBefore(Node newChild, Node refChild) void normalize() Node removeChild(Node oldChild) Node replaceChild(Node newChild, Node oldChild) void setNodeValue(String nodeValue) |
NodeList |
int getLength()
Node item(int index) |
Element |
String getAttribute(String name)
Attr getAttributeNode(String name) NodeList getElementsByTagName(String name) String getTagName() boolean hasAttribute(String name) void removeAttribute(String name) Attr removeAttributeNode(Attr oldAttr) void setAttribute(String name, String value) |
NamedNodeMap |
int getLength()
Node getNamedItem(String name) Node item(int index) Node removeNamedItem(String name) Node setNamedItem(Node arg) |
Attr |
String getName()
Element getOwnerElement() String getValue() void setValue(String value) |
ATTRIBUTE_NODE | ノードは Attr です。 |
CDATA_SECTION_NODE | ノードは CDATASection です。 |
COMMENT_NODE | ノードは Comment です。 |
DOCUMENT_FRAGMENT_NODE | ノードは DocumentFragment です。 |
DOCUMENT_NODE | ノードは Document です。 |
DOCUMENT_TYPE_NODE | ノードは DocumentType です。 |
ELEMENT_NODE | ノードは Element です。 |
ENTITY_NODE | ノードは Entity です。 |
ENTITY_REFERENCE_NODE | ノードは EntityReference です。 |
NOTATION_NODE | ノードは Notation です。 |
PROCESSING_INSTRUCTION_NODE | ノードは ProcessingInstruction です。 |
TEXT_NODE | ノードは Text ノードです。 |