xml转成java对象.怎么弄

我用java调的C#的webservice接口 对方直接返回的是DataSet类型,现在我想把这个xml转成java的具体封装,现在需求就是把刚刚那个xml转成java对象就行,哪位大神能懂指教一下

你可以借助于在线工具把xml转成JAVA类
xml转javaBean实体类在线转换工具—工具猫 xml java转换,xml生成Java转换工具,xml生成Java在线转换工具, xml转java,xml转javaBean,xml转javaBean在线工具,在线xml转javaBean,在线xml转java实体类在线生成,java实体类在线生成 http://www.toolscat.com/convert/xml-java

使用一些xml转换工具,如DOM,SAX,Digester,其中Digester可以把xml文件转成JavaBean对象

下面是我用的XML工具


package com.lockstone.las.voicelogger.util;

import com.lockstone.las.common.file.FileOperations;
import com.lockstone.las.common.util.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
import org.jsoup.parser.XmlTreeBuilder;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.*;

/**
 * @author 34745
 * @ClassName: XMLUtil
 * @Description: (XML字符串与实体类之间互转) 需要依赖xsream Jsoup 注意依赖版本问题
 * @date 2019年1月31日
 */
public class XmlUtil {

    private XmlUtil(){
        throw new IllegalStateException("Utility class");
    }

    /*日志打印*/
    private static Logger LOGGER = LoggerFactory.getLogger(XmlUtil.class);

    /**
     * @param clazz 类的字节码文件
     * @param xml   传入的XML字符串
     * @return XML字符串转实体类
     */
    public static <T> T parseFromXml(Class<T> clazz, String xml) {
        XStream xStream = new XStream(new DomDriver());
        xStream.processAnnotations(clazz);
        @SuppressWarnings("unchecked")
        T t = (T) xStream.fromXML(xml);
        return t;
    }

    /**
     * @param obj 实体类
     * @return 实体类转XML字符串
     */
    public static String toXml(Object obj) {
        XStream xStream = new XStream(new DomDriver());
        // 扫描@XStream注解
        xStream.processAnnotations(obj.getClass());
        // 注意! toXML时,会把date时间格式的数据自动转化为UTC时间 所以如果数据中的时间是UTC时间的话,转成XML后时间会不对,变成再次-时区的值
        return xStream.toXML(obj).replaceAll("\\_+", "_");//正则过滤双下划线转为单下划线
    }

    /**
     * @param xml       需要xml格式的字符串
     * @param elementId 节点名
     * @return
     * @Description: 获取xml格式字符串中指定的元素标签片段
     */
    public static String getElementString(String xml, String elementId) {
        if (StringUtils.isBlank(xml) || StringUtils.isBlank(elementId)) {
            return null;
        }
        // 获取document对象
        Document document = Jsoup.parse(xml, "", new Parser(new XmlTreeBuilder()));
        // 获取对应的document片段
        return document.select(elementId).toString().replaceAll("\\s*", "");
    }

    /**
     * @param xml
     * @param elementId
     * @param clazz
     * @return
     * @Description: xml字符串节点片段转对象
     */
    public static <T> T parseElementObj(String xml, String elementId, Class<T> clazz) {
        if (StringUtils.isBlank(xml) || StringUtils.isBlank(elementId)) {
            return null;
        }
        return parseFromXml(clazz, getElementString(xml, elementId));
    }

    /**
     * 将对象根据路径转换成xml文件
     *
     * @param obj
     * @param path
     * @return
     */
    public static void convertToXml(Object obj, String path) {
        try {
            // 利用jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            // 格式化xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // 将对象转换成输出流形式的xml
            // 创建输出流
            FileWriter fw = new FileWriter(path);
            marshaller.marshal(obj, fw);
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException eIO) {
            eIO.printStackTrace();
        }
    }

    /**
     * 将XML转为指定的POJO
     *
     * @param xmlStr
     * @return
     */
    public static String xmlFileToPojo(String xmlStr) {
        File file = new File(xmlStr);
        StringBuilder stringBuffer = new StringBuilder();
        if (!(file.exists())) {
            LOGGER.warn("Check if the configuration files in the directory exist: {}", xmlStr);
        }
        try (FileInputStream fis = new FileInputStream(xmlStr);
             InputStreamReader isr = new InputStreamReader(fis, "utf-8");
             BufferedReader br = new BufferedReader(isr);
        ) {
            String str;
            // 从文件系统中的某个文件中获取字节
            while ((str = br.readLine()) != null) {
                if (!str.equals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>")) {
                    stringBuffer.append(str);
                }
            }
        } catch (FileNotFoundException e) {
            LOGGER.error("The specified file could not be found: {}", e.getMessage());
        } catch (IOException e) {
            LOGGER.error("Failed to read the file: {}", e.getMessage());
        }
        return new String(stringBuffer);
    }

    /**
     * 将POJO转为指定的XML
     *
     * @param path
     * @param object
     * @return
     */
    public static boolean pojoToXmlFile(String path, Object object) {
        return FileOperations.writeTempFile(path, toXml(object), false);
    }
}