首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >castor marshaller抑制xsi

castor marshaller抑制xsi
EN

Stack Overflow用户
提问于 2011-10-19 16:50:10
回答 2查看 1.7K关注 0票数 0

我读了你写的一篇文章,内容是:

代码语言:javascript
复制
Marshaller marshaller = new Marshaller(w);
marshaller.setSuppressXSIType(true);

问题是,我正在使用这种方法,但结果并没有改变。

我的代码是:

代码语言:javascript
复制
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。

EN

回答 2

Stack Overflow用户

发布于 2012-07-01 13:19:42

如果使用字符串编写器创建编组程序,那么问题就解决了。

代码语言:javascript
复制
StringWriter st = new StringWriter(); 
Marshaller marshaller = new Marshaller(st);

但是,如果你这样做,它不会工作。

代码语言:javascript
复制
Marshaller marshaller = new Marshaller();
marshaller.setValidation(true);
marshaller.setSuppressXSIType(true);             
marshaller.setSuppressNamespaces(true);
marshaller.setSupressXMLDeclaration(true);
marshaller.setMapping(mapping);

marshaller.marshal(order,st);
票数 1
EN

Stack Overflow用户

发布于 2012-02-27 04:34:59

这也是我所做的,它对我很有效。下面是一个例子,希望对你有所帮助:

MarshallerTest.java:

代码语言:javascript
复制
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:

代码语言:javascript
复制
public class Person {
    private String name;
    private List<String> hobbies;

// ...getters and setters
}

castor.properties

代码语言:javascript
复制
org.exolab.castor.indent=true

输出:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<person>
    <hobbies>fishing</hobbies>
    <hobbies>hiking</hobbies>
    <name>alex</name>
</person>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7818774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档