对象转xml格式工具类

import com.ruiyun.gui.store.haikang.haikang.bean.FCSearchDescription;
import com.ruiyun.gui.store.haikang.haikang.bean.FDSearchDescription;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class ObjToXmlUtil {
    public static String toXml(Object obj) {
        XStream xstream = new XStream(new DomDriver("utf8"));
        StringBuilder builder = new StringBuilder();
        if(FDSearchDescription.class.isInstance(obj)){
            xstream.alias("FDSearchDescription", FDSearchDescription.class);
            String xml = xstream.toXML(obj);
            builder.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?>\n");
            builder.append(xml);
        }else if (FCSearchDescription.class.isInstance(obj)){
            xstream.alias("FCSearchDescription", FCSearchDescription.class);
            String xml = xstream.toXML(obj);
            builder.append("<?xml  version=\"1.0\" encoding=\"UTF-8\"?>\n");
            builder.append(xml);
        }
        return builder.toString();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,可以使用dom4j和fastjson库来实现XMLJSON的功能。具体步骤如下: 1.使用dom4j解析XML文件,将其换为Document对象。 ```java SAXReader reader = new SAXReader(); Document document = reader.read(new File("example.xml")); ``` 2.使用XPath表达式选择需要换的节点,并将其换为JSON对象。 ```java JSONObject jsonObject = new JSONObject(); Element root = document.getRootElement(); List<Element> elements = root.selectNodes("//book"); for (Element element : elements) { JSONObject bookObject = new JSONObject(); bookObject.put("id", element.attributeValue("id")); bookObject.put("name", element.elementText("name")); bookObject.put("author", element.elementText("author")); bookObject.put("price", element.elementText("price")); jsonObject.put(element.attributeValue("id"), bookObject); } ``` 3.使用fastjson将JSON对象换为JSON字符串。 ```java String jsonString = JSON.toJSONString(jsonObject); ``` 完整代码如下: ```java import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.File; import java.util.List; public class XmlToJsonUtil { public static String xmlToJson(String xmlFilePath) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read(new File(xmlFilePath)); JSONObject jsonObject = new JSONObject(); Element root = document.getRootElement(); List<Element> elements = root.selectNodes("//book"); for (Element element : elements) { JSONObject bookObject = new JSONObject(); bookObject.put("id", element.attributeValue("id")); bookObject.put("name", element.elementText("name")); bookObject.put("author", element.elementText("author")); bookObject.put("price", element.elementText("price")); jsonObject.put(element.attributeValue("id"), bookObject); } return JSON.toJSONString(jsonObject); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值