package jp.co.melb.alive.ta.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class XMLParser {
private static File file;
private static FileInputStream fis;
private static Document doc;
public void fileRead(String filePath){
try{
file=new File(filePath);
fis=new FileInputStream(file);
InputSource inputSource=new InputSource(fis);
inputSource.setEncoding("Shift_JIS");
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
this.doc = builder.parse(inputSource);
fis.close();
}catch(Exception e){
try {
fis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
public String getTagValue(String parentTag,String tag){
String tagValue="";
NodeList nl = this.doc.getElementsByTagName(parentTag);
Node parentnNode=nl.item(0);
Element element =(Element)parentnNode;
Node node;
if(element.getElementsByTagName(tag)!=null && element.getElementsByTagName(tag).getLength()>0){
node=element.getElementsByTagName(tag).item(0);
tagValue=node.getTextContent();
}
return tagValue;
}
}