** 개인적 메모입니다. 태클 사절 **
DOM 파서
: 문서의 모든 내용을 메모리에 트리 형태로 펼친 후 읽기 때문에 속도는 대단히 빠르고,
임의의 노드를 여러번 읽을 수 있다. 하지만, 전체 문서를 다 읽어서 트리를 완성한 후에야
읽기가 가능하므로, 처음 시작이 다소 느리다는 단점이 있고, 문서가 커지면 메모리를
많이 소비하는 것도 문제다.
XML 편집도 가능하다.
String xml = "\n" +
"- Mouse
";
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream istream = new ByteArrayInputStream(xml.getBytes("utf-8"));
Document doc = builder.parse(istream);
Element order = doc.getDocumentElement();
NodeList items = order.getElementsByTagName("item");
Node item = items.item(0);
Node text = item.getFirstChild();
String ItemName = text.getNodeValue();
}
catch (Exception e){
}
/**
* XML 문서를 열려면 먼저 DocumentBuilderFactory 객체 필요. (추상으로 newInstance로.)
*이후에 추상클래스 newDocumentBuilder()도 필요.
*/
static DocumentBuilderFactory newInstance ()
DocumentBuilder newDocumentBuilder()
/** XML 전체를 받아오는 것. */
Document parse (InputStream stream [, String systemId])
Document parse (String uri)
Document parse (File file)
/**
* XML 문서는 유일한 루트 엘리먼트를 가진다.
* 이 메서드가 루트 앨리먼트를 구한다. 루트를 구해야 자식들을 읽을 수 있다.
*/
Document parse (InputStream stream [, String systemId])
Document parse (String uri)
Document parse (File file)
newInstance ()
DocumentBuilder newDocumentBuilder()
/** XML 문서를 열려면 먼저 DocumentBuilderFactory 객체 필요. (추상으로 newInstance로.)
이후에 추상클래스 newDocumentBuilder()도 필요. */
// XML 전체를 받아오는 것.
Document parse (InputStream stream [, String systemId])
Document parse (String uri)
Document parse (File file)
//XML 문서는 유일한 루트 엘리먼트를 가진다.
Element Document.getDocumentElement ()
//루트 밑에 tagname을 가진 sub-parent를 구한다.
NodeList Element.getElementsByTagName (String tagname)
// DOM은 엘리먼트 안의 문자열도 하나의 객체로 취급하기 때문에 태그 안의
// 문자열을 읽으려면 getFirstChild -> getNodeValue로 값을 읽어야 한다.
String getNodeName ()
short getNodeType ()
String getNodeValue ()
Node getFirstChild()
Node getLastChild()
Node getNextSibling ()
Node getPreviousSibling ()
Node getParentNode ()
NodeList getChildNodes ()
************ ex *************
String xml = "\n" +
"" +
"- Mouse
"+
"- KeyBoard
" +
"- HDD
";
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream istream = new ByteArrayInputStream(xml.getBytes("utf-8"));
Document doc = builder.parse(istream);
Element order = doc.getDocumentElement();
NodeList items = order.getElementsByTagName("item");
String result = "";
for (int i = 0; i < items.getLength; i++){
Node item = items.item(i);
Node text = item.getFirstChild();
String ItemName = text.getNodeValue();
Result += ItemName + " : ";
NamedNodeMap Attrs = item.getAttributes();
for (int j = 0; j < Attrs.getLength(); j++){
Node attr = Attrs.item(j);
Result += (attr.getNodeName() + " = " + attr.getNodeValues() + " ");
}
Reulst += "\n";
}
} catch (Exception e) {
}
'Program > Android' 카테고리의 다른 글
| [Android] TextView 에 글씨 색상 및 효과. (1) | 2012.09.03 |
|---|---|
| [Android] 백그라운드 전환하기. (3) | 2012.08.13 |
| Android XmlPullParserFactory (0) | 2012.08.09 |
| Android Emulator Key Assignment (0) | 2012.04.19 |
| [Android] Apk jarsinger (1) | 2012.04.10 |