java-读取大型xml文件并获取元素的字节位置

java-读取大型xml文件并获取元素的字节位置,java,xml,Java,Xml,我正在使用 javax.xml.stream.XMLInputFactory 浏览13GB维基百科xml文件 现在我想知道标记行从哪个字节位置开始,这样我就可以跳到那里读取它了 下面是一些代码: inputStream = new FileInputStream(xmlFile); // I am free to change this XMLInputFactory inputFactory = XMLInputFactory.newInstance(); // maybe there is

我正在使用

javax.xml.stream.XMLInputFactory

浏览13GB维基百科xml文件

现在我想知道标记行从哪个字节位置开始,这样我就可以跳到那里读取它了

下面是一些代码:

inputStream = new FileInputStream(xmlFile); // I am free to change this

XMLInputFactory inputFactory = XMLInputFactory.newInstance(); // maybe there is a better way?
eventReader = inputFactory.createXMLEventReader(inputStream);


// this is in a loop
event = eventReader.nextEvent();

if (event.isStartElement()) {
    StartElement startElement = event.asStartElement();

    if (startElement.getName().getLocalPart() == "page") {
         // !!! here I want to know the byte position in the file
    }
}
我尝试的是:

inputStream.getChannel().position()


跳转到标签所在的位置并读取标签。但这不起作用,因为
eventReader
读取约8000字节的块。

您需要添加编码:

eventReader = inputFactory.createXMLEventReader(inputStream, "ASCII");

要了解元素在XML流中的来源,请调用
getLocation()


你不能用这个神奇的方式让XML读取过程在中间开始,一个XML文件必须按顺序读取。

见这个维基百科文件在UTF-8中,而不是ASCII。返回UTF-8索引,这意味着我需要先读取整个文件,然后才能知道字节索引。你有什么证据证明这一点?我尝试过了。当我设置通道位置时,索引变小了,XML读取器崩溃了。(第二个页面标记之前有UMLAUT)。在任何情况下,当周围有UMLAUT或任何其他非SCII文本时,您都会得到不正确的字符串。Location.getCharacterOffset()的文档与您的经验相矛盾。试试伍德斯托克?
eventReader = inputFactory.createXMLEventReader(inputStream, "ASCII");