  | |  | Help whit validation in Web services (xerces ) | Help whit validation in Web services (xerces ) 2003-10-02 - By Zulema Olguin
Hi I working whit Web service in java, axis and Xerces2, this web service get like paremeter an XML, the which use a schema whith restriction, but the validations no work, when i introduce a bad parameter. Note: The error of validation are write by catalina.out the xsd and xml was generate whith xmlspy <?xml version="1.0" encoding="UTF-8"?> <data xmlns:xsi=" <http://www.w3.org/2001/XMLSchema-instance> http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="1.xsd"> <data1>123</data1> <data2>1213</data2> </data>
<?xml version="1.0" encoding="UTF-8"?> <!--W3C Schema generated by XMLSPY v5 rel. 4 U ( <http://www.xmlspy.com/> http://www.xmlspy.com)--> <xs:schema xmlns:xs=" <http://www.w3.org/2001/XMLSchema> http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="data"> <xs:complexType> <xs:sequence> <xs:element ref="data1"/> <xs:element ref="data2"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="data1" type="xs:number"/> <xs:element name="data2" type="xs:number"/> </xs:schema> The program is : import javax.xml.parsers.*; import org.apache.xpath.*; import org.w3c.dom.*; import org.apache.xpath.*; import java.util.Hashtable; import org.apache.xerces.parsers.DOMParser; import org.xml.sax.ErrorHandler; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXParseException; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; import org.xml.sax.InputSource; import org.xml.sax.*; import java.io.IOException; import java.io.StringReader; import java.io.*;
public class z {
// Funciones Xpaht //====================================================================== = private static String findValue(Node node, String xql) throws Exception { // se validan los parametros de entrada if( (xql == null) || (xql.length() == 0) ) { throw new Exception("ERROR: findValue llamado con segundo parametro(xql) vacio"); } if(node == null) { throw new Exception("ERROR: findValue llamado con primer parametro(node) nulo"); throw new Exception("ERROR: findValue llamado con primer parametro(node) nulo"); } return getValorHijoDependiendoDeQuery(XPathAPI.selectSingleNode(node,xql) ); }
private static String getValorHijoDependiendoDeQuery ( Node node ) { NodeList NodosHijos; StringBuffer valor = new StringBuffer(); NodosHijos = node.getChildNodes(); for(int i=0; i < NodosHijos.getLength(); i++ ) { if( NodosHijos.item(i).getNodeType() == Node.TEXT_NODE ) { valor.append(NodosHijos.item(i).getNodeValue()); } } return valor.toString(); } public String getz(String xmlString) { String sxm=null; String respRenta = null; DOMParser parser = new DOMParser();
try {
// paso 1 recibe xml desde parametro y valida el esquema //====================================================================== =
parser.setFeature (" http://xml.org/sax/features/namespaces <http://xml.org/sax/features/namespaces> ", true); parser.setFeature (" http://xml.org/sax/features/validation <http://xml.org/sax/features/validation> ", true); parser.setFeature (" http://apache.org/xml/features/validation/schema <http://apache.org/xml/features/validation/schema> ", true); parser.setFeature (" http://apache.org/xml/features/validation/schema-full-checking <http://apache.org/xml/features/validation/schema-full-checking> ", true); parser.setFeature(" http://apache.org/xml/features/dom/defer-node-expansion <http://apache.org/xml/features/dom/defer-node-expansion> ", true); parser.setFeature(" http://apache.org/xml/features/continue-after-fatal-error <http://apache.org/xml/features/continue-after-fatal-error> ", true); parser.parse(new InputSource(new StringReader(xmlString)) ); Document document = parser.getDocument(); //====================================================================== = Element root = document.getDocumentElement(); Node configNode = root.getFirstChild(); NodeList childNodes = configNode.getChildNodes(); // Forma URL //====================================================================== = String URL = "d1=" + findValue( root, "/data/d1" ) + "&"; URL += "d2=" + findValue( root, "/data/d2" ) + "&"; URL += "d3=" + findValue( root, "/data/d3" ) ;
return URL; } catch (SAXNotRecognizedException ex4) { return "ERROR SAXNotRecognizedException: " + ex4.toString(); } catch (SAXNotSupportedException ex5) { return "ERROR SAXNotSupportedException: " + ex5.toString(); } catch (SAXParseException ex1) { return "ERROR SAXParseException: " + ex1.toString(); } catch (SAXException ex) { return "ERROR SAXException: " + ex.toString(); //return "Error SAXException: NO PUDE PARSEAR"; } catch (Exception e2) { return "ERROR Exception: " + e2.toString()+ "chao"; //return "Error Exception : NO PUDE PARSEAR"; } }
Gracias.- Zulema Olguin T. Chile
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1226" name=GENERATOR></HEAD> <BODY style="COLOR: #000000; FONT-FAMILY: Arial" hb_focus_attach="true"> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2>Hi</FONT></EM></SPAN></DIV> <DIV><SPAN class=000185620-26092003><FONT face=Georgia size=2></FONT></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2>I working whit Web service in java<SPAN class=375134823-02102003>,</SPAN> <SPAN class=375134823-02102003> axis </SPAN>and Xerces2, this web service get like paremeter an XML, the which use a schema whith restriction, but the validations no work, when i introduce a bad parameter.</FONT></EM></SPAN></DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><SPAN class=375134823-02102003><FONT face=Georgia size=2>Note: The error of validation are write by catalina.out </FONT></SPAN></EM></SPAN></DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2>the xsd and xml was generate whith xmlspy </FONT></EM></SPAN></DIV> <DIV><SPAN class=000185620-26092003><FONT face=Georgia size=2></FONT></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><FONT face=Georgia size=2></FONT></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2><?xml version="1.0" encoding="UTF-8"?><BR><data xmlns:xsi="</FONT></EM><A href="http://www.w3.org/2001/XMLSchema-instance"><EM><FONT face=Georgia size=2>http://www.w3.org/2001/XMLSchema-instance</FONT></EM></A><EM><FONT face=Georgia size=2>" xsi:noNamespaceSchemaLocation="1.xsd"><BR> <data1>123</data1 ><BR> <data2>1213</data2><BR></data><BR></FONT></EM>< /SPAN></DIV> <DIV><SPAN class=000185620-26092003><FONT face=Georgia size=2></FONT></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2><?xml version="1.0" encoding="UTF-8"?><BR><!--W3C Schema generated by XMLSPY v5 rel. 4 U (</FONT></EM><A href="http://www.xmlspy.com/"><EM><FONT face=Georgia size=2>http://www.xmlspy.com</FONT></EM></A><EM><FONT face=Georgia size=2>)--><BR><xs:schema xmlns:xs="</FONT></EM><A href="http://www.w3.org/2001/XMLSchema"><EM><FONT face=Georgia size=2>http://www.w3.org/2001/XMLSchema</FONT></EM></A><EM><FONT face=Georgia size=2>" elementFormDefault="qualified"><BR> <xs:element name="data"><BR> <xs:complexType><BR> < ;xs:sequence><BR> <xs:element ref="data1"/><BR> <xs:element ref="data2"/><BR> </xs:sequence><BR> < /xs:complexType><BR> </xs:element><BR> <xs:element name="data1" type="xs:number"/><BR> <xs:element name="data2" type="xs:number"/><BR></xs:schema></FONT></EM></SPAN></DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><EM><FONT face=Georgia size=2></FONT></EM></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT face=Georgia size=2><EM>The program is :</EM></FONT></SPAN></SPAN></DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><EM><FONT face=Georgia size=2></FONT></EM></SPAN></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><EM><FONT face=Georgia size=2></FONT></EM></SPAN></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>import javax.xml.parsers.*;<BR>import org.apache.xpath.*;<BR>import org.w3c.dom.*;<BR>import org.apache.xpath.*;</FONT></SPAN></SPAN></DIV> <DIV> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>import java.util.Hashtable;</FONT></SPAN></SPAN></DIV> <DIV> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>import org.apache.xerces.parsers.DOMParser;<BR>import org.xml.sax.ErrorHandler;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.Node;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.NodeList;</FONT></SPAN></SPAN></DIV> <DIV> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>import org.xml.sax.SAXParseException;<BR>import org.xml.sax.SAXException;<BR>import org.xml.sax.SAXNotRecognizedException;<BR>import org.xml.sax.SAXNotSupportedException;</FONT></SPAN></SPAN></DIV> <DIV> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>import org.xml.sax.InputSource;<BR>import org.xml.sax.*;<BR>import java.io.IOException;<BR>import java.io.StringReader;<BR>import java.io.*;<BR></FONT></SPAN></SPAN></DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003></SPAN></SPAN><SPAN class=000185620-26092003><SPAN class=375134823-02102003></SPAN></SPAN><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2></FONT></SPAN></SPAN> </DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2>public class z {</FONT></SPAN></SPAN></DIV> <DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2> </FONT></SPAN></SPAN></DIV><SPAN class=000185620-26092003><SPAN class=375134823-02102003><FONT size=2> <DIV><BR> // Funciones Xpaht<BR> //=======================================================================<BR> private static String findValue(Node node, String xql) throws Exception<BR> {<BR> // se validan los parametros de entrada<BR> if( (xql == null) || (xql.length() == 0) ) {<BR> throw new Exception("ERROR: findValue llamado con segundo parametro(xql) vacio");<BR> }<BR> if(node == null) {<BR> throw new Exception("ERROR: findValue llamado con primer parametro(node) nulo");</DIV> <DIV> throw new Exception("ERROR: findValue llamado con primer parametro(node) nulo");<BR> }<BR> return getValorHijoDependiendoDeQuery(XPathAPI.selectSingleNode(node,xql) );<BR> }</DIV> <DIV> </DIV> <DIV><BR> private static String getValorHijoDependiendoDeQuery ( Node node )<BR> {<BR> NodeList NodosHijos;<BR> StringBuffer valor = new StringBuffer();<BR> NodosHijos = node.getChildNodes();<BR> for(int i=0; i < NodosHijos.getLength(); i++ )<BR> {<BR> if( NodosHijos.item(i).getNodeType() == Node.TEXT_NODE )<BR> {<BR> valor.append(NodosHijos.item(i).getNodeValue());<BR> }<BR> }<BR> return valor.toString();<BR> }</DIV> <DIV> </DIV> <DIV> public String getz(String xmlString) {</DIV> <DIV> </DIV> <DIV> String sxm=null;<BR> String respRenta = null;<BR> DOMParser parser = new DOMParser();</DIV> <DIV> </DIV> <DIV><BR> try {</DIV> <DIV><BR> // paso 1 recibe xml desde parametro y valida el esquema<BR> //=======================================================================</DIV> <DIV> </DIV> <DIV><BR> parser.setFeature ("<A href="http://xml.org/sax/features/namespaces">http://xml.org/sax/features /namespaces</A>", true);<BR> parser.setFeature ("<A href="http://xml.org/sax/features/validation">http://xml.org/sax/features /validation</A>", true);<BR> parser.setFeature ("<A href="http://apache.org/xml/features/validation/schema">http://apache.org/xml /features/validation/schema</A>", true);<BR> parser.setFeature ("<A href="http://apache.org/xml/features/validation/schema-full-checking">http:/ /apache.org/xml/features/validation/schema-full-checking</A>", true);</DIV> <DIV> </DIV> <DIV> parser.setFeature("<A href="http://apache.org/xml/features/dom/defer-node-expansion">http://apache .org/xml/features/dom/defer-node-expansion</A>", true);<BR> parser.setFeature("<A href="http://apache.org/xml/features/continue-after-fatal-error">http://apache .org/xml/features/continue-after-fatal-error</A>", true);</DIV> <DIV> </DIV> <DIV> </DIV> <DIV> parser.parse(new InputSource(new StringReader(xmlString)) );<BR> Document document = parser.getDocument();</DIV> <DIV> </DIV> <DIV> <BR> //=======================================================================<BR> Element root = document.getDocumentElement();<BR> Node configNode = root.getFirstChild();<BR> NodeList childNodes = configNode.getChildNodes();</DIV> <DIV> </DIV> <DIV> // Forma URL </DIV> <DIV> //=======================================================================<BR> String URL = "d1=" + findValue( root, "/data/d1" ) + "&";<BR> URL += "d2=" + findValue( root, "/data/d2" ) + "&";<BR> URL += "d3=" + findValue( root, "/data/d3" ) ;</DIV> <DIV><BR> return URL;</DIV> <DIV> </DIV> <DIV> } catch (SAXNotRecognizedException ex4) {<BR> return "ERROR SAXNotRecognizedException: " + ex4.toString();<BR> } catch (SAXNotSupportedException ex5) {<BR> return "ERROR SAXNotSupportedException: " + ex5.toString();<BR> } catch (SAXParseException ex1) {<BR> return "ERROR SAXParseException: " + ex1.toString();<BR> } catch (SAXException ex) {<BR> return "ERROR SAXException: " + ex.toString();<BR> //return "Error SAXException: NO PUDE PARSEAR";<BR> } catch (Exception e2) {<BR> return "ERROR Exception: " + e2.toString()+ "chao";<BR> //return "Error Exception : NO PUDE PARSEAR";<BR> }<BR> }<BR></DIV> <DIV> </DIV></FONT></SPAN></SPAN><FONT face="Times New Roman"> <DIV><SPAN class=375134823-02102003></SPAN><FONT face=Arial><FONT size=2>G<SPAN class=375134823-02102003>racias.-</SPAN></FONT></FONT><BR></FONT><I></I><I> <FONT face=Georgia size=2>Zulema Olguin T.</FONT></I></DIV> <DIV><FONT face="Times New Roman"><SPAN class=375134823-02102003></SPAN><EM><FONT face=Georgia><FONT size=2>C<SPAN class=375134823-02102003>hile </SPAN></FONT></FONT></EM><BR></DIV></FONT> <P></P></BODY></HTML>
|
|
 |