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(xm l);
//Set namespace and schemaLocation for validation this will not concern the paths in xml sent to IC
setXmlElementAttribute(roo tElementNa me, "xmlns", nameSpace, documentTemp);
setXmlElementAttribute(roo tElementNa me, "xsi:schemaLocation",
nameSpace + " " + System.getProperty("user.d ir") + "/" + xsdFilePath, documentTemp);
//Transform document to xml String
String tempXml = getXmlStringFromDocument(d ocumentTem p);
//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(thi s);
parser.parse(new InputSource(new StringReader(tempXml)));
if (saxParseException == null) {
return true;
} else {
return false;
}
} catch (Exception e) {
printExceptionToFile("XML_ EXCECPTION ", e);
return false;
}
}
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(xm
//Set namespace and schemaLocation for validation this will not concern the paths in xml sent to IC
setXmlElementAttribute(roo
setXmlElementAttribute(roo
nameSpace + " " + System.getProperty("user.d
//Transform document to xml String
String tempXml = getXmlStringFromDocument(d
//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(thi
parser.parse(new InputSource(new StringReader(tempXml)));
if (saxParseException == null) {
return true;
} else {
return false;
}
} catch (Exception e) {
printExceptionToFile("XML_
return false;
}
}
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The schema can be referenced from the xml file.
See this links:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/5438989c-9937-4288-a960-5db69cb3ba60.asp
valipotor
See this links:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/html/5438989c-9937-4288-a960-5db69cb3ba60.asp
valipotor
ASKER
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( ).getResou rceAsStrea m("resourc es/xsd/my. xsd"));
But then I get:
java.lang.ClassCastExcepti on: weblogic.utils.zip.SafeZip FileInputS tream
at org.apache.xerces.impl.xs. XMLSchemaL oader.rese t(Lorg.apa che.xerces .xni.parse r.XMLCompo nentManage r;)V(Unkno wn Source)
at org.apache.xerces.impl.xs. XMLSchemaV alidator.r eset(Lorg. apache.xer ces.xni.pa rser.XMLCo mponentMan ager;)V(Un known Source)
at org.apache.xerces.parsers. XML11Confi guration.c onfigurePi peline()V( Unknown Source)
at org.apache.xerces.parsers. XIncludeAw areParserC onfigurati on.configu rePipeline ()V(Unknow n Source)
at org.apache.xerces.parsers. XML11Confi guration.p arse(Z)Z(U nknown Source)
at org.apache.xerces.parsers. XML11Confi guration.p arse(Lorg. apache.xer ces.xni.pa rser.XMLIn putSource; )V(Unknown Source)
at org.apache.xerces.parsers. XMLParser. parse(Lorg .apache.xe rces.xni.p arser.XMLI nputSource ;)V(Unknow n Source)
at org.apache.xerces.parsers. AbstractSA XParser.pa rse(Lorg.x ml.sax.Inp utSource;) V(Unknown Source)
at com.myxml.XmlManager.valid ateXML(Lja va.lang.St ring;Ljava .lang.Stri ng;Ljava.l ang.String ;Ljava.lan g.String;) Z(XmlManag er.java:16 83)
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
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(
But then I get:
java.lang.ClassCastExcepti
at org.apache.xerces.impl.xs.
at org.apache.xerces.impl.xs.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at org.apache.xerces.parsers.
at com.myxml.XmlManager.valid
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
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