Hi Sean,
This is about the only way to handle validation in JiBX. Other data
binding techniques which support validation generally do pretty much the
same thing behind the scenes - serialize to byte stream or character
stream/string, validate the byte/character stream. You might want to try
using the Java 5 validation handling instead of Xerces, though - I
suspect the Java 5 implementation would be more performant, though I
haven't checked (so if you *do* try both, let us know how the
performance compares!). Here's some sample code for using the Java 5
version:
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* Schema validator using support added in JDK 1.5.
*/
public class Validate
{
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("Usage: java Validate schema-file " +
"inst-file1 inst-file2 ...");
return;
}
Validator validator;
Document document;
try {
// create a SchemaFactory capable of understanding WXS schemas
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// create validator from schema instance
Source schemaFile = new StreamSource(new File(args[0]));
Schema schema = factory.newSchema(schemaFile);
validator = schema.newValidator();
// validate all instance documents
for (int i = 1; i < args.length; i++) {
// parse input XML document into a DOM tree
DocumentBuilder parser =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
document = parser.parse(new File(args[i]));
// validate the DOM tree
try {
validator.validate(new DOMSource(document));
} catch (SAXException e) {
System.err.println("Document validation error on " +
args[i]);
e.printStackTrace();
} catch (IOException e) {
System.err.println("Error processing document");
e.printStackTrace();
}
}
} catch (Exception e) {
System.err.println("Error in validation setup");
e.printStackTrace();
}
}
}
- Dennis
Dennis M. Sosnoski
SOA and Web Services in Java
Training and Consulting
http://www.sosnoski.com - http://www.sosnoski.co.nz
Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117
Sean Mickey wrote:
> Hi marco -
> Your need to validate the XML produced by JiBX does not mean that you *must*
> use the schema generator, it just means that you have to create a schema that
> matches the XML that will be produced by JiBX during marshalling. You could
> certainly use the schema generator, but it is not necessary. We are using
> existing classes (from the JiBX perspective) that are not generated from the
> XML schema.
>
> We have the need for schema validation on our project, because our XML must
> be sent over JMS, where it is received by an infrastructure process that
> validates the structure of the XML before allowing it to continue through to
> delivery for the JMS MessageConsumer. We looked for a way to automate the
> schema validation within JiBX, but did not find anything that meets our
> needs. Note that this is our first project with JiBX, so it is possible that
> we missed something.
>
> We ended up creating a simple framework that contains 4 validation points,
> because our messaging model is request-reply:
> 1 Sender has Java Object -> JiBX marshall to XML -> Schema validation, if
> good -> send JMS message with XML
> 2 Framework receives JMS message -> obtains XML and performs schema
> validation, if good -> JiBX unsmarshall -> Receiver is given Java Object
> 3 Handler does necessary processing and creates a new Java Object -> JiBX
> marshall to XML -> Schema validation, if good -> send JMS reply message with
> XML
> 4 Framework receives JMS reply message -> obtains XML and performs schema
> validation, if good -> JiBX unmarshall -> Original sender from step 1 is
> given reply Java Object
>
> We use Xerces 2.9.1 for all of the schema validation. I hope this is helpful -
> Sean
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]]
> Sent: Thursday, January 08, 2009 5:09 AM
> To: [email protected]
> Subject: [jibx-users] Validating XML against schema
>
>
> Hi all,
> I am currently using JIBX to unmarshal XML into java objects..
>
> I need to validate the XML against a schema...
>
> Does that mean that I need to use schema generator to use my classes?
>
> What if I have existing classes (not being generated from the schema)?
>
> Thanks an dregards
> marco
> _______________________________________________
>
> This e-mail may contain information that is confidential, privileged or
> otherwise protected from disclosure. If you are not an intended recipient of
> this e-mail, do not duplicate or redistribute it by any means. Please delete
> it and any attachments and notify the sender that you have received it in
> error. Unless specifically indicated, this e-mail is not an offer to buy or
> sell or a solicitation to buy or sell any securities, investment products or
> other financial product or service, an official confirmation of any
> transaction, or an official statement of Barclays. Any views or opinions
> presented are solely those of the author and do not necessarily represent
> those of Barclays. This e-mail is subject to terms available at the following
> link: www.barcap.com/emaildisclaimer. By messaging with Barclays you consent
> to the foregoing. Barclays Capital is the investment banking division of
> Barclays Bank PLC, a company registered in England (number 1026167) with its
> registered offic
> e at 1 Churchill Place, London, E14 5HP. This email may relate to or be
> sent from other members of the Barclays Group.
> _______________________________________________
>
> ------------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It is the best place to buy or sell services for
> just about anything Open Source.
> http://p.sf.net/sfu/Xq1LFB
> _______________________________________________
> jibx-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>
> ------------------------------------------------------------------------------
> Check out the new SourceForge.net Marketplace.
> It is the best place to buy or sell services for
> just about anything Open Source.
> http://p.sf.net/sfu/Xq1LFB
> _______________________________________________
> jibx-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>
>
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users