public class hiDom extends Object
DOM(Document/Element)の取り扱いを簡便化します。
Nodeは隠蔽しています。
ElementのリストはArrayList<Element>としています。
DOM関連で必要なimportは次のものです。
// DOM関連 import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.*;
Dom(Document/Element)のインターフェースも用い次のようなアクセスができます。
import otsu.hiNote.*; import java.util.*; import org.w3c.dom.Document; import org.w3c.dom.Element; public class Test { public static void main(String[] args_){ try{ Document doc = hiDom.readXMLfile("data.xml"); chkElm(doc.getDocumentElement()); } catch(Exception e){ e.printStackTrace(hiU.err); System.exit(1); } } static void chkElm(Element element){ ArrayList<Element> elms=hiDom.getChildElements(element); for(Element elm:elms){ if(hiDom.isTextElement(elm)){ System.out.println(elm.getTagName()+":" +hiDom.getText(elm)); } else{ System.out.println(elm.getTagName()); chkElm(elm); } } } }
修飾子とタイプ | フィールドと説明 |
---|---|
static boolean |
indent |
修飾子とタイプ | メソッドと説明 |
---|---|
static Element |
appendElement(Document doc,
Element elm,
String name)
Elementに指定タグ名のElementを追加する.
|
static Element |
appendElement(Document doc,
String name)
Documentに指定タグ名のElementを追加する.
|
static Element |
appendElement(Element to_,
Element elm_)
他Documentからのelementを取り込む。
|
static Element |
appendElement(Element elm,
String name)
Elementに指定タグ名のElementを追加する.
|
static Element |
appendTextElement(Document doc,
Element elm,
String name,
String text)
Elementに指定タグ名のテキストElementを追加する.
|
static Element |
appendTextElement(Element elm,
String name,
String text)
Elementに指定タグ名のテキストElementを追加する.
|
static HashMap<String,String> |
getAttributes(Element elm_)
Elementの全attributeを取得する。
|
static ArrayList<Element> |
getChildElements(Element elm)
子Elementのリストを取得する.
|
static Element |
getChildTaggedElement(Element elm,
String tag)
指定TagのElement1個を取得する.
|
static ArrayList<Element> |
getChildTaggedElements(Element elm,
String tag)
指定TagのElementリストを取得する.
|
static Element |
getFirstChildElement(Element elm) |
static Element |
getTaggedElement(Element elm,
String tag)
指定TagのElementを取得する.
|
static ArrayList<Element> |
getTaggedElements(Element elm,
String tag)
指定TagのElementリストを取得する.
|
static String |
getText(Element elm)
テキスト部を得る.
|
static boolean |
isTextElement(Element elm)
子要素がテキストか調べる.
|
static Document |
newDocument()
新規DOM(Document)を作成する.
|
static void |
print(Node node_,
OutputStream wrt_)
DocumentまたはElementを印字する.
|
static void |
print(Node node_,
OutputStream outs_,
String charSet_)
DocumentまたはElementを印字する.
|
static void |
print(Node node_,
Writer wrt_)
DocumentまたはElementを印字する.
|
static void |
print(Node node_,
Writer wrt_,
String charSet_)
DocumentまたはElementを印字する.
|
static String |
printDom(Document doc_) |
static void |
printDom(Document doc_,
OutputStream wrt_) |
static void |
printDom(Document doc_,
OutputStream outs_,
String charSet_) |
static String |
printDom(Document doc_,
String charSet_) |
static void |
printDom(Document doc_,
Writer wrt_) |
static void |
printDom(Document doc_,
Writer wrt_,
String charSet_) |
static Document |
readXMLfile(File file_)
指定名のXMLファイルを読みDOM(Document)を得る
|
static Document |
readXMLfile(String fileName_)
指定名のXMLファイルを読みDOM(Document)を得る
次の作業に相当します。
|
static Document |
readXMLsource(InputSource is_)
指定名の入力ソースを読みDOM(Document)を得る.
|
static Document |
readXMLstream(InputStream is_)
指定名の入力ストリームを読みDOM(Document)を得る.
|
static Document |
readXMLtext(String text_)
XML-Text読みDOM(Document)を得る.
|
static void |
setAttributes(Element elm_,
HashMap<String,String> attrs_)
Elementに複数のattributeを設定する
|
static String |
toString(Node node_)
DocumentまたはElementを文字列にする.
|
static String |
toString(Node node_,
String charSet_)
文字列にする.
|
public static Document readXMLfile(String fileName_)
次の作業に相当します。
--- DocumentBuilderFactory dfact =DocumentBuilderFactory.newInstance(); DocumentBuilder builder=dfact.newDocumentBuilder(); Document doc =builder.parse(new File(fileName_));
fileName_
- XMLファイル名public static Document readXMLfile(File file_)
file_
- XMLファイルpublic static Document readXMLstream(InputStream is_)
is_
- 入力ストリームpublic static Document readXMLsource(InputSource is_)
InputSourceを使えばStringに入れたXMLの解析などもできます。
//import org.xml.sax.InputSource; String text;// ここにXMLデータがあるとする StringReader rd = new StringReader(text); InputSource is = new InputSource(rd); Document doc= hiDom.readXMLsource(is);
is_
- 入力ストリームpublic static Document readXMLtext(String text_)
text_
- 読み込む長文Stringpublic static Document newDocument()
次の作業に相当します。
DocumentBuilderFactory _dfact =DocumentBuilderFactory.newInstance(); DocumentBuilder _builder=_dfact.newDocumentBuilder(); Document doc =_builder.newDocument();root-elementはhiDom.appendElement()を用いて次のように生成します。
Document doc = hiDom.newDocument(); Element root= hiDom.appendElement(doc,"root");
public static void printDom(Document doc_, OutputStream outs_, String charSet_)
public static void printDom(Document doc_, OutputStream wrt_)
public static String toString(Node node_, String charSet_)
node_
- DocumentまたはElementcharSet_
- 文字セットpublic static String toString(Node node_)
node_
- DocumentまたはElementpublic static void print(Node node_, Writer wrt_, String charSet_)
node_
- DocumentまたはElementwrt_
- 出力するWritercharSet_
- 文字セット;nullを指定するとシステム標準public static void print(Node node_, Writer wrt_)
node_
- DocumentまたはElementwrt_
- 出力するWriterpublic static void print(Node node_, OutputStream outs_, String charSet_)
node_
- DocumentまたはElementouts_
- 出力するストリームcharSet_
- 文字セット(utf-8,shift-jisなど);nullを指定するとシステム標準public static void print(Node node_, OutputStream wrt_)
node_
- DocumentまたはElementwrt_
- 出力するWriterpublic static ArrayList<Element> getChildTaggedElements(Element elm, String tag)
指定要素の直接の子Elementの中で指定タグのものを得ます。
elm
- 基準Elementtag
- タグpublic static Element getChildTaggedElement(Element elm, String tag)
指定要素の直接の子Elementの中で指定タグのもの内最初に見つけた1個を得ます。
elm
- 基準Elementtag
- タグpublic static ArrayList<Element> getTaggedElements(Element elm, String tag)
指定Elementの子孫Elementで、指定名のタグを持つものを検索します。
Documentから検索するにはDocument.getDocumentElement()で主Element
を取得して引数とします。
// Document doc; ArrayList<Element> _elms= hiDom.getChildTaggedElements( doc.getDocumentElement() ,"タグ");
elm
- 基準Elementtag
- タグpublic static Element getTaggedElement(Element elm, String tag)
指定Elementの子孫Elementで、指定名のタグを持つものを検索します。
Documentから検索するにはDocument.getDocumentElement()で主Element
を取得して引数とします。
// Document doc; ArrayList<Element> _elms= hiDom.getChildTaggedElements( doc.getDocumentElement() ,"タグ");
elm
- 基準Elementtag
- タグpublic static String getText(Element elm)
子nodeがTEXT,CDATA-SECTIONの場合、
文字列部を取得します。
それ以外の場合は空の文字列が戻ります。
elm
- テキスト部を得るElementpublic static boolean isTextElement(Element elm)
子nodeがTEXT,CDATA-SECTIONか調べます。
elm
- テキスト部を得るElementpublic static ArrayList<Element> getChildElements(Element elm)
elm
- 基準Elementpublic static Element appendElement(Document doc, String name)
次の作業に相当します。
--- Element elm= doc.createElement(name); doc.appendChild(elm); //return elm;
doc
- このDocumentに追加されるname
- 追加するElementの名前public static Element appendElement(Element elm, String name)
次の作業に相当します。
--- Document doc= elm.getOwnerDocument(); if( doc==null ){ throw new Exception("can't get owner-document"); } Element ee= doc.createElement(name); elm.appendChild(ee); //return ee;
elm
- このElementに追加されるname
- 追加するElementの名前public static Element appendTextElement(Element elm, String name, String text)
テキストElementを追加します。テキストにはHTMLエスケープ
が施されます。
次の作業に相当します。
--- Document doc= elm.getOwnerDocument(); if( doc==null ){ throw new Exception("can't get owner-document"); } Element ee= doc.createElement(name); ee.appendChild(doc.createTextNode(text)); elm.appendChild(ee); //return ee;
elm
- このElementに追加されるname
- 追加するテキストElementの名前text
- 追加するテキストpublic static Element appendElement(Document doc, Element elm, String name)
次の作業に相当します。
--- Element ee= doc.createElement(name); elm.appendChild(ee); //return ee;
doc
- documentelm
- このElementに追加されるname
- 追加するElementの名前public static Element appendTextElement(Document doc, Element elm, String name, String text)
テキストElementを追加します。テキストにはHTMLエスケープ
が施されます。
次の作業に相当します。
--- Element ee= doc.createElement(name); ee.appendChild(doc.createTextNode(text)); elm.appendChild(ee); //return ee;
doc
- documentelm
- このElementに追加されるname
- 追加するテキストElementの名前text
- 追加するテキストpublic static HashMap<String,String> getAttributes(Element elm_)
次の操作に相当します。
HashMap<String,String> attrs = new HashMap<String,String>(); NamedNodeMap _attList = elm_.getAttributes(); for (int i=0; i<_attList.getLength(); i++) { Attr _att = (Attr)_attList.item(i); attrs.put(_att.getName(),_att.getValue()); }
elm_
- Elementpublic static void setAttributes(Element elm_, HashMap<String,String> attrs_)
elm_
- Elementattrs_
- attributeセット、キーはattribute名