Java - 读取 XML 文件
我正在尝试从XML文件中读取一些数据并遇到一些问题,我拥有的XML如下所示:
<xml version="1.0" encoding="UTF-8"?>
<EmailSettings>
<recipient>test@test.com</recipient>
<sender>test2@test.com</sender>
<subject>Sales Query</subject>
<description>email body message</description>
</EmailSettings>
我正在尝试将这些值作为字符串读取到我的Java程序中,到目前为止,我已经编写了以下代码:
private static Document getDocument (String filename){
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(true);
factory.setValidating(false);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(filename));
}
catch (Exception e){
System.out.println("Error reading configuration file:");
System.out.println(e.getMessage());
}
return null;
}
Document doc = getDocument(configFileName);
Element config = doc.getDocumentElement();
不过,我正在努力读取实际的字符串值。