-------------------------------------------------------------------------------------------------
출처: 웹 서핑 & 블로그 주인장
-------------------------------------------------------------------------------------------------
======================================================================================================
[ XML 표준 SAX 방식 ]
======================================================================================================
개념: SAXParser가 org.xml.sax.helpers.DefaultHandler에게 파싱된 tag(element)와 text를
차례로 전달한다.
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
public class Echo01 extends DefaultHandler
{
public static void main(String argv[])
{
if (argv.length != 1) {
System.err.println("Usage: cmd filename");
System.exit(1);
}
// Use an instance of ourselves as the SAX event handler
DefaultHandler handler = new Echo01();
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
// Set up output stream
out = new OutputStreamWriter(System.out, "UTF8");
// Parse the input
SAXParser saxParser = factory.newSAXParser();
saxParser.parse( new File(argv[0]), handler);
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(0);
}
static private Writer out;
//===========================================================
// SAX DocumentHandler methods
//===========================================================
public void startDocument()
throws SAXException
{
emit("<?xml version='1.0' encoding='UTF-8'?>");
nl();
}
public void endDocument() throws SAXException
{
try {
nl();
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
public void startElement(String namespaceURI,
String lName, // local name
String qName, // qualified name
Attributes attrs)
throws SAXException
{
String eName = lName; // element name
if ("".equals(eName)) eName = qName; // namespaceAware = false
emit("<"+eName);
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); i++) {
String aName = attrs.getLocalName(i); // Attr name
if ("".equals(aName)) aName = attrs.getQName(i);
emit(" ");
emit(aName+"=\""+attrs.getValue(i)+"\"");
}
}
emit(">");
}
public void endElement(String namespaceURI,
String sName, // simple name
String qName // qualified name
)
throws SAXException
{
emit("</"+sName+">");
}
public void characters(char buf[], int offset, int len)
throws SAXException
{
String s = new String(buf, offset, len);
emit(s);
}
//===========================================================
// Utility Methods ...
//===========================================================
// Wrap I/O exceptions in SAX exceptions, to suit handler signature requirements
private void emit(String s)
throws SAXException
{
try {
out.write(s);
out.flush();
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
// Start a new line
private void nl()
throws SAXException
{
String lineEnd = System.getProperty("line.separator");
try {
out.write(lineEnd);
} catch (IOException e) {
throw new SAXException("I/O error", e);
}
}
}
======================================================================================================
[ XML 표준 DOM 방식 ]
======================================================================================================
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
class DomParse
{
public static void main(String[] args)
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// xml 문서 Parsing → tree 형태로 메모리에 적재 [리턴값: document (interface)]
Document xmlDoc = builder.parse("CSPrivateScreenData.xml");
Element root = xmlDoc.getDocumentElement();
NodeList list = root.getElementsByTagName("CharacterInfo"); // 자식을 가지는 TagName
for (int i = 0; i < list.getLength(); i++)
{
Element element = (Element) list.item(i);
System.out.println("result: " + getData(element, "FieldName")); // 해당 태그의 Text 값 출력
}
}
catch (Exception e) {
e.printStackTrace(System.err);
}
}
public static String getData(Element element, String tagName)
{
NodeList list = element.getElementsByTagName(tagName);
Element cElement = (Element) list.item(0);
if (cElement.getFirstChild() != null) {
return cElement.getFirstChild().getNodeValue();
} else {
return "";
}
}
}
======================================================================================================
[ kXML Parser SAX 방식 ]
======================================================================================================
Parsing Element-Only and Text-Only Content
General XML content can be parsed with the XML pull API using a loop advanving to the next event and a switch statement that depends on the event type. However, when using XML for data transfer (in contrast to text documents), most XML elements contain either only text or only other elements (possibly with further sub-elements). For those common cases, the parsing process can be simplified significantly by using the XmlPull API methods nextTag and nextText. Additionally, the method require() may optionally be used to assert a certain parser state.
The following sample illustrates both situations and methods. The outer element elements has element-only content; the contained text-elements have text-only content:
<elements>
<text>text1</text>
<text>text2</text>
</elements>
Parsing Code
parser.nextTag();
parser.require(XmlPullParser.START_TAG, null, "elements");
while(parser.nextTag() == XmlPullParser.START_TAG) {
parser.require(XmlPullParser.START_TAG, null, "text");
// handle element content
System.out.println("text content: "+ parser.nextText());
parser.require(XmlPullParser.END_TAG, null, "text");
}
parser.require(XmlPullParser.END_TAG, null, "elements");
nextTag() advances to the next start or end tag, skipping insignificant events such as white space, comments and PIs. nextText() requires that the current position is a start tag.
It returns the text content of the corresponding element. The post condition is that the current position is an end tag. Please note that the calls require() are optional assertions, they may be
left out completely.
======================================================================================================
[ kXML Parser DOM 방식 ]
======================================================================================================
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import org.kxml2.io.*;
import org.kxml2.kdom.*;
import org.xmlpull.v1.*;
public class XMLElementFinder
{
private Element root;
/**
* 초기화 - URL 생성 후 해당 파일을 읽어와서 DOM 생성 (Using kXML Parser)
* @throws XmlPullParserException
* @throws IOException
*/
public XMLElementFinder(String filePath) throws XmlPullParserException, IOException
{
// 원격지의 파일을 읽어온다.
XmlPullParser parser = new KXmlParser();
InputStream inputFile = new URL(filePath).openStream();
parser.setInput(inputFile, "UTF-8");
// parser.setInput(new FileReader(filePath)); // 지정 경로의 파일을 읽을 경우
// KXML parser로 Parsing 한 후 DOM 객체 생성
Document document = new Document();
document.parse(parser); // KXML Parser로 Parsing
root = document.getRootElement(); // Root Element
}
/**
* Root Tag Element를 반환하는 함수
* @return root: Root Tag
* @throws Exception
*/
public Element getRootTagElement() throws Exception
{
return root;
}
/**
* 태그 이름에 해당하는 Tag Element들을 반환
* @param elementName 자식 태그들만을 가지는 Tag의 이름
* @param tagName 찾을 Tag들의 이름
* @return child: 찾은 Tag들의 배열
* @throws Exception
*/
public Element[] getElmentsByTagName(Node elementName, String tagName) throws Exception
{
Element parent = (Element)elementName; // 부모 Tag Element
Element child = null;
ArrayList tagList = new ArrayList();
int nParent = parent.getChildCount();
for(int i=0; i<nParent; ++i)
{
if(parent.getType(i) != Element.ELEMENT)
{
continue; // 유효한 XML Tag Element가 아닌 경우, 다시 찾는다.
}
child = parent.getElement(i);
if(tagName.equals(child.getName()))
{
tagList.add(child);
}
}
Element[] childs = new Element[tagList.size()]; // Child Tag Element Array
childs = castObjectsToElements(tagList.toArray()); // Cast (Object[] → Element[])
tagList.clear();
tagList = null;
return childs;
}
/**
* 해당 Element의 자식 Element들을 반환한다.
* @param node Tag Node
* @return childs: element의 자식 Tag에 해당하는 Elements
* @throws Exception
*/
public Element[] getChildElements(Node node) throws Exception
{
Element element = (Element)node;
ArrayList tagList = new ArrayList();
Element parent = element, child = null;
int nChild = element.getChildCount();
for(int i=0; i<nChild; ++i)
{
if(parent.getType(i) != Element.ELEMENT)
{
continue; // 유효한 XML Tag Element가 아닌 경우, 다시 찾는다.
}
child = parent.getElement(i);
tagList.add(child);
}
Element[] childs = new Element[tagList.size()]; // Child Tag Element Array
childs = castObjectsToElements(tagList.toArray()); // Cast (Object[] → Element[])
tagList.clear();
tagList = null;
return childs;
}
/**
* 해당 Element에 속한 Tag의 Value 값을 가져온다.
* @param node 해당 Tag Node
* @return resultText: 해당 태그의 Value 값
* @throws Exception
*/
public String getElementValueByTagName(Node node) throws Exception
{
Element element = (Element)node;
String resultText = element.getText(0);
return ((resultText != null) && (resultText.length() > 0))? resultText : null;
}
/**
* 태그 명에 해당하는 Node의 Attribute 값들을 반환한다.
* @param node Tag Node
* @return attributes: 해당 Element가 가지고 있는 Attribute 값
* @throws Exception
*/
public Hashtable getAtrributesByTagName(Node node) throws Exception
{
String attributeName = null, attributeValue = null;
Hashtable attributes = new Hashtable();
Element element = (Element)node;
int length = element.getAttributeCount();
for(int i=0; i<length; ++i)
{
attributeName = element.getAttributeName(i);
attributeValue = element.getAttributeValue(i);
if((attributeName != null) && (attributeValue != null))
{
attributes.put(attributeName, attributeValue);
}
}
return (attributes.size() > 0)? attributes : null;
}
/**
* Casting 관련 함수 (Object Array → Element Array)
* @return elements: element의 배열
* @throws ClassCastException
*/
private Element[] castObjectsToElements(Object[] objects) throws ClassCastException
{
int elementArraySize = objects.length;
Element[] elements = new Element[elementArraySize];
for(int i=0; i<elementArraySize; ++i)
{
elements[i] = (Element)objects[i];
}
return elements;
}
}
'IT_Programming > Java' 카테고리의 다른 글
[펌] Java에서의 Out Of Memory Error(OOME)에 대한 정리... (0) | 2010.06.23 |
---|---|
Java로 구현한 JavaScript의 escape()와 unescape() (0) | 2010.06.17 |
MediaTracker 사용하기 (0) | 2010.05.04 |
[펌] SWING에서의 쓰레드 핸들링 (0) | 2010.04.23 |
[펌] Concurrency in Swing (0) | 2010.04.23 |