我通过eclipse MOXY生成XML并且生成的xml不包含Root元素,但包含Root元素的子元素。
我附加了XSD,代码和生成的XML。我希望Root元素成为生成的XML的一部分。
我是Eclipse MOXY的新手。请帮助解决此问题。 提前致谢。
XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="example"
xmlns:myns="example" xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="ROOT" type="myns:ROOT" />
<xs:complexType name="ROOT">
<xs:sequence>
<xs:element name="HEADERDATA" type="myns:HEADERDATA" />
<xs:element name="PRODUCTS" type="myns:PRODUCTS" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="PRODUCTS">
<xs:sequence>
<!-- Here the min/max tag is for multiple nodes -->
<xs:element name="PRODUCT" type="myns:PRODUCT" minOccurs="0"
maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="HEADERDATA">
<xs:sequence>
<xs:element name="SOURCE" type="xs:string" />
<xs:element name="TARGET" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="PRODUCT">
<xs:sequence>
<xs:element name="LASTUPDATED" type="xs:string" />
<xs:element name="TYPE" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
&#13;
Code :
DynamicJAXBContext context = DynamicJAXBContextFactory.createContextFromXSD(xsdInputStream, null, null, null);
DynamicEntity rootEntity = context.newDynamicEntity("ROOT");
DynamicEntity headerDataEntity = context.newDynamicEntity("HEADERDATA");
headerDataEntity.set("source", "source");
headerDataEntity.set("target", "target");
rootEntity.set("HEADERDATA".toLowerCase(), headerDataEntity);
DynamicEntity productsEntity = context.newDynamicEntity("PRODUCTS");
ArrayList productList = new ArrayList();
DynamicEntity productEntity = context.newDynamicEntity("PRODUCT");
productEntity.set("lastupdated", "LASTUPDATED");
productEntity.set("type", "type");
productList.add(productEntity);
productsEntity.set("PRODUCT".toLowerCase(), productList);
rootEntity.set("PRODUCTS".toLowerCase(), productsEntity);
JAXBMarshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(rootEntity, System.out);
&#13;
生成的XML:
<?xml version="1.0" encoding="UTF-8"?>
<HEADERDATA>
<SOURCE>source</SOURCE>
<TARGET>target</TARGET>
<TIMESTAMP>timestamp</TIMESTAMP>
<XML_VERSION>xml_version</XML_VERSION>
<XML_NAME>xml_name</XML_NAME>
</HEADERDATA><PRODUCTS>
<PRODUCT>
<LASTUPDATED>LASTUPDATED</LASTUPDATED>
<TYPE>type</TYPE>
<STYLEID>styleid</STYLEID>
<PRODUCTID>productid</PRODUCTID>
</PRODUCT>
</PRODUCTS>
&#13;
答案 0 :(得分:0)
MOXy的动态JAXB创建的模型等同于将生成的静态模型。 ROOT
实体对应于命名的复杂类型。就像静态模型一样,您需要将其包装在JAXBElement
的实例中以提供根元素名称。