Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从多个属性中,我需要获取其中一个属性,并使用jaxb以xml字符串形式打印_Java_Xml_Xml Parsing_Jaxb_Marshalling - Fatal编程技术网

Java 从多个属性中,我需要获取其中一个属性,并使用jaxb以xml字符串形式打印

Java 从多个属性中,我需要获取其中一个属性,并使用jaxb以xml字符串形式打印,java,xml,xml-parsing,jaxb,marshalling,Java,Xml,Xml Parsing,Jaxb,Marshalling,如何使用以下类中的JAXB在Xml字符串中单独获取一个属性: 例如: public class Student { private Integer age; private String name; private Integer id; public void setAge(Integer age) { this.age = age; } public Integer getAge() { return age

如何使用以下类中的JAXB在Xml字符串中单独获取一个属性

例如:

public class Student {
    private Integer age;
    private String name;
    private Integer id;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}
XMLString中的预期输出为:

<?xml version="1.0"?>
<student_list>
        <id = “1234”/>
        <id = “4567”/>
</student_list>

我需要与上面表示的格式相同的xml字符串。

Option#1-
@xmltransive
您可以使用
@xmltransive
映射不希望在XML中显示的属性

public class Student {
    private Integer age;
    private String name;
    private Integer id;

    @XmlTransient
    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    @XmlTrasient
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

@XmlAttribute
public void setId(Integer id) {
    this.id = id;
    }

    public Integer getId() {
        return id;
    }
}
选项2-
@xmlacessortype(xmlacesstype.NONE)
或者,您可以使用
@xmlacessortype(xmlacesstype.NONE)
对类进行注释,以便XML中只包含带注释的属性

@XmlAccessorType(XmlAccessType.NONE)
public class Student {
    private Integer age;
    private String name;
    private Integer id;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @XmlAttribute
    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}
选项1-
@xmltransive
您可以使用
@xmltransive
映射不希望在XML中显示的属性

public class Student {
    private Integer age;
    private String name;
    private Integer id;

    @XmlTransient
    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    @XmlTrasient
    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

@XmlAttribute
public void setId(Integer id) {
    this.id = id;
    }

    public Integer getId() {
        return id;
    }
}
选项2-
@xmlacessortype(xmlacesstype.NONE)
或者,您可以使用
@xmlacessortype(xmlacesstype.NONE)
对类进行注释,以便XML中只包含带注释的属性

@XmlAccessorType(XmlAccessType.NONE)
public class Student {
    private Integer age;
    private String name;
    private Integer id;

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @XmlAttribute
    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }
}

使用JAXB。。。您需要为xml输出生成xsd。然后可以从XSD生成JAXB绑定类。见下面的例子。然后使用JAXB封送/解封送从xml获取bean,反之亦然。有一个例子:

输出示例:

<student_list>
    <student id="123">
        <age>25</age>
        <name>Hello Pelo</name>
    </student>
    <student id="124">
        <age>26</age>
        <name>Hello Selo</name>
    </student>
</student_list>
StudentType.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "student_listType", propOrder = {
    "student"
})
public class StudentListType {

    @XmlElement(required = true)
    protected StudentType student;

    /**
     * Gets the value of the student property.
     * 
     * @return
     *     possible object is
     *     {@link StudentType }
     *     
     */
    public StudentType getStudent() {
        return student;
    }

    /**
     * Sets the value of the student property.
     * 
     * @param value
     *     allowed object is
     *     {@link StudentType }
     *     
     */
    public void setStudent(StudentType value) {
        this.student = value;
    }

}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "studentType", propOrder = {
    "age",
    "name"
})
public class StudentType {

    @XmlElement(required = true)
    protected String age;
    @XmlElement(required = true)
    protected String name;
    @XmlAttribute
    protected String id;

    /**
     * Gets the value of the age property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAge() {
        return age;
    }

    /**
     * Sets the value of the age property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAge(String value) {
        this.age = value;
    }

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setId(String value) {
        this.id = value;
    }

}

使用JAXB。。。您需要为xml输出生成xsd。然后可以从XSD生成JAXB绑定类。见下面的例子。然后使用JAXB封送/解封送从xml获取bean,反之亦然。有一个例子:

输出示例:

<student_list>
    <student id="123">
        <age>25</age>
        <name>Hello Pelo</name>
    </student>
    <student id="124">
        <age>26</age>
        <name>Hello Selo</name>
    </student>
</student_list>
StudentType.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "student_listType", propOrder = {
    "student"
})
public class StudentListType {

    @XmlElement(required = true)
    protected StudentType student;

    /**
     * Gets the value of the student property.
     * 
     * @return
     *     possible object is
     *     {@link StudentType }
     *     
     */
    public StudentType getStudent() {
        return student;
    }

    /**
     * Sets the value of the student property.
     * 
     * @param value
     *     allowed object is
     *     {@link StudentType }
     *     
     */
    public void setStudent(StudentType value) {
        this.student = value;
    }

}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "studentType", propOrder = {
    "age",
    "name"
})
public class StudentType {

    @XmlElement(required = true)
    protected String age;
    @XmlElement(required = true)
    protected String name;
    @XmlAttribute
    protected String id;

    /**
     * Gets the value of the age property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getAge() {
        return age;
    }

    /**
     * Sets the value of the age property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setAge(String value) {
        this.age = value;
    }

    /**
     * Gets the value of the name property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the value of the name property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setName(String value) {
        this.name = value;
    }

    /**
     * Gets the value of the id property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getId() {
        return id;
    }

    /**
     * Sets the value of the id property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setId(String value) {
        this.id = value;
    }

}
无效。它应该是
1234
无效。它应该是
1234