1

Similar to this question, but unfortunately didn't help

I am trying to parse a String to XML in Java and keep getting the error:

[Fatal Error] output.txt:1:1: Content is not allowed in prolog.

I know it must be something to do with my XML string, because I ran a test with very basic XML and the error dissappeared.

XML

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="ff99v250_01" APIVersion="1.4.3139.0"?>
<jfxpf:XPF xmlns:jfxpf="http://www.xfa.com/schema/xml-package">
   <jfxpf:Package>
      <jfxpf:Resource Location="GenReq">
         <jfxpf:Link ContentType="application/x-jetform-cft" />
      </jfxpf:Resource>
      <jfxpf:Resource Location="default.xml">
         <jfxpf:Content ContentType="text/xml" Location="default.xml">
            <xfa:Data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
               <xfa:DataGroup>
                  <data xmlns:xfe="http://www.xfa.org/schema/xfa-events/1.0" xfe:script="$config.proto.common.template.uri='GenReq'" xfe:event="$config:load">
                     <?jetform ^Dat ^page 1?>
                     <FR_NAME>Administrator</FR_NAME>
                     <JFWF_DELEGATE />
                     <ADHOC_DLN_ACTOR />
                     <ADHOC_DLN_MSG />
                         <ADHOC_DLN_TIME />
                     <ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>
                     <ADHOC_RMD_MSG />
                     <ADHOC_RMD_TIME />
                     <ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>
                     <ADHOC_RPT_TIME />
                     <ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>
                     <CIRCULATETO />
                     <COMPLETION />
                     <FOLLOWUP />
                     <MSGSUBJECT />
                     <OTHERFIELD />
                     <PRIORITY>Low</PRIORITY>
                     <REQUEST />
                     <RESPONSE />
                     <Submit />
                     <ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>
                     <JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>
                     <JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>
                     <JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>
                     <JFWF_STATUS>RECEIVED</JFWF_STATUS>
                     <JFWF_ACTION />
                     <JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>
                     <JFWF_VERSION>6.2</JFWF_VERSION>
                     <JFWF_READONLY>1</JFWF_READONLY>
                  </data>
               </xfa:DataGroup>
            </xfa:Data>
         </jfxpf:Content>
      </jfxpf:Resource>
   </jfxpf:Package>
</jfxpf:XPF>

However, I am having trouble finding the text that is causing this issue. My Java code is below:

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                    .parse(new InputSource(new StringReader(xml)));

EDIT Removing the Data node works, so the error is somewhere deep in the XML. This does not throw an error:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<?xfa generator=\"ff99v250_01\" APIVersion=\"1.4.3139.0\"?>
<jfxpf:XPF xmlns:jfxpf=\"http://www.xfa.com/schema/xml-package\">
    <jfxpf:Package>
        <jfxpf:Resource Location=\"GenReq\">
            <jfxpf:Link ContentType=\"application/x-jetform-cft\"/>
        </jfxpf:Resource>
        <jfxpf:Resource Location=\"default.xml\">
            <jfxpf:Content ContentType=\"text/xml\" Location=\"default.xml\">
            </jfxpf:Content>
        </jfxpf:Resource>
    </jfxpf:Package>
 </jfxpf:XPF>

My Imports

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JFileChooser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
Community
  • 1
  • 1
Jon
  • 3,174
  • 11
  • 39
  • 57
  • So you say the XML you posted works, right? Well, without seeing the XML that doesn't work that you didn't post it's hard to tell. But did you check, using the EBNF-Grammar of XML as per http://www.w3.org/TR/REC-xml/ whether your string actually is well-formed? – Christian Hujer Dec 02 '14 at 09:44
  • Sorry, this is the XML that is *not* working. I managed to get some other XML working that simply contained the bare essentials. I have validated this XML and it didn't come back with any issues. – Jon Dec 02 '14 at 09:47
  • According the [W3 Validator](http://validator.w3.org/check) it is valid XML. Maybe the ?xfa prolog is not accepted by the implementation. – Thomas Weller Dec 02 '14 at 09:53

2 Answers2

4

My guess is that the file starts with a BOM character U+FEFF: error at line 1, column 1.This is a zero-width space used sometimes to mark a file as being in some Unicode representation, UTF-8, UTF-16LE, UTF-16BE.

The BOM character can be removed. Check the file size, and then look what options you have: save as UTF-8 without BOM, delete.

In java (should the editor be stubborn):

Path path = Paths.get(".... .xml");
byte[] content = Files.readAllBytes(path);
String s = new String(content, StandardCharsets.UTF_8);
s = s.replaceFirst("^\uFEFF", "");
byte[] content2 = s.getBytes(StandardCharsets.UTF_8);
if (content2.length != content.length) {
    Files.write(path, content2);
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

The document and sample code you provided works fine in Java 1.8u25:

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class FatalErrorTest
{

    @Test
    public void as_given() throws SAXException, IOException, ParserConfigurationException
    {
        String xml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<?xfa generator=\"ff99v250_01\" APIVersion=\"1.4.3139.0\"?>\r\n<jfxpf:XPF xmlns:jfxpf=\"http://www.xfa.com/schema/xml-package\">\r\n   <jfxpf:Package>\r\n      <jfxpf:Resource Location=\"GenReq\">\r\n         <jfxpf:Link ContentType=\"application/x-jetform-cft\" />\r\n      </jfxpf:Resource>\r\n      <jfxpf:Resource Location=\"default.xml\">\r\n         <jfxpf:Content ContentType=\"text/xml\" Location=\"default.xml\">\r\n            <xfa:Data xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\">\r\n               <xfa:DataGroup>\r\n                  <data xmlns:xfe=\"http://www.xfa.org/schema/xfa-events/1.0\" xfe:script=\"$config.proto.common.template.uri='GenReq'\" xfe:event=\"$config:load\">\r\n                     <?jetform ^Dat ^page 1?>\r\n                     <FR_NAME>Administrator</FR_NAME>\r\n                     <JFWF_DELEGATE />\r\n                     <ADHOC_DLN_ACTOR />\r\n                     <ADHOC_DLN_MSG />\r\n                         <ADHOC_DLN_TIME />\r\n                     <ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>\r\n                     <ADHOC_RMD_MSG />\r\n                     <ADHOC_RMD_TIME />\r\n                     <ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>\r\n                     <ADHOC_RPT_TIME />\r\n                     <ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>\r\n                     <CIRCULATETO />\r\n                     <COMPLETION />\r\n                     <FOLLOWUP />\r\n                     <MSGSUBJECT />\r\n                     <OTHERFIELD />\r\n                     <PRIORITY>Low</PRIORITY>\r\n                     <REQUEST />\r\n                     <RESPONSE />\r\n                     <Submit />\r\n                     <ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>\r\n                     <JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>\r\n                     <JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>\r\n                     <JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>\r\n                     <JFWF_STATUS>RECEIVED</JFWF_STATUS>\r\n                     <JFWF_ACTION />\r\n                     <JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>\r\n                     <JFWF_VERSION>6.2</JFWF_VERSION>\r\n                     <JFWF_READONLY>1</JFWF_READONLY>\r\n                  </data>\r\n               </xfa:DataGroup>\r\n            </xfa:Data>\r\n         </jfxpf:Content>\r\n      </jfxpf:Resource>\r\n   </jfxpf:Package>\r\n</jfxpf:XPF>";
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(xml)));
        assertNotNull(doc);
    }

}
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222