关于javaxmltype的信息
本篇文章给大家谈谈javaxmltype,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java中怎么向class外的注释中传递值?
@XmlType(namespace = XX) ///////里面的值,必需是常量。静态的方法,都不能
public class test{
静态的变量,要final 修饰
java怎么调用xml的webservice服务
Java通过WSDL文件来调用webservice:
注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。
1,直接AXIS调用远程的web service
这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:
import java.util.Date;
import java.text.DateFormat;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import javax.xml.namespace.QName;
import java.lang.Integer;
import javax.xml.rpc.ParameterMode;
public class caClient {
public static void main(String[] args) {
try {
String endpoint = "";
//直接引用远程的wsdl文件
//以下都是套路
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.setOperationName("addUser");//WSDL里面描述的接口名称
call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,
javax.xml.rpc.ParameterMode.IN);//接口的参数
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
String temp = "测试人员";
String result = (String)call.invoke(new Object[]{temp});
//给方法传递参数,并且调用方法
System.out.println("result is "+result);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
2,直接SOAP调用远程的webservice
这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import java.io.*;
import java.net.*;
import java.util.Vector;
public class caService{
public static String getService(String user) {
URL url = null;
try {
url=new URL("");
} catch (MalformedURLException mue) {
return mue.getMessage();
}
// This is the main SOAP object
Call soapCall = new Call();
// Use SOAP encoding
soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
// This is the remote object we're asking for the price
soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");
// This is the name of the method on the above object
soapCall.setMethodName("getUser");
// We need to send the ISBN number as an input parameter to the method
Vector soapParams = new Vector();
// name, type, value, encoding style
Parameter isbnParam = new Parameter("userName", String.class, user, null);
soapParams.addElement(isbnParam);
soapCall.setParams(soapParams);
try {
// Invoke the remote method on the object
Response soapResponse = soapCall.invoke(url,"");
// Check to see if there is an error, return "N/A"
if (soapResponse.generatedFault()) {
Fault fault = soapResponse.getFault();
String f = fault.getFaultString();
return f;
} else {
// read result
Parameter soapResult = soapResponse.getReturnValue ();
// get a string from the result
return soapResult.getValue().toString();
}
} catch (SOAPException se) {
return se.getMessage();
}
}
}
3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。
这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。
首先 java org.apache.axis.wsdl.WSDL2Java
原本的网址是,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。
那么就会出现4个文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,在使用的时候,引入这个接口即可,就好像使用本地类一样。
Java怎么实现wsdl发送xml
步骤如下:
1.下载AXIS2类库,AXIS2是目前java调用webservice的一个主要方法(由于更新较频繁,请自行google该类库的网址)
2.由于是第三方webservice,直接引入AXIS2的包就可以
代码如下:
import java.rmi.RemoteException;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
public class webServiceTest {
public String invokeRemoteFuc() {
String endpoint = "";
String result = "no result!";
Service service = new Service();//新建一个service
Call call;
Object[] object = new Object[1];
object[0] = "Dear I miss you";//Object是用来存储方法的参数
try {
call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);// 远程调用路径
call.setOperationName("say");// 调用的方法名
// 设置参数名:
call.addParameter("str1", // 参数名
XMLType.XSD_STRING,// 参数类型:String
ParameterMode.IN);// 参数模式:'IN' or 'OUT'
// 设置返回值类型:
call.setReturnType(XMLType.XSD_STRING);// 返回值类型:String
result = (String) call.invoke(object);// 远程调用
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return result;//返回值
}
public static void main(String[] args) {
webServiceTest t = new webServiceTest();
String result = t.invokeRemoteFuc();
System.out.println(result); //输出
}
}
通过AXIS2封装好的类设置URL和参数,直接调用。
Java对下面XML文档解析要求封装为两个类
建议你用JAXB,简单实用。我根据你的要求写了个例子,应该满足你的要求。
import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder={"dbtype","url","user", "password"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="datasource")
public class Datasource {
@XmlElement(name="db-type")
private String dbtype;
@XmlElement(name="url")
private String url;
@XmlElement(name="user")
private String user;
@XmlElement(name="password")
private String password;
public Datasource() {
super();
}
public Datasource(String dbtype, String url, String user, String password) {
super();
this.dbtype = dbtype;
this.url = url;
this.user = user;
this.password = password;
}
public String getDbtype() {
return dbtype;
}
public void setDbtype(String dbtype) {
this.dbtype = dbtype;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Datasource [dbtype=" + dbtype + ", url=" + url + ", user="
+ user + ", password=" + password + "]";
}
}
import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder={"sql","time","column","allyCode"})
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name="news-ws")
public class NewsWs {
@XmlElement(name="sql")
private String sql;
@XmlElement(name="time")
private String time;
@XmlElement(name="column")
private String column;
@XmlElement(name="ally-code")
private String allyCode;
public NewsWs() {
super();
}
public NewsWs(String sql, String time, String column, String allyCode) {
super();
this.sql = sql;
this.time = time;
this.column = column;
this.allyCode = allyCode;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getColumn() {
return column;
}
public void setColumn(String column) {
this.column = column;
}
public String getAllyCode() {
return allyCode;
}
public void setAllyCode(String allyCode) {
this.allyCode = allyCode;
}
@Override
public String toString() {
return "NewsWs [sql=" + sql + ", time=" + time + ", column=" + column
+ ", allyCode=" + allyCode + "]";
}
}
import java.util.Set;
import javax.xml.bind.annotation.XmlAccessOrder;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorOrder;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorOrder(XmlAccessOrder.ALPHABETICAL)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Context", propOrder = { "datasource", "newsWs" })
@XmlRootElement(name = "context")
public class Context {
@XmlElement(name="datasource")
private Datasource datasource;
@XmlElementWrapper(name="news-ws-define")
@XmlElement(name ="news-ws")
private SetNewsWs newsWs;
public Context() {
super();
}
public Context(Datasource datasource, SetNewsWs newsWs) {
super();
this.datasource = datasource;
this.newsWs = newsWs;
}
public Datasource getDatasource() {
return datasource;
}
public void setDatasource(Datasource datasource) {
this.datasource = datasource;
}
public SetNewsWs getNewsWs() {
return newsWs;
}
public void setNewsWs(SetNewsWs newsWs) {
this.newsWs = newsWs;
}
@Override
public String toString() {
return "Conext [datasource=" + datasource + ", newsWs=" + newsWs + "]";
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.HashSet;
import java.util.Set;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class Test {
public static void main(String[] args) throws Exception {
SetNewsWs set = new HashSetNewsWs();
Datasource datasource = new Datasource("Mysql",
"", "root", "123");
NewsWs newsWs1 = new NewsWs("sql1", "2013年7月13日", "新闻内容1", "验证1");
NewsWs newsWs2 = new NewsWs("sql2", "2013年7月14日", "新闻内容2", "");
NewsWs newsWs3 = new NewsWs("sql3", "2013年7月15日", "新闻内容3", "验证1");
set.add(newsWs1);
set.add(newsWs2);
set.add(newsWs3);
Context context = new Context(datasource, set);
JAXBContext jaxbContext = JAXBContext.newInstance(context.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
jaxbMarshaller.marshal(context, byteArrayOutputStream);
String xml = byteArrayOutputStream.toString("UTF-8");
System.out.println(xml);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElementContext jaxbElement = (JAXBElementContext) unmarshaller
.unmarshal(new StreamSource(byteArrayInputStream),
Context.class);
Context context1 = (Context) jaxbElement.getValue();
System.out.println(context1.getDatasource().toString());
SetNewsWs set1 = context1.getNewsWs();
for(NewsWs ws : set1) {
System.out.println("**************");
System.out.println(ws.getSql());
System.out.println(ws.getTime());
System.out.println(ws.getColumn());
System.out.println(ws.getAllyCode());
}
}
}
代码里面我用了Set导致有时生成的xml文件中tagnews-ws顺序打乱了,如果想按集合添加顺序来生成xml,只需要把Set用List替换即可。
@XmlElementWrapper(name="news-ws-define")
@XmlElement(name ="news-ws")
private ListNewsWs newsWs;
里面所用到的jar全部都是jdk自带的,无需第三方包。
执行结果:前半部是对象转XML,后半部是XML转对象。
?xml version="1.0" encoding="UTF-8" standalone="yes"?
context
datasource
db-typeMysql/db-type
url;/url
userroot/user
password123/password
/datasource
news-ws-define
news-ws
sqlsql1/sql
time2013年7月13日/time
column新闻内容1/column
ally-code验证1/ally-code
/news-ws
news-ws
sqlsql2/sql
time2013年7月14日/time
column新闻内容2/column
ally-code/ally-code
/news-ws
news-ws
sqlsql3/sql
time2013年7月15日/time
column新闻内容3/column
ally-code验证1/ally-code
/news-ws
/news-ws-define
/context
Datasource [dbtype=Mysql, url=, user=root, password=123]
**************
sql2
2013年7月14日
新闻内容2
**************
sql1
2013年7月13日
新闻内容1
验证1
**************
sql3
2013年7月15日
新闻内容3
验证1
关于javaxmltype和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。