xml和Java Bean互转

    /**
     * 将XML字符串转换为指定类型的Java对象
     * @param xmlStr XML字符串
     * @param clazz 对象的类型
     * @param  对象类型
     * @return 转换后得到的对象
     * @throws RuntimeException 如果转换失败,则抛出运行时异常
     */
    public static <T> T xmlToBean(String xmlStr, Class<T> clazz) {
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (T) unmarshaller.unmarshal(new StringReader(xmlStr));
        } catch (JAXBException e) {
            // 将JAXBException转换为更具体的异常类型,或者添加堆栈跟踪信息。
            // 这里可以包装成一个具体的XML解析异常,例如XMLParsingException,并添加错误信息和原始异常。
            throw new RuntimeException("XML parsing failed for class: " + clazz.getName(), e);
        }
    }

    /**
     * 将Java对象转换为XML字符串
     * @param bean 要转换的Java对象
     * @return 转换后的XML字符串
     * @throws JAXBException 当转换发生错误时抛出该异常
     */
    public static String beanToXml(Object bean) {
        try {
            JAXBContext context = JAXBContext.newInstance(bean.getClass());
            Marshaller marshaller = context.createMarshaller();

            // 这意味着在生成的XML中不会包含XML声明(如)
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
            StringWriter writer = new StringWriter();
            marshaller.marshal(bean, writer);
            return writer.toString();
        } catch (JAXBException e) {
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(基础知识,xml,java,开发语言)