如何使用 JaXB 获取验证事件?

2022-01-19 00:00:00 validation xml java jaxb

我尝试使用 Jaxb 在变量中获取验证消息.从这里尝试示例 http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/xml/bind/Unmarshaller.html

I try to get validation message in variable with Jaxb. Try example from here http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.6/api/javax/xml/bind/Unmarshaller.html

我的代码:

JAXBContext jaxbContext = JAXBContext.newInstance("com.piyush");
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new StreamSource(new File("D:/liferay-develop/workspace/cat_test/v1/STD_MP.xsd")));
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
jaxbUnmarshaller.setSchema(schema);
ValidationEventCollector validationCollector= new ValidationEventCollector();
jaxbUnmarshaller.setEventHandler( validationCollector );
STDMP ts = (STDMP)jaxbUnmarshaller.unmarshal(xml_gkuzu);
if(validationCollector.hasEvents())
{
    for(ValidationEvent event:validationCollector.getEvents())
    {
        String msg = event.getMessage();
        System.out.println(msg);
    }
}

但是什么也没发生.我做错了什么?

But nothing happens. What am I doing wrong ?

推荐答案

以下应该会有所帮助:

JAXB2ValidationEventCollector

ValidationEventCollector 来自 JAXB 1 (JSR-31) 并没有似乎不太支持我们在 JAXB 2 (JSR-222) 中对验证所做的更改.您可以通过创建 ValidationEventHandler 的子类来解决此问题,如下所示.

ValidationEventCollector came from JAXB 1 (JSR-31) and doesn't appear to support the changes we made to validation in JAXB 2 (JSR-222) very well. You can solve this issue by creating a subclass of ValidationEventHandler like the following.

package forum12295028;

import javax.xml.bind.ValidationEvent;
import javax.xml.bind.util.ValidationEventCollector;

class JAXB2ValidationEventCollector extends ValidationEventCollector {

    @Override
    public boolean handleEvent(ValidationEvent event) {
        super.handleEvent(event);
        return true;
    }

}

<小时>

示例

下面的例子可以用来证明一切正常

The following example can be used to prove that everything works

客户

package forum12295028;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlRootElement
public class Customer {

    private String name;

    private List<PhoneNumber> phoneNumbers = 
        new ArrayList<PhoneNumber>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement(name="phone-number")
    public List<PhoneNumber> getPhoneNumbers() {
        return phoneNumbers;
    }

    public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
        this.phoneNumbers = phoneNumbers;
    }

}

电话号码

package forum12295028;

public class PhoneNumber {

}

customer.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="customer">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="stringMaxSize5"/>
                <xs:element ref="phone-number" maxOccurs="2"/>
             </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="phone-number">
        <xs:complexType>
            <xs:sequence/>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="stringMaxSize5">
        <xs:restriction base="xs:string">
            <xs:maxLength value="5"/>
        </xs:restriction>
    </xs:simpleType>

</xs:schema> 

input.xml

<?xml version="1.0" encoding="UTF-8"?>
<customer>
   <name>Jane Doe</name>
   <phone-number/>
   <phone-number/>
   <phone-number/>
</customer>

演示

package forum12295028;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.bind.util.ValidationEventCollector;
import javax.xml.validation.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("src/forum12295028/customer.xsd")); 

        JAXBContext jc = JAXBContext.newInstance(Customer.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        ValidationEventCollector validationCollector = new JAXB2ValidationEventCollector();
        unmarshaller.setEventHandler(validationCollector);

        Customer customer = (Customer) unmarshaller.unmarshal(new File("src/forum12295028/input.xml"));

        if(validationCollector.hasEvents())
        {
            for(ValidationEvent event:validationCollector.getEvents())
            {
                String msg = event.getMessage();
                System.out.println(msg);
            }
        }
    }

}

输出

cvc-maxLength-valid: Value 'Jane Doe' with length = '8' is not facet-valid with respect to maxLength '5' for type 'stringMaxSize5'.
cvc-type.3.1.3: The value 'Jane Doe' of element 'name' is not valid.
cvc-complex-type.2.4.d: Invalid content was found starting with element 'phone-number'. No child element is expected at this point.

相关文章