3

私は次のクラスを持っています:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "item", propOrder = {
    "content"
})
public class Item {

    @XmlElementRefs({
        @XmlElementRef(name = "ruleref", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "tag", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "one-of", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "item", type = JAXBElement.class, required = false)
    })    
    @XmlMixed
    protected List<Serializable> content;

要素には、次のような引用符を含む文字列を含めることができます。

<tag>"some kind of text"</tag>

さらに、item 要素自体に、引用符を含む文字列を含めることができます。

<item>Some text, "this has string"</item>

Moxy を使用して生成された XML は、タグ要素とアイテム要素のテキスト値をエスケープします。

<tag>&quote;some kind of text&quote;</tag>

どうすればそれを防ぐことができますが、これらの要素でのみですか? 属性やその他の要素はそのままにしておく必要があります (つまり、エスケープします)。

ありがとうございました。

4

1 に答える 1

3

独自のCharacterEscapeHandler.

Java モデル

フー

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Foo {

    private String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

}

デモコード

デモ

import java.io.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;
import org.eclipse.persistence.oxm.CharacterEscapeHandler;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Foo foo = new Foo();
        foo.setBar("\"Hello World\"");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);

        marshaller.setProperty(MarshallerProperties.CHARACTER_ESCAPE_HANDLER, new CharacterEscapeHandler() {

            @Override
            public void escape(char[] buffer, int start, int length,
                    boolean isAttributeValue, Writer out) throws IOException {
                out.write(buffer, start, length);
            }

        });

        marshaller.marshal(foo, System.out);
    }

}

出力

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <bar>&quot;Hello World&quot;</bar>
</foo>
<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <bar>"Hello World"</bar>
</foo>
于 2013-10-23T20:30:12.737 に答える