2

好的,这似乎是一个常见的错误消息,但我从几个角度看了这个,我很困惑。

我的 XML(完整的文件很大,因此限制在似乎相关的区域,除非有人可以告诉我如何/为什么我需要更多):

<?xml version="1.0" encoding="utf-8"?>
<MyXML date="201112102200" type="daily">
    <CountryList>
        <CountryName code="AARCT" name="Antarctica" IsTerritory="True"/>
        <CountryName code="ABKHAZ" name="Abkhazia" IsTerritory="True"/>

        <!-- ... -->

        <CountryName code="VCAN" name="Vatican City" IsTerritory="False" ProfileURL="vatican city.doc"/>
        <CountryName code="VEN" name="Venezuela" IsTerritory="False" ProfileURL="venezuela.doc"/>
        <CountryName code="VI" name="US Virgin Islands" IsTerritory="True"/>
        <CountryName code="VIETN" name="Vietnam" IsTerritory="False" ProfileURL="vietnam.doc"/>
        <CountryName code="WALLIS" name="Wallis and Futuna Islands" IsTerritory="True"/>
        <CountryName code="WSOMOA" name="Samoa" IsTerritory="False" ProfileURL="samoa.doc"/>
        <CountryName code="YEMAR" name="Yemen" IsTerritory="False" ProfileURL="yemen.doc"/>
        <CountryName code="YUG" name="Serbia" IsTerritory="False" ProfileURL="serbia.doc"/>
        <CountryName code="ZAIRE" name="Democratic Republic of the Congo" IsTerritory="False" ProfileURL="democratic republic of the congo.doc"/>
        <CountryName code="ZAMBIA" name="Zambia" IsTerritory="False" ProfileURL="zambia.doc"/>
        <CountryName code="ZIMBAB" name="Zimbabwe" IsTerritory="False" ProfileURL="zimbabwe.doc"/>
    </CountryList>

    <!-- ... -->

</MyXML>

所以错误是:

org.xml.sax.SAXParseException: Element type "CountryNamecode" must be followed by either attribute specifications, ">" or "/>".
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1231)
    at com.foo.bar.Baz.<init>(Baz.java:38)
    at com.foo.bar.BazTest.testRecordCounts(BazTest.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我的日志记录表明它在国家代码“VI”之后失败,并且“VIETN”条目有问题。

因此,似乎没有一个名为“CountryNamecode”的格式错误的元素,我已经检查了不可靠的字符,但这都是非常普通的字符。整个文件在我使用 STS、Oxygen 和 xmllint 检查后验证。

对此的任何帮助将不胜感激。

干杯,伙计们!

编辑:

XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(this);
xmlReader.setErrorHandler(this);
xmlReader.parse(new InputSource(new StringReader(retriever.getContent())));

检索器对象正在返回 xml 字符串,除此之外,我正在解析 InputSource,并将其传递给 StringReader。除非有什么明显的东西我错过了

4

1 回答 1

1

我准备打赌问题出在底层数据流代码中。

为了支持我的理论,打开原始数据文件,将光标移动到 和 之间的空格CountryNamecode然后找到一种方法来确定该空格字符在文件中的确切偏移量。它可能是 1024 的精确倍数,也可能是 4096 或 8192。

然后查看用于提供 SAX 解析器的InputSourceor代码。Reader它可能看起来像:

sax = factory.newSAXParser();
try {
  // Here I am using an InputSource wrapping a StringReader.
  sax.parse(new InputSource(new StringReader(xml)), this);
} catch (SAXException ex) {
  log.warning("XMLParser failed on: "+xml, ex);
}

我怀疑您使用的任何东西而不是new InputSource(new StringReader(xml))我上面使用的东西都会破坏数据。

于 2012-02-27T15:33:48.413 回答