加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML解析文件

发布时间:2020-12-16 06:27:19 所属栏目:百科 来源:网络整理
导读: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.C
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;
//XML解析文件
public class Test {
    private String endPoint;

    private String AspId; //AP提供商编号

    private String TradeCode;

    String namespaceURI = "TradeService";

    Document document = null;

    public String errInfo = "";

    public String succInfo = "";

    String paraSerial = "serial";//序号

    String paraRand = "rand";//随机码

    String paraEncode = "encode";//hashcode

    /**** * 根据传进去的节点名,返回该节点的值 * @param nodeName * @return * @throws Exception */
    public String getSingleNodeValue(String nodeName) throws Exception {
        String ret = "";
        NodeList nodeList = document.getElementsByTagName(nodeName);
        if (nodeList.item(0) == null) {
// throw new Exception("Can't find node!" + nodeName);
            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) {
        // endPoint="http://192.168.0.110:8080/ZZweb/ProductPurchaseImpl.jws";
        /*** * 第一个参数和第二个是连接地址,第三个是方法名,最后个是参数。 */
        String resultXml = this.getWebServiceResult(endPoint,endPoint,Name,new Object[] { "" });
        System.out.println(resultXml);
        try {
            this.loadXml(resultXml);
        } catch (Exception e) {
            // this.printErrorMsg("数据传送出错!");
        }
        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;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读