致命错误 - prolog中不允许使用内容

时间:2014-12-02 09:35:16

标签: java xml domdocument

Similar to this question, but unfortunately didn't help

我正在尝试用Java解析String到XML并继续收到错误:

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

我知道它必须与我的XML字符串有关,因为我使用非常基本的XML运行测试并且错误消失了。

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>

但是,我无法找到导致此问题的文本。我的Java代码如下:

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

修改 删除Data节点有效,因此错误位于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\">
            </jfxpf:Content>
        </jfxpf:Resource>
    </jfxpf:Package>
 </jfxpf:XPF>

我的进口

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;

2 个答案:

答案 0 :(得分:4)

我的猜测是文件以BOM字符U + FEFF开头:第1行第1列错误。这是一个零宽度空间,有时用于将文件标记为某种Unicode表示形式UTF-8, UTF-16LE,UTF-16BE。

可以删除BOM字符。检查文件大小,然后查看您拥有的选项:保存为没有BOM的UTF-8,删除。

在java中(编辑应该固执):

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);
}

答案 1 :(得分:1)

您提供的文档和示例代码在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);
    }

}