Castor Marshaller 抑制 xsi

发布于 2024-12-10 17:20:16 字数 493 浏览 0 评论 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=..

我做错了什么吗?我正在使用 Castor xml 1.3.2。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风蛊 2024-12-17 17:20:16

如果您使用字符串编写器创建编组器,那么问题就会消失。

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

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);
╭⌒浅淡时光〆 2024-12-17 17:20:16

这也是我所做的,并且对我有用。这是示例,希望有帮助:

:

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:castor.properties

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

// ...getters and setters
}

MarshallerTest.java

org.exolab.castor.indent=true

输出:

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文