Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: unable to get the o/p for xml file

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default unable to get the o/p for xml file

    hi,
    i m parsing xml file to java file....its is showing errors...unable to find the xml file...

    xml file:

    <?xml version = "1.0" ?>
    <Employee-Detail>

    <Employee>
    <Emp_Id> E-001 </Emp_Id>
    <Emp_Name> Vinod </Emp_Name>
    <Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
    </Employee>

    <Employee>
    <Emp_Id> E-002 </Emp_Id>
    <Emp_Name> Amit </Emp_Name>
    <Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
    </Employee>

    <Employee>
    <Emp_Id> E-003 </Emp_Id>
    <Emp_Name> Deepak </Emp_Name>
    <Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
    </Employee>

    </Employee-Detail>


    java file:

    package testxml;

    import javax.xml.parsers.*;
    import org.xml.sax.*;
    import org.xml.sax.helpers.*;
    import java.io.*;

    public class EmployeeDetails{
    public static void main(String[] args) throws IOException{
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter XML file name:");
    String xmlFile = bf.readLine();
    EmployeeDetails detail = new EmployeeDetails(xmlFile);
    }
    public EmployeeDetails(String str){
    try{
    File file = new File(str);
    if (file.exists()){
    SAXParserFactory parserFact = SAXParserFactory.newInstance();
    SAXParser parser = parserFact.newSAXParser();
    System.out.println("XML Data: ");
    DefaultHandler dHandler = new DefaultHandler(){

    boolean id;
    boolean name;
    boolean mail;

    public void startElement(String uri, String localName,
    String element_name, Attributes attributes)throws SAXException{
    if (element_name.equals("Emp_Id")){
    id = true;
    }
    if (element_name.equals("Emp_Name")){
    name = true;
    }
    if (element_name.equals("Emp_E-mail")){
    mail = true;
    }
    }

    public void characters(char[] ch, int start, int len) throws SAXException{
    String str = new String (ch, start, len);
    if (id){
    System.out.println("Emp_Id: "+str);
    id = false;
    }
    if (name){
    System.out.println("Name: "+str);
    name = false;
    }
    if (mail){
    System.out.println("E-mail: "+str);
    mail = false;
    }
    }
    };

    parser.parse(str, dHandler);
    }
    else{
    System.out.println("File not found!");
    }
    }
    catch (Exception e){
    System.out.println("XML File hasn't any elements");
    e.printStackTrace();
    }
    }
    }



    the error is FILE NOT FOUND...
    can anyone help me out....

    thanx


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: unable to get the o/p for xml file

    Is the file in the right place?
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: unable to get the o/p for xml file

    hi gud mrng...
    yes it is in the same path...i tried it nw its working fine....
    i need hlp...i m hvng "file.xml" file....i need to develop web-application program by using that xml file...can u tell hw to do tht....bye

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2011, 08:01 AM
  2. unable to run log code
    By javaking in forum What's Wrong With My Code?
    Replies: 10
    Last Post: April 7th, 2010, 12:49 AM
  3. Unable to sendViaPost to url
    By mousumi in forum Java Networking
    Replies: 2
    Last Post: January 28th, 2010, 04:52 AM
  4. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  5. Unable to create a new connection!
    By fh84 in forum JDBC & Databases
    Replies: 1
    Last Post: November 20th, 2009, 05:58 PM