我将 @XmlElement(name ="title",required = true)放在javabean属性 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;
}
....
Run Code Online (Sandbox Code Playgroud)
在Main的某个地方:
// 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();
Run Code Online (Sandbox Code Playgroud)
bdo*_*han 20
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;
}
Run Code Online (Sandbox Code Playgroud)
演示代码
下面是一些代码,演示了如何使用它生成XML模式JAXBContext.
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;
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
生成的XML架构
下面是生成的XML模式,说明对应于该foo字段的XML元素如何与该字段对应minOccurs="0"的XML元素bar(注释时@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>
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
演示代码
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);
}
}
Run Code Online (Sandbox Code Playgroud)
产量
下面是运行演示代码生成的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>
Run Code Online (Sandbox Code Playgroud)