码迷,mamicode.com
首页 > 其他好文 > 详细

XML解析文件

时间:2015-02-28 11:31:34      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

XML解析文件

标签:

原文地址:http://blog.csdn.net/u011328015/article/details/43982995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!