我有 XML 文件,我尝试读取到 Document 对象:
<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>
我尝试通过代码将其读入 Document 对象:
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;
}
我总是得到 xml_doc NULL 有人可以帮我解决问题吗?
我总是知道文件不存在,使用: Document doc = XMLReader.Get_XML_Document("C:\MCSA\MCSA_config.xml");