gpt4 book ai didi

java - 从 JAXB 的 XSD 创建 XML 文件

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:06:48 24 4
gpt4 key购买 nike

我在使用 JAXB 从 XSD 创建 XML 文件时遇到问题,下面是用于创建它的 XSD 文件。 (注:出于保密原因,名字已被编辑)

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://ibm.org/seleniumframework" xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:element name="Test" type="sel:Test">
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Option1" type="sel:Option1Type" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element name="Option2" type="sel:Option2Type" xmlns:sel="http://ibm.org/seleniumframework"/>
<xs:element name="Option3" type="sel:ScreensType" xmlns:sel="http://ibm.org/seleniumframework"/>
</xs:choice>
</xs:complexType>
</xs:element>

<xs:complexType name="ScreensType">
<xs:sequence>
<xs:element type="sel:ScreenType" name="Screen" minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/>
</xs:sequence>
</xs:complexType>

<xs:complexType name="ScreenType">
<xs:sequence>
<xs:element name="ScreenData" minOccurs="1" maxOccurs="unbounded" xmlns:sel="http://ibm.org/seleniumframework"/>
</xs:sequence>
<xs:attribute type="xs:string" name="name1" use="required" />
<xs:attribute type="xs:string" name="name2" use="required" />
<xs:attribute type="xs:string" name="name3" use="required" />
</xs:complexType>

</xs:schema>

这是我用来尝试创建 XML 的代码:

public void generateXml() throws JAXBException, IOException {

Test test = new Test();
ScreensType screens = new ScreensType();
ScreenType screen = new ScreenType();
screen.setName1("a");
screen.setName2("b");
screen.setName3("c");

File f = new File("new.xml");
JAXBContext context= JAXBContext.newInstance("com.q1labs.qa.xmlgenerator.model.generatedxmlclasses");
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(test, f);
jaxbMarshaller.marshal(test, System.out);

}

这是输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test xmlns="http://ibm.org/seleniumframework"/>

我怎样才能得到代码来输出屏幕和屏幕标签及其属性,我不确定我做错了什么。

最佳答案

您创建了 TestScreensType 的实例,但在将它们编码为 XML 之前从未设置它们的任何属性。以下是更正后的代码。

public void generateXml() throws JAXBException, IOException {
Test test = new Test();
ScreensType screens = new ScreensType();
test.getOption1OrOption2OrOption3().add(screens);
ScreenType screen = new ScreenType();
screen.setName1("a");
screen.setName2("b");
screen.setName3("c");
screens.getScreen().add(screen);

File f = new File("new.xml");
JAXBContext context= JAXBContext.newInstance("com.q1labs.qa.xmlgenerator.model.generatedxmlclasses");
Marshaller jaxbMarshaller = context.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

jaxbMarshaller.marshal(test, f);
jaxbMarshaller.marshal(test, System.out);
}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test xmlns="http://ibm.org/seleniumframework">
<Option3>
<Screen name1="a" name2="b" name3="c"/>
</Option3>
</Test>

关于java - 从 JAXB 的 XSD 创建 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12147428/

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