0

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)
David
  • 6,462
  • 2
  • 25
  • 22
Alan Faz
  • 142
  • 14
  • 2
    It's possible that this error is thrown because your `encoding` should be "utf-8" (see [here](http://www.xml.com/axml/target.html#charencoding)). But more likely is that there's some non-printable garbage at the start of your file. Use a hex-dump program to see. – kdgregory Apr 16 '14 at 19:04
  • possible duplicate of [Content is not allowed in Prolog SAXParserException](http://stackoverflow.com/questions/4569123/content-is-not-allowed-in-prolog-saxparserexception) – Nathan Hughes Apr 16 '14 at 19:08

0 Answers0