Solved

query about the parsing of xml file

Posted on 2009-05-18
6
192 Views
Last Modified: 2012-05-07
i m getting the always null value in document doc of follwing code

please give me any solution
its urgent

when i print this line  its give output     doc null
  System.out.println("After Parse method_______________->++++++ "+doc);
/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */
 

package net.ensode.jasperbook.struts;
 

import java.io.*;

    import javax.xml.parsers.*;

    import org.w3c.dom.*;

    import org.xml.sax.*;
 

    public class BasicDom {

        public static void main(String[] args) {

            Document doc = parseXmlFile("userxml.xml", false);

         System.out.println("After Parse method_______________->++++++ "+doc);

        }
 

        // Parses an XML file and returns a DOM document.

        // If validating is true, the contents is validated against the DTD

        // specified in the file.

        public static Document parseXmlFile(String filename, boolean validating) {

            try {

                // Create a builder factory

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

                factory.setValidating(validating);

                System.out.println("In Parse Methood++++++++++++++++++++++++++++++++++");

                // Create the builder and parse the file

                Document doc = factory.newDocumentBuilder().parse(new File(filename));

                return doc;

            } catch (SAXException e) {

                e.printStackTrace();

                // A parsing error occurred; the xml input is not valid

            } catch (ParserConfigurationException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return null;

        }

    }

Open in new window

0
Comment
Question by:achitaley
  • 3
  • 3
6 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24418872
the xml needs to be in the same directory you are running it from

0
 
LVL 92

Expert Comment

by:objects
ID: 24418878
where is the xml file?

0
 

Author Comment

by:achitaley
ID: 24418892
xml is also in the project folder
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 24418895
how are you running the code?

0
 

Author Comment

by:achitaley
ID: 24570755
through netbean IDE
0
 

Author Comment

by:achitaley
ID: 24589528
i tried but found This is not a proper solution
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now