0

i have XML file i try to read to a Document object:

<MCSA_Configuration>
    <Variables>
        <python27_path> C:\Python27\python.exe </python27_path>
        <python32_path> C:\Python32\python.exe </python32_path>
        <xlrd> xlrd-ok </xlrd>
    </Variables>
</MCSA_Configuration>

and i try to read it into Document object by code:

 import java.io.File;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;

public static Document Get_XML_Document(String xml_file_path) {

        File file;
        Document xml_doc = null;
        // TODO code application logic here 
        try {
            file = new File(xml_file_path);
            if (file.exists()) {
                    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                    xml_doc = docBuilder.parse(file);
            } else {
                System.out.println("Error: XML File not found!");
            }

        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return xml_doc;
    }

i always get xml_doc NULL can someone help me solve the problem?

I Always get that the file doesn't exist, is use: Document doc = XMLReader.Get_XML_Document("C:\MCSA\MCSA_config.xml");

2
  • 2
    Do you get Error: XML File not found! or a stack trace? Commented Dec 18, 2012 at 12:16
  • Check for the file whether it exists or not . If problem persists then post your stack stack trace Commented Dec 18, 2012 at 12:18

2 Answers 2

1

instead of just checking if (file != null) check whether file exists or not if (file.exists()). Possible issue is file does not exist at that path

Sign up to request clarification or add additional context in comments.

3 Comments

I Always get that the file doesn't exist, is use: Document doc = XMLReader.Get_XML_Document("C:\\MCSA\\MCSA_config.xml"); and the file is realy there..
can you also try with forward slashes, e.g. XMLReader.Get_XML_Document("C:/MCSA/MCSA_config.xml");
Sounds like there maybe a trailing space in your filename or fileextensions are not shown in windows and the file is actually MCSA_config.xml.txt
0

Your code working fine.

Check the condition after getting document

if(xml_doc == null)
  System.out.println("Doc is null");
else 
  System.out.println("Doc is not  null");

You will get

Doc is not null

When you try to print the document it will give the output

[#document: null]

You can perform operation through that document object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.