我读了你写的关于的帖子:
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=..
。
我做错了吗?我正在使用castor xml 1.3.2。
答案 0 :(得分: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);
答案 1 :(得分: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>