0

あなたが書いた次の記事を読みました。

Marshaller marshaller = new Marshaller(w);
marshaller.setSuppressXSIType(true);

問題は、私がその方法を使用していることですが、結果は変わりませんでした。

私のコードは次のとおりです。

Marshaller m = new Marshaller(); 
m.setSuppressXSIType(true);
m.setSuppressNamespaces(true); 
m.setSupressXMLDeclaration(true);
m.setMarshalExtendedType(false);
m.marshal(obj, file);

しかし、私が取得したのは、xml タグ内のxmlns:xsi=..とです。xsi:type=..

私は何か間違ったことをしていますか?私はキャスターxml 1.3.2を使用しています。

4

2 に答える 2

1

文字列ライターでマーシャラーを作成すると、問題はなくなります。

StringWriter st = new StringWriter(); 
Marshaller marshaller = new Marshaller(st);

ただし、以下を実行すると機能しません。

Marshaller marshaller = new Marshaller();
marshaller.setValidation(true);
marshaller.setSuppressXSIType(true);             
marshaller.setSuppressNamespaces(true);
marshaller.setSupressXMLDeclaration(true);
marshaller.setMapping(mapping);

marshaller.marshal(order,st);
于 2012-07-01T05:19:41.623 に答える
0

それは私もやったことであり、私にとってはうまくいきました。これが例です。お役に立てば幸いです:

MarshallerTest.java:

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;

public class MarshallerTest {

    public static void main(String[] args) throws IOException, MappingException, MarshalException, ValidationException {
        Mapping mapping = new Mapping();
        mapping.loadMapping(MarshallerTest.class.getResource("/mapping.xml"));
        StringWriter sw = new StringWriter();
        Marshaller marshaller = new Marshaller(sw);
        marshaller.setMapping(mapping);
        marshaller.setSuppressNamespaces(true);
        marshaller.setSuppressXSIType(true);

        Person alex = new Person();
        alex.setName("alex");
        alex.setHobbies(Arrays.asList(new String[]{"fishing", "hiking"}));
        marshaller.marshal(alex);
        System.out.println(sw.toString());
    }
}

Person.java:

public class Person {
    private String name;
    private List<String> hobbies;

// ...getters and setters
}

castor.properties

org.exolab.castor.indent=true

出力:

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <hobbies>fishing</hobbies>
    <hobbies>hiking</hobbies>
    <name>alex</name>
</person>
于 2012-02-26T20:34:58.890 に答える