import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class FileParser
{
String fileName;
/** Creates a new instance of FileParser */
public FileParser(String name)
{
this.fileName= name;
File f = new File(fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
read(doc);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void read(Document doc)
{
Element root = doc.getDocumentElement();
NodeList list = doc.getElementsByTagName("campagn");
for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; NodeList nodelist = element.getElementsByTagName("name"); Element element1 = (Element) nodelist.item(0); NodeList fstNm = element1.getChildNodes(); System.out.println("Name : " + (fstNm.item(0)).getNodeValue()); NodeList nodelist1 = element.getElementsByTagName("beginningdate"); Element element2 = (Element) nodelist1.item(0); NodeList beginningdate = element2.getChildNodes(); System.out.println("beginningdate : " + (beginningdate.item(0)).getNodeValue()); NodeList nodelist2 = element.getElementsByTagName("endingdate"); Element element3 = (Element) nodelist2.item(0); NodeList endingdate = element3.getChildNodes(); System.out.println("endingdate : " + (endingdate.item(0)).getNodeValue()); NodeList nodelist3 = element.getElementsByTagName("file"); Element element4 = (Element) nodelist3.item(0); NodeList file = element4.getChildNodes(); System.out.println("file : " + (file.item(0)).getNodeValue()); } } } } ------------------------==============------------------------ XML File(campagn.xml):
No comments:
Post a Comment