关闭

XMLParser 使用

1516人阅读 评论(0) 收藏 举报
分类:

XMLParser 工具类

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;
public class XMLParser {

    public static Map<String, Object> getMapFromXML(String xmlString)
            throws ParserConfigurationException, IOException, SAXException {

        // 这里用Dom的方式解析回包的最主要目的是防止API新增回包字段
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputStream is = Util.getStringStream(xmlString);
        Document document = builder.parse(is);

        // 获取到document里面的全部结点
        NodeList allNodes = document.getFirstChild().getChildNodes();
        Node node;
        Map<String, Object> map = new HashMap<String, Object>();
        int i = 0;
        while (i < allNodes.getLength()) {
            node = allNodes.item(i);
            if (node instanceof Element) {
                map.put(node.getNodeName(), node.getTextContent());
            }
            i++;
        }
        return map;

    }

    public static Object getObjectFromXML(String xml, Class<?> tClass) {
        // 将从API返回的XML数据映射到Java对象
        XStream xStreamForResponseData = new XStream();
        xStreamForResponseData.alias("xml", tClass);
        // xStreamForResponseData.ignoreUnknownElements();//暂时忽略掉一些新增的字段
        return xStreamForResponseData.fromXML(xml);
    }

    public static Object getObjectFromXML(String xml, String rootName,
            Class<?> tClass) {
        // 将从API返回的XML数据映射到Java对象
        XStream xStreamForResponseData = new XStream();
        xStreamForResponseData.alias(rootName, tClass);
        // xStreamForResponseData.ignoreUnknownElements();//暂时忽略掉一些新增的字段
        return xStreamForResponseData.fromXML(xml);
    }

    public static void main(String[] args) {
        Map<String, Object> map=new HashMap<String, Object>();
        String xmlstr="<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml> ";
        try {
            map=getMapFromXML(xmlstr);
            System.out.println(map);            

        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

XmlJSON 工具类

import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
public class XmlJSON {

    public static String xmlToJson(String xml) {
        XMLSerializer serializer = new XMLSerializer();
        return serializer.read(xml).toString();
    }

    public static String jsonToXML(String json) {
        JSONObject jobj = JSONObject.fromObject(json);

        XMLSerializer xmlSerializer = new XMLSerializer();
        // 根节点名称
        xmlSerializer.setRootName("xml");
        // 不对类型进行设置
        xmlSerializer.setTypeHintsEnabled(false);
        String xmlStr=xmlSerializer.write(jobj);
        xmlStr=xmlStr.split("\r\n")[1];
        log.info("转换后的参数:"+xmlStr);
        return xmlStr;
    }
}
1
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:77727次
    • 积分:1809
    • 等级:
    • 排名:千里之外
    • 原创:100篇
    • 转载:4篇
    • 译文:3篇
    • 评论:1条
    文章分类
    最新评论