簡単なタスク:要素に2つの属性を記述します。
String nsURI = "http://example.com/";
XMLOutputFactory outF = XMLOutputFactory.newFactory();
outF.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
XMLStreamWriter out = outF.createXMLStreamWriter(System.out);
out.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "element", nsURI);
out.writeAttribute("attribute", "value");
out.writeAttribute("attribute2", "value");
out.writeEndElement();
out.close();
ウッドストックスの答え:
<element xmlns="http://example.com/" attribute="value" attribute2="value"></element>
JDK 6の回答:
<zdef-1905523464:element xmlns="" xmlns:zdef-1905523464="http://example.com/" attribute="value" attribute2="value"></zdef-1905523464:element>
何?!
さらに、要素にプレフィックスを追加すると、次のようになります。
out.writeStartElement("ns", "element", nsURI);
JDK6はxmlns=""を発行しようとしなくなりました:
<ns:element xmlns:ns="http://example.com/" attribute="value" attribute2="value"></ns:element>
そして、属性を削除した場合(つまり、属性が1つしかない場合)は問題ありません。
これはJDK6のバグだと確信しています。そして、誰かが両方のライブラリ(および他のライブラリ)を幸せに保つ回避策を提案できますか?私がそれを助けることができるならば、私はwoodstoxを要求したくありません。