Issue
I have a to send an xml file to a spring boot controller, in this file i have a list of objects, i have to validate the xml file and, if it's valid i have to unmarshal those objects into dtos and than save in db. These are the xml file
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ListProductDTO
xsi:noNamespaceSchemaLocation="schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<productDTO>
<eancode>1111111111111</eancode>
<name>hamburger</name>
<price>10</price>
<weight>100</weight>
<description>a nice hamburger</description>
<quantity>1</quantity>
<categoryName>meat</categoryName>
</productDTO>
<productDTO>
<eancode>2111111111111</eancode>
<name>banana</name>
<price>6</price>
<weight>100</weight>
<description>a nice banana</description>
<quantity>1</quantity>
<categoryName> fruit</categoryName>
</productDTO>
<productDTO>
<eancode>3111111111111</eancode>
<name>apple</name>
<price>10</price>
<weight>100</weight>
<description>a nice apple</description>
<quantity>1</quantity>
<categoryName>fruit</categoryName>
</productDTO>
</ListProductDTO>
and this is the xsd schema that i use
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="listProductDTO">
<xs:complexType>
<xs:sequence>
<xs:element name="products" type="productDTO" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="productDTO">
<xs:sequence>
<xs:element name="categoryName" type="xs:string" minOccurs="0"/>
<xs:element name="description" type="xs:string" minOccurs="0"/>
<xs:element name="eancode" type="xs:string" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
<xs:element name="price" type="xs:decimal" minOccurs="0"/>
<xs:element name="quantity" type="xs:int" minOccurs="0"/>
<xs:element name="weight" type="xs:double" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
also the class that i use for conversion
@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
@XmlRootElement(name = "ListProductDTO")
@XmlAccessorType(XmlAccessType.FIELD)
public class ListProductDTO {
@XmlElement(name = "productDTO")
private List<ProductDTO> products ;
}
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@NoArgsConstructor
@XmlRootElement(name = "productDTO")
@XmlType(propOrder = {"eancode","name","price","weight","description","quantity","categoryName"})
@XmlAccessorType(XmlAccessType.FIELD)
public class ProductDTO {
@NonNull
@NotBlank(message = "eancode cannot be blank")
@Eancode(message = "eancode not valid")
@XmlElement(name = "eancode")
private String eancode;
@NonNull
@NotBlank(message = "invalid name")
@XmlElement(name = "name")
private String name;
@NonNull
@DecimalMin(message = "invalid price", value = "0.01")
@XmlElement(name = "price")
private BigDecimal price;
@NonNull
@DecimalMin(message = "weight cannot be lesser than zero", value = "0.01")
@XmlElement(name = "weight")
private Double weight;
@XmlElement(name = "description")
private String description;
@NonNull
@Min(message = "quantity cannot be lesser than zero", value = 1)
@XmlElement(name = "quantity")
private Integer quantity;
@NonNull
@NotBlank(message = "a product must belong to a category")
@XmlElement(name = "categoryName")
private String categoryName;
this is the code that i use for setup jaxb unmarshaller and set the schema:
JAXBContext context = JAXBContext.newInstance(ListProductDTO.class,ProductDTO.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
File file = new File("src/main/resources/schemas/schema.xsd");
Schema schema = sf.newSchema(file);
unmarshaller.setSchema(schema);
ListProductDTO temp = (ListProductDTO) unmarshaller.unmarshal(tmp);
the last line of code throws this exception:
org.xml.sax.SAXParseException: cvc-elt.1.a: impossible to find the element "ListProductDTO".
how can i solve?
edit: tmp is a temporary file that i generate after i decoded the base64 econded xml file that i send to the controller
Solution
The Schema and XML you have provided are not validating against each other.
You should first check if your XML is as per the Schema, i.e. validating against the schema
Secondly you are getting :
impossible to find the element "ListProductDTO"
as there is no such element in the schema instead it is listProductDTO
Answered By - Ravi Shankar
Answer Checked By - Candace Johnson (JavaFixing Volunteer)