Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8502797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:35:07+00:00 2026-06-11T01:35:07+00:00

I try to get validation message in variable with Jaxb. Try example from here

  • 0

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

My code:

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T01:35:09+00:00Added an answer on June 11, 2026 at 1:35 am

    The following should help:

    JAXB2ValidationEventCollector

    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;
        }
    
    }
    

    EXAMPLE

    The following example can be used to prove that everything works

    Customer

    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;
        }
    
    }
    

    PhoneNumber

    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>
    

    Demo

    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);
                }
            }
        }
    
    }
    

    Output

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to get to a page straight from Bash at http://www.ocwconsortium.org/ . The
I use the JQuery Validation plugin to validate a form ( http://docs.jquery.com/Plugins/Validation/ ) For
I try to get this following url using the downloadURL function as follows: http://www.ncbi.nlm.nih.gov/nuccore/27884304
I'm getting the error message from some validation code I have in my model.
Here is the code: try { $result = Model_User::update_user($_POST); // message: save success Message::add('success',
I try get a my attribute inside Fancybox, and I get when I use
i try to get my FragmentPagerAdapter working, but the examples are a bit to
I try to get the whole content between an opening xml tag and it's
I try to get the main window handle using following code in Lazarus (Free
I try to get some website attribute (colour of the cell) and compare in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.