
这些元素嵌套在definitions元素中,
(1) Definitions是WSDL文档的根元素。对应于这个类: org.eclipse.wst.wsdl.Definition 其他的对芹返象都可以通过这个对象获得
(2) Types - 数据类型定义的容器,它使用某种类型系统(一般地使用XML Schema中的类型系统)。
(3) Message - 通信消息的数据结构的抽象类型化定义。使用Types所定义的类型来定义整个消息的数据结构。
(4) PortType - 对于某个访问入口点类型所支持的 *** 作的抽象集合,这些 *** 作可以由一个或多个服务访问点来支持。
(子节点) Operation - 对服务中所支持的 *** 作的抽象描述,一般单个Operation描述了一个访问入口的请求/响应消息对。
(5) Binding - 特定端口类型的具体协议和数据孙首禅格式规范的绑定。
(6) Service- 相关服务访问点的集合。
(子节点) Port - 定义为协议/数据格式绑定与具体Web访问地址组合的单则尘个服务访问点。
下面是代码实例:
import java.io.Fileimport java.io.IOExceptionimport java.util.Iteratorimport java.util.Listimport java.util.Mapimport java.util.Set import javax.wsdl.Messageimport javax.wsdl.Partimport javax.wsdl.PortTypeimport javax.xml.namespace.QName import org.eclipse.emf.common.util.URIimport org.eclipse.emf.ecore.resource.Resourceimport org.eclipse.emf.ecore.resource.ResourceSetimport org.eclipse.emf.ecore.resource.impl.ResourceSetImplimport org.eclipse.wst.wsdl.Definitionimport org.eclipse.wst.wsdl.Typesimport org.eclipse.wst.wsdl.internal.impl.PartImplimport org.eclipse.wst.wsdl.internal.util.WSDLResourceFactoryImplimport org.eclipse.wst.wsdl.util.WSDLResourceImplimport org.eclipse.xsd.XSDElementDeclarationimport org.eclipse.xsd.XSDSchemaimport org.eclipse.xsd.util.XSDResourceImplimport org.junit.Testimport org.junit.Before public class WSDLParserWithEclipse { Definition definition=null String wsdlPathString="E:/HelloEclipse-EMF-WSDL-XSD/test.wsdl" @Before public void setup(){ ResourceSet resourceSet = new ResourceSetImpl() Resource.Factory.Registry registry = resourceSet.getResourceFactoryRegistry() Map extensionToFactoryMap = registry.getExtensionToFactoryMap() extensionToFactoryMap.put("wsdl", new WSDLResourceFactoryImpl()) File wsdlFile =new File(wsdlPathString) URI uri = URI.createFileURI(wsdlFile.getAbsolutePath()) // You can avoid this cast, but will have to cast anyway later to get the Definition out the resource contents WSDLResourceImpl wsdlResource = (WSDLResourceImpl) resourceSet.createResource(uri) try { wsdlResource.load(null) definition = wsdlResource.getDefinition() }catch(Exception e){ e.printStackTrace() } } @Test public void testTypes(){ Types types = definition.getETypes() List schemas = types.getSchemas("http://www.xxxxx.com/problem") XSDSchema schema = (XSDSchema) schemas.get(0) org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, schema.getElement())} @Test public void testMessage(){ Map messages=definition.getMessages() System.out.println("The message size is:"+messages.size()) Set setMessages=messages.keySet() Iterator iteratorMessages=setMessages.iterator() while(iteratorMessages.hasNext()){ QName key=(QName)iteratorMessages.next() Message message=(Message)messages.get(key) //{http://www.xxxxx.com/problem}getKeysSoapIn //System.out.println("Message Name:"+message.getQName()) if(message.getQName().toString().indexOf("getKeysSoapIn")>0){ System.out.println("Message Name:"+message.getQName()) Map partsMap=message.getParts() //org.eclipse.xsd.impl.XSDElementDeclarationImpl System.out.println("Message Part size for getKeysSoapIn message is:"+partsMap.size()) PartImpl part= (PartImpl)partsMap.get("problem") XSDElementDeclaration xsdElementDeclaration=part.getElementDeclaration() XSDResourceImpl.serialize(System.out, xsdElementDeclaration.getElement()) } } } @Test public void testPortType(){ Map portTypes=definition.getPortTypes() System.out.println("Port Type size:"+portTypes.size()) if(portTypes!=null&&portTypes.size()>0){ Set set=portTypes.keySet() Iterator iterator=set.iterator() while(iterator.hasNext()){ QName object=(QName)iterator.next() PortType portType=(PortType)portTypes.get(object) System.out.println("Port Type name:"+portType.getQName()) org.eclipse.xsd.util.XSDResourceImpl.serialize(System.out, portType.getDocumentationElement()) } } } }
xsd就是xml schema,可以利用以下方法生成xml:Schema文件定义如下:<o:p></o:p>
<!----><o:p></o:p>
<xs:schema xmlns="http://mydomain.com/myApp"xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http://mydomain.com/myApp" elementFormDefault="qualified"attributeFormDefault="unqualified"><o:p></o:p>
<xs:element name="e1"><o:p></o:p>
<xs:complexType><o:p></o:p>
<xs:sequence><o:p></o:p>
<xs:element name="e11" type="xs:int"/前雹><o:p></o:p>
<xs:element name="e12"><o:p></o:p>
<xs:complexType><o:p></o:p>
<xs:attribute name="e121" type="xs:string" use="required"/><o:p></o:p>
<xs:attribute name="e122" type="xs:long"/><o:p></o:p>
xs:complexType><o:p></o:p>
xs:element><o:p></o:p>
xs:sequence><o:p>团御</o:p>
xs:complexType><o:p></o:p>
xs:element><o:p></o:p>
xs:schema><o:p></o:p>
<o:p></o:p>
解析代码如下:
import org.apache.xmlbeans.*
import com.mydomain.myApp.E1Document
public class OutputXml {
/**
* 其中e11=11 e121=”e121”
* @param args
*/
public static void main(String args[]) {
E1Document e1Document
E1Document.E1 E1Element
E1Document.E1.E12 E1e2Element
XmlOptions xmlOptions
e1Document = E1Document.Factory.newInstance()
E1Element = e1Document.addNewE1()
E1e2Element=E1Element.addNewE12()
E1Element.setE11(11)
E1e2Element.setE121("e121")
/**
* 如果需要设置setE122的值塌悔岩请打开,由于xsd文件定义的元素不是必须的.
*/
//E1e2Element.setE122()
xmlOptions = new XmlOptions()
xmlOptions.setSavePrettyPrint()
xmlOptions.setSavePrettyPrintIndent(4)
String xmlStr = e1Document.xmlText(xmlOptions)
System.out.println("XML Instance Document is : \n" + xmlStr )
}
}
这有个.java的验证手高方法修改相应路径后可直接运行验证
package test
import java.io.File
import javax.xml.XMLConstants
import javax.xml.parsers.FactoryConfigurationError
import javax.xml.parsers.ParserConfigurationException
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder
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
public class TestDOMValidation
{
public static void main(String[] args)
{
try
{
// Get Document Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
// Leave off validation, and turn off namespaces
factory.setValidating(false)
factory.setNamespaceAware(false)
DocumentBuilder builder = factory.newDocumentBuilder()
Document doc = builder.parse(new File("data/test/shiporder.xml"))
//Document doc = builder.parse(new File("data/test/simple.xml"))
// SchemaFactory is a schema compiler. It reads external representations of schemas and
//毕槐尺 prepares them for validation.
SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
// Source: an object that implements this interface contains the information needed to act
/明禅/ as source input (XML source or transformation instructions).
Source constraints = new StreamSource(new File("data/test/shiporder.xsd"))
//Source constraints = new StreamSource(new File("data/test/simple.xsd"))
// Schema object represents a set of constraints that can be checked/ enforced against an
// XML document.
Schema schema = constraintFactory.newSchema(constraints)
// Validator is a processor that checks an XML document against Schema.
Validator validator = schema.newValidator()
// Validate the DOM tree
try
{
validator.validate(new DOMSource(doc))
System.out.println("Document validates fine.")
}
catch (org.xml.sax.SAXException e)
{
System.out.println("Validation error: " + e.getMessage())
}
}
catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support the requested features.")
}
catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining Document Builder Factory.")
}
catch (Exception e)
{
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)