通过 eclipse moxy 生成 XML 不包含根元素
generated XML through eclipse moxy doesnt contain Root element
我正在通过 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>
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);
生成 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>
MOXy 的动态 JAXB 创建了一个与将要生成的静态模型等效的模型。 ROOT
实体对应于命名的复杂类型。就像静态模型一样,您需要将其包装在 JAXBElement
的实例中以提供根元素名称。
我正在通过 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>
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);
生成 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>
MOXy 的动态 JAXB 创建了一个与将要生成的静态模型等效的模型。 ROOT
实体对应于命名的复杂类型。就像静态模型一样,您需要将其包装在 JAXBElement
的实例中以提供根元素名称。