2012-02-27 102 views
1

これは一般的なエラーメッセージのようですが、私は困った。SAXParseException:要素タイプ "CountryNamecode"の後に属性指定 ">"または "/>"が続く必要があります

マイ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を使用して、ファイルをチェックしたときにファイル全体が検証されます。

これに関するお手伝いがあれば幸いです。

乾杯!

EDIT:

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

レトリーバーのオブジェクトは、XML文字列を返し、それ以外のですが、私はそれをにStringReaderを渡し、のInputSourceを解析しています。明白でないものがない限り、私は紛失しています。

+0

xmlソースの 'CountryName'と' code'属性の間にスペースがないように聞こえます。 –

+0

それよりも少し隠されていましたが、それはソースファイルからの文字列の読み込みでした(下の回答のように)が、提案を歓迎しました。 – theZenPebble

答えて

1

私はその問題が基礎データストリームコードにあると考えています。

理論をサポートするには、元のデータファイルを開き、CountryNamecodeの間のスペースにカーソルを移動し、ファイル内のそのスペース文字の正確なオフセットを特定する方法を見つけます。 1024の倍数とおそらく4096または8192である可能性があります。

次に、SAXパーサーに使用するInputSourceまたは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))で使用しているものは何でも疑いがある:それはおそらくのようになります。

+0

こんにちはOldCurmudgeon、これは非常に妥当と思われますが、私のコードは大きく異なるわけではなく、Questionにコードスニペットを追加しました。 – theZenPebble

+0

@ pocon64 - あなたの編集から、 'retriever.getContent()'を調べてバッファ間で何ができるかを調べることをお勧めします。 – OldCurmudgeon

+0

ブルズアイ、私はそれを追加している間、私は文字列をトリミングしていた。乾杯! – theZenPebble

関連する問題