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



类似于这个问题,但不幸的是没有帮助

我正在尝试在 Java 中将字符串解析为 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;

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

可以删除 BOM 字符。检查文件大小,然后查看您有哪些选项:另存为 UTF-8 而不带 BOM,删除。

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

您提供的文档和示例代码在 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"?>rn<?xfa generator="ff99v250_01" APIVersion="1.4.3139.0"?>rn<jfxpf:XPF xmlns:jfxpf="http://www.xfa.com/schema/xml-package">rn   <jfxpf:Package>rn      <jfxpf:Resource Location="GenReq">rn         <jfxpf:Link ContentType="application/x-jetform-cft" />rn      </jfxpf:Resource>rn      <jfxpf:Resource Location="default.xml">rn         <jfxpf:Content ContentType="text/xml" Location="default.xml">rn            <xfa:Data xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">rn               <xfa:DataGroup>rn                  <data xmlns:xfe="http://www.xfa.org/schema/xfa-events/1.0" xfe:script="$config.proto.common.template.uri='GenReq'" xfe:event="$config:load">rn                     <?jetform ^Dat ^page 1?>rn                     <FR_NAME>Administrator</FR_NAME>rn                     <JFWF_DELEGATE />rn                     <ADHOC_DLN_ACTOR />rn                     <ADHOC_DLN_MSG />rn                         <ADHOC_DLN_TIME />rn                     <ADHOC_DLN_UNITS>Days</ADHOC_DLN_UNITS>rn                     <ADHOC_RMD_MSG />rn                     <ADHOC_RMD_TIME />rn                     <ADHOC_RMD_UNITS>Days</ADHOC_RMD_UNITS>rn                     <ADHOC_RPT_TIME />rn                     <ADHOC_RPT_UNITS>Days</ADHOC_RPT_UNITS>rn                     <CIRCULATETO />rn                     <COMPLETION />rn                     <FOLLOWUP />rn                     <MSGSUBJECT />rn                     <OTHERFIELD />rn                     <PRIORITY>Low</PRIORITY>rn                     <REQUEST />rn                     <RESPONSE />rn                     <Submit />rn                     <ADHOC_VALIDDATA>True</ADHOC_VALIDDATA>rn                     <JFWF_TRANID>2xxyg9sffane7pwd5j8yv9t49s.1</JFWF_TRANID>rn                     <JFWF_INSTRUCTION>Initiate a General Request. Fill the request form, then identify the next participant.</JFWF_INSTRUCTION>rn                     <JFWF_TRANSPORT>HTTP</JFWF_TRANSPORT>rn                     <JFWF_STATUS>RECEIVED</JFWF_STATUS>rn                     <JFWF_ACTION />rn                     <JFWF_CHOICE>*Select Next Participant,Cancel</JFWF_CHOICE>rn                     <JFWF_VERSION>6.2</JFWF_VERSION>rn                     <JFWF_READONLY>1</JFWF_READONLY>rn                  </data>rn               </xfa:DataGroup>rn            </xfa:Data>rn         </jfxpf:Content>rn      </jfxpf:Resource>rn   </jfxpf:Package>rn</jfxpf:XPF>";
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(new InputSource(new StringReader(xml)));
        assertNotNull(doc);
    }
}

最新更新