gpt4 book ai didi

java - 使用 JAXB 创建带有 XSD 的 XML

转载 作者:行者123 更新时间:2023-12-02 05:47:15 25 4
gpt4 key购买 nike

我在 java 中有这个类,是我使用 xjc 基于 xsd 文件自动创建的:

 public class Item{

@XmlAttribute(name = "id_item", required = true)
protected String idInstitution;
@XmlAttribute(name = "item_name", required = true)
protected String itemName;
@XmlAttribute(name = "id_language")
protected String idLanguage;

//省略 geters 和 setter

正如您所看到的,有一些必需的属性,但我创建了一个没有这些属性的 xml 文件,我认为这是不可能的,我做错了什么?

这是我创建的 xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ItemList>
<Item item_name="test" />
</ItemList>

这是我创建 xml 的方法:

@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void createXML(@RequestBody ItemList list) throws JAXBException, FileNotFoundException {

JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);

Marshaller marshaller = jaxbContext.createMarshaller();

marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(list, System.out);

}

最佳答案

我通常使用这个工具从xsd创建xml。
http://xsd2xml.com

也许您可以弄清楚如何对其进行 API 调用来实现您的目的。

关于java - 使用 JAXB 创建带有 XSD 的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56083416/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com