Code to parse XML file and get the Parsed data, in Android(used in Movie_Project)

package org.example.Movie.XMLFileData;
import java.io.File;
import java.util.ArrayList;
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;
public class Audios {

 public ArrayList<String> getAudioList()
 {
  ArrayList<String>   herosNames=new ArrayList<String>();
  try {
   File fXmlFile = new File("http://9.dharani.org/Audios.xml");  
   DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
   Document doc = dBuilder.parse(fXmlFile);
   doc.getDocumentElement().normalize();
 
   //System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
   NodeList nList = doc.getElementsByTagName("Movie"); 
  
   for (int temp = 0; temp < nList.getLength(); temp++) { 
      Node nNode = nList.item(temp);
      if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
         Element eElement = (Element) nNode;         
         System.out.println("MovieName : " + herosNames.add(getTagValue("MovieName", eElement)));        
      }
   }
    } catch (Exception e) {
   e.printStackTrace();
    }
  return herosNames; }

 private static String getTagValue(String sTag, Element eElement) {
  NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
 
         Node nValue = (Node) nlList.item(0);
 
  return nValue.getNodeValue();
   }
}

above code used by just calling a method from other activity like:
Audios audio=new Audios();
ArrayList<String>  audioNames=audio.getAudioList();

Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard