3

私は次のXMLを持っています:

<?xml version="1.0" encoding="UTF-8"?>
<details>
  ...
  <address1>Test&amp;Address</address1>
  ...
</details>

JAXBを使用してマーシャリングを解除しようとすると、次の例外がスローされます。

Caused by: org.xml.sax.SAXParseException: The reference to entity "Address" must end with the ';' delimiter.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)

しかし&amp;、XMLのをに変更すると&apos;、機能します。問題はアンパサンドだけにあるようで、&amp;理由がわかりません。

マーシャリングを解除するコードは次のとおりです。

JAXBContext context = JAXBContext.newInstance("some.package.name", this.getClass().getClassLoader());
Unmarshaller unmarshaller = context.createUnmarshaller();
obj = unmarshaller.unmarshal(new StringReader(xml));

誰かが洞察を持っていますか?

編集:私は以下の@ abhin4vによって提案された解決策を試しました(つまり、後にスペースを追加します&amp;)が、それもうまくいかないようです。スタックトレースは次のとおりです。

Caused by: org.xml.sax.SAXParseException: The entity name must immediately follow the '&' in the entity reference.
        at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
        at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
        at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEntityReference(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
        at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
        at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
        at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
        at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:194)
4

3 に答える 3

3

Xercesはに変換&amp;して&から解決しようとしますが、。&Addressで終わっていないため失敗し;ます。との間にスペースを入れる&Address、機能するはずです。Xercesは&、OPで指定された2番目のエラーを解決してスローしようとするため、スペースを入れることはできません。テストをCDATAセクションでラップすることができ、Xercesはエンティティを解決しようとしません。

于 2010-06-08T16:28:36.863 に答える
3

私もこれに遭遇しました。最初のパスでは、&ampをトークン文字列(AMPERSAND_TOKEN)に置き換え、JAXBを介して送信してから、アンパサンドを再置き換えしました。理想的ではありませんが、簡単な修正でした。

2回目のパスで多くの重要な変更を加えたため、何が問題を正確に解決したのかわかりません。html dtdsへのJAXBアクセスを提供することで、はるかに幸せになったと思いますが、それは単なる推測であり、私のプロジェクトに固有のものである可能性があります。

HTH

于 2010-06-08T16:31:20.473 に答える
1

問題は、私が使用しているフレームワーク(Mentawaiフレームワーク)が原因であることがわかりました。上記のXMLは、HTTPリクエストのPOST本文から取得されます。

どうやら、フレームワークはXML本体の文字エンティティを変換するため、に&amp;なり&、アンマーシャラーはXMLのアンマーシャリングに失敗します。

于 2010-06-08T19:39:13.190 に答える