本文介绍了从JAXB的XSD创建XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用JAXB从XSD创建XML文件时遇到的问题是用于创建它的XSD文件。 (注意:由于机密性,名称已被编辑)
I'm having trouble creating an XML file from XSD using JAXB below is the XSD file used to create it. (Note: Names have been edited due to confidentiality)
<?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的代码:
This is the code I am using to try and create the 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"/>
如何获取代码输出屏幕和屏幕标签及其属性,我'我不确定我做错了什么。
How can I get the code to output the screens and the screen tag along with it's properties, I'm not sure what I'm doing wrong.
推荐答案
你创建一个测试的实例
和 ScreensType
但在将它们编组为XML之前从不设置任何属性。下面是更正后的代码。
You create an instance of Test
and ScreensType
but never set any of their properties before marshalling them to XML. Below is the corrected code.
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>
这篇关于从JAXB的XSD创建XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!