2011-10-19 44 views
0

我讀過你寫的關於一個帖子:蓖麻編組抑制XSI

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); 

但我得到了什麼仍是xmlns:xsi=..xsi:type=.. XML標記內。

我做錯了什麼?我正在使用castor xml 1.3.2。

回答

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> 
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);