gpt4 book ai didi

java - StaX:序言中不允许出现内容

转载 作者:行者123 更新时间:2023-12-01 12:54:20 28 4
gpt4 key购买 nike

我有以下(测试)XML 文件和使用 StaX 的 Java 代码。我想将此代码应用于大约 30 GB 大但元素相当小的文件,所以我认为 StaX 是一个不错的选择。我收到以下错误:

线程“main”中出现异常 javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] 消息:序言中不允许出现内容 在 com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:598) 在 at.tuwien.mucke.util.xml.staxtest.StaXTest.main(StaXTest.java:18) 在 sun.reflect.NativeMethodAccessorImpl.invoke0( native 方法) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:601) 在 com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

<?xml version='1.0' encoding='utf-8'?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<price>44.95</price>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<price>5.95</price>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>

代码如下:

package xml.staxtest;

import java.io.*;
import javax.xml.stream.*;

public class StaXTest {

public static void main(String[] args) throws Exception {

XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader streamReader = xif.createXMLStreamReader(new FileReader("D:/Data/testFile.xml"));

while(streamReader.hasNext()){
int eventType = streamReader.next();

if(eventType == XMLStreamReader.START_ELEMENT){
System.out.println(streamReader.getLocalName());
}

//... more to come here later ...
}
}

}

最佳答案

解决了!

我在定义<?xml version="1.0" encoding="ISO-8859-1" ?>中添加了编码我必须将其存储为 ANSI(因为 Notepad++ 假定为 UTF-8)。愚蠢的!

关于java - StaX:序言中不允许出现内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24010946/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com