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");

SRy
  • 2,901
  • 8
  • 36
  • 57
Aviadjo
  • 635
  • 5
  • 17
  • 36

2 Answers2

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

vishal_aim
  • 7,636
  • 1
  • 20
  • 23
  • 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.. – Aviadjo Dec 18 '12 at 17:06
  • can you also try with forward slashes, e.g. `XMLReader.Get_XML_Document("C:/MCSA/MCSA_config.xml");` – vishal_aim Dec 19 '12 at 03:40
  • 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 – IDKFA Dec 19 '12 at 06:46
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.

Dhinakar
  • 4,061
  • 6
  • 36
  • 68