Snippet: validating xml with xsd in Java

     
import java.io.Reader;
import java.io.StringReader;


import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;


import org.springframework.core.io.ClassPathResource;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.xml.sax.SAXException;


....


String xmlFormated = "";
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

       ClassPathResource cpr = new ClassPathResource("my_xsd_file.xsd");
       Source schemaFile = new StreamSource( cpr.getFile()  );
       Schema schema = factory.newSchema(schemaFile);

       Validator validator = schema.newValidator();
       Reader reader = new StringReader(xmlFormated);
       Source source = new StreamSource(reader);


       try {
           validator.validate(source);
       } catch (SAXException e) {
        log.error(e);
       }


...

How to see maven modules as projects in Eclipse Package Explorer

When a user, not project's creator, downloads a maven project with modules inside. He/She sees a project that contains all modules. To get all modules as projects you can follow these tutorial:

1.- Download parent project
2.- On File menu, select import > Maven > Existing Maven Projects
3.- Push next
4.- Browse to downloaded project in Root Directory select list.
5.- Check pom.xml files listed below
6.- Push Finish

Now you can see the parent maven project and all modules as projects. All modules are synchronized with parent project.