3

是否可以在 XML 中隐藏一个值,以便只有在解组时才能检索它。

4

2 回答 2

2

注意: 我是EclipseLink JAXB (MOXy)负责人,也是JAXB (JSR-222)专家组的成员。

如果您使用 MOXy 作为您的 JAXB 提供程序,以下是一些用于实现此用例的选项。要将 MOXy 用作您的 JAXB 提供程序,您需要包含一个jaxb.properties与您的域模型在同一包中命名的文件,其中包含以下条目:

选项1 -XmlAdapter

AnXmlAdapter可用于在封送操作期间清空属性值。虽然XmlAdapter是标准 JAXB 类,但从marshal方法返回 null 会导致在使用 JAXB 参考实现时发生异常。

字符串适配器

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class StringAdapter extends XmlAdapter<String, String> {

    @Override
    public String marshal(String string) throws Exception {
        return null;
    }

    @Override
    public String unmarshal(String string) throws Exception {
        return string;
    }

}

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name="Person")
public class Person {

    String password;

    @XmlJavaTypeAdapter(StringAdapter.class)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

选项 #2 - 没有吸气剂的二传手

当 MOXy 用作 JAXB 提供程序时,如果您的属性带有带注释的 setter 而没有 getter,则 MOXy 会将其视为只读属性。

import javax.xml.bind.annotation.*;

@XmlRootElement(name="Person")
public class Person {

    String password;

    @XmlElement
    public void setPassword(String password) {
        this.password = password;
    }

}

选项 #3 - MOXy 的@XmlReadOnly扩展

MOXy 的@XmlReadOnly扩展也可用于将属性标记为只读。

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlReadOnly;

@XmlRootElement(name="Person")
public class Person {

    String password;

    @XmlReadOnly
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

演示代码

输入.xml

<Person>
    <password> some password </password>
</Person>

*演示*

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14231799/input.xml");
        Person person = (Person) unmarshaller.unmarshal(xml);

        System.out.println(person.password);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(person, System.out);
    }

}

输出

 some password 
<?xml version="1.0" encoding="UTF-8"?>
<Person/>
于 2013-01-10T10:33:07.880 回答
1

以下方法可用于任何JAXB (JSR-222)实现。

演示

AMarshaller.Listener可用于在编组对象之前清空一个值,然后再将其恢复。

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Person.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum14231799/input.xml");
        Person person = (Person) unmarshaller.unmarshal(xml);

        System.out.println(person.password);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setListener(new Marshaller.Listener() {

            private String password;

            @Override
            public void afterMarshal(Object object) {
                if(object instanceof Person) {
                    Person person = (Person) object;
                    person.setPassword(password);
                    password = null;
                }
            }

            @Override
            public void beforeMarshal(Object object) {
                if(object instanceof Person) {
                    Person person = (Person) object;
                    password = person.getPassword();
                    person.setPassword(null);
                }
            }
        });
        marshaller.marshal(person, System.out);

        System.out.println(person.password);
    }

}

输入.xml

<Person>
    <password> some password </password>
</Person>

输出

 some password 
<?xml version="1.0" encoding="UTF-8"?>
<Person/>
 some password 
于 2013-01-10T10:53:52.177 回答