방랑로그
[java] XPath (ex) 본문
[java] XPath (ex)
// XML 활용범위가 넓다.
// XPath 를 처음 써본 기념으로 기록을 남긴다.
import java.io.InputStream;
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;
import javax.xml.xpath.*;
public class SysMsgManager {
private String LTN= null;
private String data = null;
private Document document = null;
public SysMsgManager() {}
public SysMsgManager(String LTN) throws Exception{
this.LTN=LTN;
dataLoad();
}
public void setLTN(String LTN){ this.LTN=LTN; }
public String LTN(){ return this.LTN; }
//XPath 를 사용하지 않은 메소드
/*
public String getMsg(String sCode){
String msgTmp = null;
Element elAt = null;
NodeList nodelist = document.getElementsByTagName(msg);
for(int i=0;i
if(sCode.equals(elAt.getElementsByTagName(code).item(0).getFirstChild().getNodeValue()) ){
if(this.langTagName.equals(english)) msgTmp=elAt.getElementsByTagName(english).item(0).getFirstChild().getNodeValue() ;
else msgTmp=elAt.getElementsByTagName(korean).item(0).getFirstChild().getNodeValue();
}
}
if(msgTmp==null || msgTmp.equals()) msgTmp = Message Load Failed!!;
return msgTmp;
}
*/
//XPath를 사용한 메소드 (import javax.xml.xpath)
public String getMsg(String sCode) throws Exception{
String msgTmp = ;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(//xxx/aaa/text()); //DOM 의 노드를 path 형식으로
Object result = expr.evaluate(document,XPathConstants.NODESET);
NodeList nodes = (NodeList)result;
NodeList nodesTmp = null;
for(int i=0;i
msgTmp+=Check In sCode!! +nodes.item(i).getNodeValue()+\n;
if(LTN.equals(english)){
expr=xpath.compile(//xxx/ttt/vvv/text()); //DOM 의 노드를 path 형식으로
result = expr.evaluate(document,XPathConstants.NODESET);
nodesTmp = (NodeList)result;
msgTmp+= nodesTmp .item(i).getNodeValue();
}else{
expr=xpath.compile(//msg/content/korean/text());
result = expr.evaluate(document,XPathConstants.NODESET);
nodesTmp = (NodeList)result;
msgTmp+= nodesTmp .item(i).getNodeValue();
}
}
}
if(msgTmp==null || msgTmp.equals()) msgTmp = Message Load Failed!!;
return msgTmp;
}
//XPath를 사용한 메소드 (import org.apache.xpath.XPathAPI;)
public String getMsg(String sCode) throws Exception{
String msgTmp = "" ;
NodeList nodelist = XPathAPI.selectNodeList(document,"//root");
Node node = null;
for(int i=0;i<nodelist.getLength();i++){
node=nodelist.item(i);
if(sCode.equals( XPathAPI.selectSingleNode(node,"code/text()").getNodeValue() ) ){
if(LTN.equals("flag1")){
msgTmp+=XPathAPI.selectSingleNode(nodelist.item(i),"child/child1/text()").getNodeValue() ;
}else{
msgTmp+=XPathAPI.selectSingleNode(nodelist.item(i),"child/child2/text()").getNodeValue() ;
}
}
}
if(msgTmp==null || msgTmp.equals("")) msgTmp="Message Load Error !!";
return msgTmp;
}
//XML Data Loading
private void dataLoad () throws Exception {
InputStream is = //이부분은 각자 알맞게 XML을 스트림으로.... ;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(is);
}
'IT개발 > JAVA & J2EE' 카테고리의 다른 글
[j2ee] 내부객체 (0) | 2017.12.15 |
---|---|
[java] xml (ex) (0) | 2017.12.15 |
[java] XPath (0) | 2017.12.15 |
[java] XPath (0) | 2017.12.15 |
[java,php] Session iframe 문제 (0) | 2017.12.15 |