一、依赖包
需要导入dom4j包 下载地址:🛫飞机路径
二、实列方法以及思路
@Test
public void analysisXml(){
String xml = getHttpProductApi();
try {
xml = new String(xml.getBytes("GBK"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
Document document = DocumentHelper.parseText(xml);
document.setXMLEncoding("utf-8");
Element root = document.getRootElement();
Element header = root.element("Header");
Element productResponse = root.element("ProductResponse");
Iterator otherInfo = productResponse.elementIterator("OtherInfo");
while (otherInfo.hasNext()){
Element itemEle = (Element) otherInfo.next();
List<DefaultAttribute> attributes1 = itemEle.attributes();
for (DefaultAttribute attr : attributes1){
System.out.println(itemEle.getName()+":[ nodeName:"+attr.getName()+" nodeValue:"+ attr.getStringValue()+" stringValue:"+itemEle.getText()+"]\n");
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public String getHttpProductApi(){
String url = ".....";
String xmlHeader=
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<PriceAvailabilityRequest>\n" +
"\t<Header>\n" +
"\t\t<MessageId>WUXIAPPTEC-2021-11-08 13:25:15.072</MessageId>\n" +
"\t\t<Timestamp>2021-11-08 13:25:15.072</Timestamp>\n" +
"\t</Header>\n" +
"\t<RequesterInfo>\n" +
"\t\t<Customer code=\"WUXIAPPTEC_PACN\"/>\n" +
"\t\t<Location code=\"CN\"/>\n" +
"\t\t<PreferredLanguage>EN</PreferredLanguage>\n" +
"\t\t<PreferredCountry>CN</PreferredCountry>\n" +
"\t</RequesterInfo> \n" +
"\t\t<ProductRequest itemNumber=\"1\"> \n" +
"\t\t\t<SupplierPartNumber>195030-100ML</SupplierPartNumber> \n" +
"\t\t\t<UnitOfMeasure unit=\"\"/> \n" +
"\t\t\t<Quantity>1</Quantity> \n" +
"\t\t</ProductRequest>\n" +
"</PriceAvailabilityRequest>";
String returnMsg = post(url, xmlHeader);
System.out.println(returnMsg);
return returnMsg;
}
public String post(String url, String xmlFileName) {
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout");
HttpClient client = new HttpClient();
PostMethod myPost = new PostMethod(url);
client.setConnectionTimeout(300 * 1000);
String responseString = null;
try {
myPost.setRequestHeader("Content-Type", "text/xml");
myPost.setRequestHeader("charset", "utf-8");
myPost.setRequestBody(xmlFileName);
int statusCode = client.executeMethod(myPost);
if (statusCode == HttpStatus.SC_OK) {
BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());
byte[] bytes = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count = 0;
while ((count = bis.read(bytes)) != -1) {
bos.write(bytes, 0, count);
}
byte[] strByte = bos.toByteArray();
responseString = new String(strByte, 0, strByte.length, "utf-8");
bos.close();
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
myPost.releaseConnection();
return responseString;
}
如果报错:java.io.FileNotFoundException: 项目路径\xxx.xx (系统找不到指定的文件。)
在项目路径下加缺少的文件
|