Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.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 @XmlElement和无用的';必需的';参数_Java_Jaxb - Fatal编程技术网

Java @XmlElement和无用的';必需的';参数

Java @XmlElement和无用的';必需的';参数,java,jaxb,Java,Jaxb,我将@xmlement(name=“title”,required=true)放在javabean属性之前 对某些\u属性进行int,并且没有为某些\u属性指定值。由于某些原因,此属性在生成的XML中没有出现。因此,请解释必需的含义 代码中一些有意义的部分: @XmlRootElement(name = "book") @XmlType(propOrder = { "author", "name", "publisher", "isbn" }) public class Book { priv

我将@xmlement(name=“title”,required=true)放在javabean属性之前 对某些\u属性进行int,并且没有为某些\u属性指定值。由于某些原因,此属性在生成的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;
}
....
主视图中的某个位置

    // 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();
//创建书籍
Book book1=新书();
第1册setIsbn(“978-0060554736”);
书1.作者(“尼尔·施特劳斯”);
book1.setPublisher(“Harpercollins”);
图书目录。添加(第1册);
Book book2=新书();
第2册setIsbn(“978-3832180577”);
账簿2.设置名称(“Feuchtgebiete”);
第二册作者(“夏洛特·罗奇”);
书籍2.setPublisher(“Dumont Buchverlag”);
增加(第2册);
JAXBContext context=JAXBContext.newInstance(Bookstore.class);
Marshaller m=context.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式的_输出,Boolean.TRUE);
//写入System.out
m、 元帅(书店,系统输出);
//写入文件
m、 封送员(书店,新文件(书店XML));
//从之前创建的xml文件中获取变量
System.out.println();
System.out.println(“来自XML文件的输出:”);
Unmarshaller um=context.createUnmarshaller();
bookstore2=(Bookstore)um.unmarshal(新文件阅读器(Bookstore_XML));
ArrayList=bookstore2.getBooksList();

您能给我们看一下您的代码和生成的xml的示例吗? 根据文件:

如果required为true,则Javabean属性映射到XML模式 带有minOccurs=“1”的元素声明。maxOccurs对于单个 有值属性和多值属性的“无界”

发件人:

需要公共抽象布尔值

自定义所需的元素声明。 如果required()为true,则Javabean属性将映射到带有minOccurs=“1”的XML模式元素声明。对于单值属性,maxOccurs为“1”,对于多值属性,maxOccurs为“无界”

如果required()为false,则Javabean属性将映射到带有minOccurs=“0”的XML模式元素声明。对于单值属性,maxOccurs为“1”,对于多值属性,maxOccurs为“无界”

属性映射到一个元素,该元素(希望)在模式中声明为必需的。您生成的XML不符合该特定模式,这或多或少是我从一个实例中所期望的,该实例没有“按规则行事”,它在
@xmlement
@xmlement
注释上的
required
属性会影响从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;

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

}
下面是一些代码,演示如何使用
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元素(用
@xmlement(required=true)注释)
没有。这是因为默认的
minOccurs
为1表示它是必需的


如果需要
null
值的元素 域模型(根)

baz
字段已使用
@xmlement(nillable=true)
进行注释。如果值为null,则生成的XML元素将利用
xsi:nil
属性。没有此注释,null值将被视为缺少节点

演示代码

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



但上面说@xmlement(required=true)保证值以XML格式打印,因为我们可以看到结果中没有bar值XML@voipp-它在哪里说的?很遗憾,我找不到。答案是thanx。解释得很好!简短而甜美!非常感谢!So required=true是指:minOccurs=1,但这并不意味着它出现在结果请求中?