Im trying to use an XML as a configuration file for my java application but I am getting an exception on the parsing. My XML is: I checked with several people and they told the xml is correct, so did the w3cValidator.
<?xml version="1.0" encoding="UTF8"?>
<config>
<devpath>"C:/Users/fer/Desktop/Files/"</devpath>
<pfxname>keystore.pfx</pfxname>
<fontname>font.ttf</fontname>
<pfxpass>keystore_pass</pfxpass>
<keypass>key_pass</keypass>
</config>
My code to read and parse:
private static void readXMLConfig() {
String devpath = null;
String pfxname = null;
String fontname;
String pfxpass;
String keypass;
Document dom;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
File file = new File("config");
DocumentBuilder db = dbf.newDocumentBuilder();
dom = db.parse(file);
Element root = dom.getDocumentElement();
devpath = getTextValue(devpath, root, "Developer_Route");
pfxname = getTextValue(pfxname, root, "Pfx_Route");
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getTextValue(String def, Element doc, String tag) {
String value = def;
NodeList nl;
nl = doc.getElementsByTagName(tag);
if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {
value = nl.item(0).getFirstChild().getNodeValue();
}
return value;
}
I am getting an error every time which is
[Fatal Error] config:1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; systemId: file:///config; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:177)
at clarice.itext5.ClariceIText5.readXMLConfig(ClariceIText5.java:404)
at clarice.itext5.ClariceIText5.main(ClariceIText5.java:97)