java http 接收xml参数_读写xml并使用xml文件进行参数传递给接口获取返回内容——wishPost请求物流单号为例...
时间: 2024-03-21 13:43:35 浏览: 58
Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException
首先,我们可以使用Java中的DOM或SAX库来读写XML文件。以下是使用DOM库的示例代码:
```java
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
// 读取XML文件
public static void readXML(String filePath) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(filePath);
// 获取根节点
Element root = doc.getDocumentElement();
// 获取子节点
NodeList nodeList = root.getElementsByTagName("wishPost");
Node node = nodeList.item(0);
// 获取物流单号
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String logisticsNo = element.getElementsByTagName("logisticsNo").item(0).getTextContent();
System.out.println("物流单号:" + logisticsNo);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 写入XML文件
public static void writeXML(String filePath, String logisticsNo) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
// 创建根节点
Element root = doc.createElement("root");
doc.appendChild(root);
// 创建子节点
Element wishPost = doc.createElement("wishPost");
root.appendChild(wishPost);
Element logisticsNoNode = doc.createElement("logisticsNo");
logisticsNoNode.appendChild(doc.createTextNode(logisticsNo));
wishPost.appendChild(logisticsNoNode);
// 写入XML文件
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
} catch (Exception e) {
e.printStackTrace();
}
}
```
然后,我们可以使用Java的HttpURLConnection类来发送HTTP请求,并将XML文件作为请求参数。以下是示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public static void sendPostRequest(String url, String xmlFilePath) {
try {
// 读取XML文件
BufferedReader br = new BufferedReader(new FileReader(xmlFilePath));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
String xmlString = sb.toString();
br.close();
// 发送POST请求
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
con.setRequestProperty("Content-Type", "application/xml");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(xmlString.getBytes("UTF-8"));
os.flush();
os.close();
// 获取返回内容
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
sb = new StringBuilder();
while ((line = in.readLine()) != null) {
sb.append(line);
}
String response = sb.toString();
in.close();
System.out.println("返回内容:" + response);
} catch (Exception e) {
e.printStackTrace();
}
}
```
以上代码演示了如何使用XML文件作为参数发送HTTP请求,并从响应中获取返回内容。其中url参数是接口的URL地址,xmlFilePath参数是XML文件的路径。
阅读全文