2008년 09월 03일
XML Parser 구현시
//패키지
javax.xml.parsers.DocumentBuilder
javax.xml.parsers.DocumentBuilderFactory
//Dom , sax 패키지
org.w3c.dom.Document
org.w3c.dom.Element
org.w3c.dom.Node
org.w3c.dom.NodeList
org.xml.sax.InputSource
----------------------------------------------------------------------------------------------------
//메쏘드 [String 받을시 사용]
public static Document loadXmlString(String xmlData) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource xmlSrc = new InputSource(new StringReader(xmlData));
Document doc = builder.parse(xmlSrc);
//Element root = doc.getDocumentElement();
//root.normalize();
return doc;
} catch (Exception pce) {
pce.printStackTrace();
}
return null;
}
//xml 파일 읽어들어서 램 상주.
public static Document loadXmlFile(String FileUrl) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(FileUrl));
//Element root = doc.getDocumentElement();
//root.normalize();
return doc;
} catch (Exception pce) {
pce.printStackTrace();
}
return null;
}
--------------------------------------------------------------------------------
/**
* root Element 아래의 Tag text value 값을 반환한다.
* @param root root Element object
* @param tagName tag name
* @return tag의 text value
*/
public static String getTagValue(Document root, String tagName ) {
NodeList nList = root.getElementsByTagName(tagName);
for (int i = 0; i < nList.getLength(); i++) {
Node node = nList.item(i);
if (node != null) {
Node child = node.getFirstChild();
if ((child != null) && (child.getNodeValue() != null)) {
return child.getNodeValue();
}
}
}
return "";
}
-----------------------------------------------------------------------------------------------------------
/**
* Node의 attribute value를 반환한다.
* @param node Node object
* @param attrName attribute name
* @return attribute의 value
*/
public static String getAttribute(Node node, String attrName) {
if ((node != null) && (node instanceof Element)) {
return ((Element) node).getAttribute(attrName);
}
return "";
}
/**
* Node의 attribute value를 반환한다.
* @param node Node object
* @param attrName attribute name
* @return attribute의 value
*/
public static String getAttribute(NodeList nl, String attrName) {
String sAttribute = "";
for(int k = 0; k<nl.getLength(); k++){
Node n = nl.item(k);
if(n != null){
sAttribute = getAttribute(n,attrName);
}
}
return sAttribute;
}
이 글과 관련있는 글을 자동검색한 결과입니다 [?]
# by | 2008/09/03 13:44 | JAVA | 트랙백 | 덧글(1)





☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
무슨 말인지 모르겠다.. ^^;;;
열공모드.. 고고...