gpt4 book ai didi

java - 如何将 Restful 服务上的字符串转换为 xml 文件以进行 xsd 验证

转载 作者:行者123 更新时间:2023-11-30 03:32:20 25 4
gpt4 key购买 nike

更新的问题

我已经将我的客户端代码和restful服务代码修改为

更新后的客户端代码是

URL url = new URL("http://localhost:9090/XsdValidation/api/data/xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

////


StringBuilder builder = new StringBuilder();
FileReader fileReader = new FileReader("Employee.xml");
BufferedReader reader = new BufferedReader(fileReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder .append(line);
}
String xml = builder .toString();
System.out.println("xml file is "+xml);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(xml);
out.close();
reader.close();

更新后的rest ful服务代码是

@Path("/data")
public class DataAccess {
@POST
@Path("/xml")
@Consumes(MediaType.APPLICATION_XML)
public String readXml(String file) {

System.out.println("in xml");
return file;
}

}

一切都很好,我得到的字符串是 <?xml version="1.0" encoding="UTF-8"?><empns:employee xmlns:empns="Symplocus/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="Symplocus/Employee Employee.xsd "> <empns:EMP_ID>0101</empns:EMP_ID> <empns:NAME>Rajasekhar</empns:NAME> <empns:SALARY>2000</empns:SALARY> <empns:DATEOFJOINING>2001-01-01</empns:DATEOFJOINING></empns:employee>

我想将整个字符串转换为 xml 文件,以便使用 xsd 进行验证..任何人都可以有将字符串转换为 XML 文件的想法

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////这是我之前从 GAgarwarl 的帖子中得到答案的问题///我正在更新问题,现在我能够将 xml 文件发送到 Restful 服务,如何阅读此内容在 Restful 服务上读取此 xml

我的客户端代码是

URL url = new URL("http://localhost:9090/XsdValidation/api/data/xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");

OutputStream os = connection.getOutputStream();

TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader("Employee.xml");
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);

os.flush();

System.out.println(connection.getResponseCode());
//connection.disconnect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("in input stream");
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(connection.getResponseCode());

System.out.println("end of client programme");

我的安静代码是

@Path("/data")
public class DataAccess {
@POST
@Path("/xml")
@Consumes(MediaType.APPLICATION_XML)
public String readXml(/here i have to read file /) {
// here i want an idea to read the file sent by client

}

}

////这是老问题////

我对 Web 服务完全陌生,我必须在系统位置发送一个 XML 文件,如 c:/Files/Samle.xml从java客户端到Restful Web服务,之前我已经将json对象发送到Restful但无法发送xml

我发送 json 的客户端代码是

String tableName="SYMPLOCUS.IMDB1_FINANCE_BUDGE ";

String urlString="http://localhost:9090/DataAccess/api/DataAccess/loadData?tableName="+tableName;

URL url=new URL(urlString);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestMethod("POST");
httpCon.setRequestProperty("Content-Type",
"application/json");

OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream());
String json = "{\"EMPCODE\":\"125\", \"NAME_TBH\":\"aaaaa\"}";
out.write(json);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
System.out.println("in input stream");
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
System.out.println(httpCon.getResponseCode());

我的安静代码是

@Path("/loadData")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public String sendReceiveJson(String data,@QueryParam("tableName") String tableName)

{
}

以同样的方式,我想将 xml 文件发送到 Restful 服务,有人可以有任何想法吗?

最佳答案

替换 String json = "{\"EMPCODE\":\"125\",\"NAME_TBH\":\"aaaaa\"}";与:

StringBuilder builder = new StringBuilder();
FileReader fileReader = new FileReader("fileName");
BufferedReader reader = new BufferedReader(fileReader);
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
builder .append(line);
}
String xml = builder .toString();

关于java - 如何将 Restful 服务上的字符串转换为 xml 文件以进行 xsd 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28735573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com