前端之家收集整理的这篇文章主要介绍了
XML解析文件,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package fax.util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.apache.commons.lang.StringUtils;
import net.coobird.thumbnailator.Thumbnails;
public class Test {
private String endPoint;
private String AspId;
private String TradeCode;
String namespaceURI = "TradeService";
Document document = null;
public String errInfo = "";
public String succInfo = "";
String paraSerial = "serial";
String paraRand = "rand";
String paraEncode = "encode";
/**** * 根据传进去的节点名,返回该节点的值 * @param nodeName * @return * @throws Exception */
public String getSingleNodeValue(String nodeName) throws Exception {
String ret = "";
NodeList nodeList = document.getElementsByTagName(nodeName);
if (nodeList.item(0) == null) {
System.out.println("Can't find node!" + nodeName);
return "";
}
if (nodeList.item(0).getFirstChild() != null) {
ret = nodeList.item(0).getFirstChild().getNodeValue().trim();
}
return ret;
}
/**** * 把一个字符串转成XML类型 * @param content * @throws Exception */
public void loadXml(String content) throws Exception {
InputStream is = new ByteArrayInputStream(content.getBytes());
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(is);
}
/**** * 调用远程接口并获得返回的值 * @param endPoint * @param namespaceURI * @param localPart * @param args * @return */
public String getWebServiceResult(String endPoint,String namespaceURI,String localPart,Object[] args) {
String ret = null;
try {
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(endPoint));
call.setOperationName(new QName(namespaceURI,localPart));
ret = (String) call.invoke(args);
} catch (Exception e) {
}
return ret;
}
/**** * 对所有功能进行操作 * @param areaid * @param EpId * @param Account * @param Pwd * @return */
public String checkAccountPwd(String endPoint,String Name,String Account,String Pwd) {
/*** * 第一个参数和第二个是连接地址,第三个是方法名,最后个是参数。 */
String resultXml = this.getWebServiceResult(endPoint,endPoint,Name,new Object[] { "" });
System.out.println(resultXml);
try {
this.loadXml(resultXml);
} catch (Exception e) {
}
String retunstatus = "";
String usertype = "";
try {
retunstatus = this.getSingleNodeValue(Pwd);
} catch (Exception e) {
e.printStackTrace();
}
return retunstatus;
}
/** * 获取节点内容 * @description * @param stringxml * @param key * @return */
public String check(String stringxml,String key) throws Exception {
this.loadXml(stringxml);
String retunstatus = "";
retunstatus = this.getSingleNodeValue(key);
return retunstatus;
}
}
原文链接:https://www.f2er.com/xml/297163.html