我在Javabean属性之前将 @XmlElement(name =“title”,required = true)
int some_property ,并且没有为 some_property 分配值。由于某种原因,在生成的XML中不会出现此属性。因此,请解释必需的含义

代码的一些有意义的部分:

@XmlRootElement(name = "book")
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {

private String name;
private String author;
private String publisher;
private String isbn;

// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "title",required = true)
public String getName() {
    return name;
}
....

int 的某处:
    // create books
    Book book1 = new Book();
    book1.setIsbn("978-0060554736");

    book1.setAuthor("Neil Strauss");
    book1.setPublisher("Harpercollins");
    bookList.add(book1);


    Book book2 = new Book();
    book2.setIsbn("978-3832180577");
    book2.setName("Feuchtgebiete");
    book2.setAuthor("Charlotte Roche");
    book2.setPublisher("Dumont Buchverlag");
    bookList.add(book2);

    JAXBContext context = JAXBContext.newInstance(Bookstore.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // Write to System.out
    m.marshal(bookstore, System.out);

    // Write to File
    m.marshal(bookstore, new File(BOOKSTORE_XML));

    // get variables from our xml file, created before
    System.out.println();
    System.out.println("Output from our XML File: ");
    Unmarshaller um = context.createUnmarshaller();
    Bookstore bookstore2 = (Bookstore) um.unmarshal(new FileReader(BOOKSTORE_XML));
    ArrayList<Book> list = bookstore2.getBooksList();

最佳答案

required@XmlElement的作用
required批注上的@XmlElement属性会影响从Java类生成的XML模式。

域模型(根)

下面是一个简单的Java模型。请注意bar属性如何具有required=true,而foo属性却没有。

import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement
    private String foo;

    @XmlElement(required=true)
    private String bar;

    @XmlElement(nillable=true)
    private String baz;

}

演示代码

下面的代码演示了如何使用JAXBContext生成XML模式。
import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class GenerateSchema {

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

        jc.generateSchema(new SchemaOutputResolver() {
            @Override
            public Result createOutput(String namespaceUri,
                    String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }
        });
    }

}

生成的XML模式

下面是生成的XML模式注释,与foo字段相对应的XML元素如何具有minOccurs="0",而与bar字段相对应的XML元素(用@XmlElement(required=true)注释的则没有)。这是因为默认minOccurs为1表示需要。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root" type="root"/>
  <xs:complexType name="root">
    <xs:sequence>
      <xs:element name="foo" type="xs:string" minOccurs="0"/>
      <xs:element name="bar" type="xs:string"/>
      <xs:element name="baz" type="xs:string" nillable="true" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

如果您想要一个null值的元素

域模型(根)
baz字段已使用@XmlElement(nillable=true)注释。如果该值为null,则结果XML元素将利用xsi:nil属性。如果没有此注释,则将空值视为不存在的节点。
import javax.xml.bind.annotation.*;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement
    private String foo;

    @XmlElement(required=true)
    private String bar;

    @XmlElement(nillable=true)
    private String baz;

}

演示代码
import javax.xml.bind.*;

public class MarshalDemo {

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

        Root root = new Root();

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

}

输出

以下是运行演示代码得到的XML。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <baz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</root>

关于java - @XmlElement和无用的 'required'参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19266391/

10-10 03:57