「standalone =“ yes”」を削除生成されたXMLから
-
07-07-2019 - |
質問
結果のXMLに standalone =" yes" が生成されないようにするJAXB設定を知っていますか?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
解決
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
なしで使用できます
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
ただし、このベストプラクティスは検討しません。
他のヒント
JDK1.6の一部であるJAXBで
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
次のいずれかを使用できます
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
または
marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE)
デフォルトのXML宣言を無効にしてから、カスタムXML宣言を追加します
<?xml version="1.0" encoding="UTF-8"?>
by
marshaller.setProperty("com.sun.xml.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
生成されたxmlへ。したがって、 standalone =&quot; yes&quot; プロパティを回避します。
他の誰かがまだこの問題に苦労している場合は、使用を検討することができます
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
すべてのXML宣言を削除し、出力ストリーム/メソッドの先頭に独自の String
を記述するだけです
DOCTYPE
に依存するドキュメントを作成する場合(名前付きエンティティを使用する場合など)、スタンドアロンではなくなり、 standalone =&quot; yes&quot;
は許可されませんXML宣言。
ただし、スタンドアロンXMLはどこでも使用できますが、非スタンドアロンは外部をロードしないXMLパーサーにとって問題があります。
XMLをサポートしていないが恐ろしい正規表現のスープをサポートしているソフトウェアとの相互運用性を除いて、この宣言がどのように問題になるかわかりません。
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
これはJDK1.7でうまくいきました。 standalone = \&quot; no \&quot;削除して残りのxml部分のみを取得できます
デフォルトのjavax.xmlパッケージのみを使用している場合、マーシャラーのJAXB_FRAGMENTオプションを「true」に設定し(これにより、デフォルトのxml処理命令が省略されます)、XMLStreamWriterのwriteProcessingInstructionメソッドを使用して独自のものを挿入できます。
xmlStreamWriter.writeProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
jaxbMarshaller.setProperty( Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
jaxbMarshaller.marshal(object, xmlStreamWriter);
xmlStreamWriter.writeEndDocument();
次を使用できます。 marshaller.setProperty(&quot; jaxb.fragment&quot ;, Boolean.TRUE);
Java 8で動作します
プロパティの例外が発生する場合は、次の構成を追加します:
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders",
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlDeclaration", Boolean.FALSE);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
十分な「評判」がありません。 &quot; privilege&quot;コメントします。 ;-)
@Debasis、指定したプロパティに注意してください:
"com.sun.xml.internal.bind.xmlHeaders"
はずです:
"com.sun.xml.bind.xmlHeaders" (without the "internal", which are not meant to be used by the public)
「内部」を使用する場合あなたがやったように、私は javax.xml.bind.PropertyException
を取得します