Link to home
Start Free TrialLog in
Avatar of fredand44
fredand44

asked on

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

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;
        }
    }
Avatar of mukundha_expert
mukundha_expert

i think you need to put the xsd file in the folder containing the jar file ...

because your current folder will be the folder containnig the jar, so if you put the xsd in the same folder , then you can give your xsdfilepath as your xsd file name
Avatar of fredand44

ASKER

Hello!

Thanks for your reply!

Unfortunately I do not think I can do that.

Do you know if it is possible to include the schema in the XML like you can do with old DTD:s?

Best regards

Fredrik
ASKER CERTIFIED SOLUTION
Avatar of valipotor
valipotor

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello!

Thanks for all replies.

Actually you are right when you say that I have problem with load the resource from the jar.

I have tried the following:

      parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", getClass().getClassLoader().getResourceAsStream("resources/xsd/my.xsd"));

But then I get:

java.lang.ClassCastException: weblogic.utils.zip.SafeZipFileInputStream
      at org.apache.xerces.impl.xs.XMLSchemaLoader.reset(Lorg.apache.xerces.xni.parser.XMLComponentManager;)V(Unknown Source)
      at org.apache.xerces.impl.xs.XMLSchemaValidator.reset(Lorg.apache.xerces.xni.parser.XMLComponentManager;)V(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.configurePipeline()V(Unknown Source)
      at org.apache.xerces.parsers.XIncludeAwareParserConfiguration.configurePipeline()V(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Z)Z(Unknown Source)
      at org.apache.xerces.parsers.XML11Configuration.parse(Lorg.apache.xerces.xni.parser.XMLInputSource;)V(Unknown Source)
      at org.apache.xerces.parsers.XMLParser.parse(Lorg.apache.xerces.xni.parser.XMLInputSource;)V(Unknown Source)
      at org.apache.xerces.parsers.AbstractSAXParser.parse(Lorg.xml.sax.InputSource;)V(Unknown Source)
      at com.myxml.XmlManager.validateXML(Ljava.lang.String;Ljava.lang.String;Ljava.lang.String;Ljava.lang.String;)Z(XmlManager.java:1683)


I have scan you links but I can not find the answer. Please correcy me if I'm wrong!

But I found this at the bootom of one of your links that sound interesting. Not Exactly what I want but I will try it:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/620e937c-e746-4a2f-9cde-f15f6114e1c4.asp

Best regards

Fredrik
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello!

Thanks for your reply!

I use regualar SDK 1.4 and a editor and weblogic as application server.

Perhaps your links is refering to any package from weblogic that I can use to validat a xml-structure according to a xsd in a jar?

I will look at it during the day!

Best regards
Fredrik