troubleshooting Question

How to validate a xml to a xsd in this situation?

Avatar of fredand44
fredand44 asked on
Web Languages and StandardsJava
7 Comments2 Solutions595 ViewsLast Modified:
Hello!

I'm developing a EJB app. The idea is that the EJB should validate a xml-stucture with a xsd.

This was working fine until I needed to put it all in a jar-file.

The structure of the ejb.jar looks like:

com/myapp
META-INF
lib
resources/xmlskeletons
resourses/xsd

I populate a document build upon a xmlskeleton.
Then I would like to validate the xml-structure according to the xsd.

Before, when this was in a exploaded file structure, I could set the xsd-path as a element in the xml-structure.

But now when the xsd is inside the jar, and as you can see in the code below, the xsd-file will not be found.

So if you have any suggestions how to include the xsd for the validation from a jar please let me know!!

BTW I can just use Java 1.4

Best regards
Fredrik

    public boolean validateXML(String xml, String xsdFilePath, String nameSpace, String rootElementName)
    {
        try {
            //Build temporary document
            Document documentTemp = buildDocumentFromString(xml);

            //Set namespace and schemaLocation for validation this will not concern the paths in xml sent to IC
            setXmlElementAttribute(rootElementName, "xmlns", nameSpace, documentTemp);
            setXmlElementAttribute(rootElementName, "xsi:schemaLocation",
                                   nameSpace + " " + System.getProperty("user.dir") + "/" + xsdFilePath, documentTemp);

            //Transform document to xml String
            String tempXml = getXmlStringFromDocument(documentTemp);

            //Validate the xml String
            saxParseException = null;
            SAXParser parser = new SAXParser();

            //parser.setFeature("http://xml.org/sax/features/validation", true);
            parser.setFeature("http://apache.org/xml/features/validation/schema", true);
            parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
            parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", xsdFilePath);
            parser.setErrorHandler(this);
            parser.parse(new InputSource(new StringReader(tempXml)));

            if (saxParseException == null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            printExceptionToFile("XML_EXCECPTION", e);

            return false;
        }
    }
ASKER CERTIFIED SOLUTION
valipotor

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros