Sunday 4 August 2013

read .xml file using smb jcifs , jcifs


We are already discuss Java CIFS Client Library here.


Now reading .xml file using smb Requirement:-
1. PersonDetails.xml reside in server or another machine


     
         Arun
         2009-08-21T08:43:02
         Kumar
       
      
         15000
         5000
      

2. Download jcifs-1.1.11.jar add in your lib folder

Constant.java
package com.javastoreroom.mytestapp;
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
    public class Constant {
  
    public static final String USER_NAME = "domain\\username";
    public static final String PASSWORD = "domain\\password";
    public static final String FILE_SOURCE_PATH = "smb://IP-Address/folderName/PersonDetails.xml"; // The local network's broadcast address of  target file or directory. SmbFile URLs
     
 
}

SmbConnect.java

package com.javastoreroom.mytestapp;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SmbConnect {

 private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(SmbConnect.class);

 public static void main(String... args) {
  new SmbConnect().readXMLFiles();
 }

 public void readXMLFiles() {
  try {
   NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, Constant.USER_NAME, Constant.PASSWORD); //This class stores and encrypts NTLM user credentials.
   
   SmbFile sFile = new SmbFile(Constant.FILE_SOURCE_PATH , auth); //This class represents a resource on an SMB network.
          try {
       
           if(sFile.getName().endsWith(".xml")){
            
     InputStream inputStream = sFile.getInputStream();
     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document doc = dBuilder.parse(inputStream);
     doc.getDocumentElement().normalize();
     NodeList nList = doc.getElementsByTagName("person");            
            
     for (int temp = 0; temp < nList.getLength(); temp++) {
      Node nNode = nList.item(temp);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
       Element eElement = (Element) nNode;
       
       log.info("|===================Here is person details===================|");
       
       log.info(" name -- >" + eElement.getElementsByTagName("name").item(0).getTextContent());
       log.info(" joining date -- > " + eElement.getElementsByTagName("join-date").item(0).getTextContent());
       log.info(" last name -- > " + eElement.getElementsByTagName("last-name").item(0).getTextContent());
       log.info(" basic salary -- > " + eElement.getElementsByTagName("basic").item(0).getTextContent());
       log.info(" hra allowance -- > " + eElement.getElementsByTagName("HRA").item(0).getTextContent());
       
       log.info("|===========================EOF=============================|");
       
      }
            
               }
              }
    } catch (Exception exception) {
    exception.printStackTrace();
   }
  }
  catch (Exception e) {
   e.printStackTrace();
  }
 }
 
}


after executing SmbConnect.java following output are shown :-


DONE :)

No comments:

Post a Comment