castor marshaller suppress xsi

1.7k views Asked by At

I read a post written by you about:

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

The problem is that I'm using that method but the result didn't changed.

My code is :

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

But what I obtained is still the xmlns:xsi=.. and the xsi:type=.. inside the xml tag.

Am I doing something wrong? I'm using castor xml 1.3.2.

2

There are 2 answers

0
shahal On

if you create the marshaller with the string writer then the problem goes away.

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

However, if you do the below it wont work.

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

marshaller.marshal(order,st);
0
acorello On

That's what I've done as well and it worked for me. Here is the example, hope it helps:

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

output:

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <hobbies>fishing</hobbies>
    <hobbies>hiking</hobbies>
    <name>alex</name>
</person>