RahulShaw

Parser.java

Jan 16th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.57 KB | None | 0 0
  1. import java.io.File;
  2. import javax.xml.parsers.DocumentBuilderFactory;
  3. import javax.xml.parsers.DocumentBuilder;
  4. import org.w3c.dom.Document;
  5. import org.w3c.dom.NodeList;
  6. import org.w3c.dom.Node;
  7. import org.w3c.dom.Element;
  8.  
  9. public class Parser {
  10.    public static void main(String[] args){
  11.        String toSearchAttribute = "ERG";
  12.  
  13.       try {
  14.          File inputFile = new File("parser.txt");
  15.          DocumentBuilderFactory dbFactory
  16.             = DocumentBuilderFactory.newInstance();
  17.          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  18.          Document doc = dBuilder.parse(inputFile);
  19.          doc.getDocumentElement().normalize();
  20.          NodeList nList = doc.getElementsByTagName("database");
  21.          for (int temp = 0; temp < nList.getLength(); temp++) {
  22.             Node nNode = nList.item(temp);
  23.             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  24.                Element eElement = (Element) nNode;
  25.                String nodeAttribute = eElement.getAttribute("key");
  26.                 if(toSearchAttribute.equals(nodeAttribute)){
  27.                    System.out.println("driver : "
  28.                       + eElement.getAttribute("driver"));
  29.                    System.out.println("url.local : "
  30.                            + eElement.getAttribute("url.local"));
  31.                    System.out.println("login : "
  32.                            + eElement.getAttribute("login"));
  33.                    System.out.println("password : "
  34.                            + eElement.getAttribute("password"));
  35.                 }
  36.             }
  37.          }
  38.       } catch (Exception e) {
  39.          e.printStackTrace();
  40.       }
  41.    }
  42. }
Add Comment
Please, Sign In to add comment