zoukankan      html  css  js  c++  java
  • JAXBContext中bean和xml之间的转换,以及xml相关的方法

    @Data
    @AllArgsConstructor
    @NoArgsConstructor //需要无惨构造器
    @XmlRootElement
    public class ClassA {
    
        private String file1;
        private String file2;
    
    }

    测试类

    public class jbTest {
        public static void main(String[] args) {
            // bean转换成xml
            ClassA a=new ClassA("hello string","hello int");
            System.out.println(a);
            try {
                JAXBContext context = JAXBContext.newInstance(ClassA.class);
                Marshaller marshaller = context.createMarshaller();
                marshaller.marshal(a,System.out);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
    
            printLine();
    
            // xml转换成bean
            String xmlStr="<?xml version="1.0" encoding="UTF-8" standalone="yes"?>" +
                    "<classA><file1>hello string</file1><file2>hello int</file2></classA>";
            try {
                JAXBContext context = JAXBContext.newInstance(ClassA.class);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                ClassA a2 = (ClassA) unmarshaller.unmarshal(new StringReader(xmlStr));
                System.out.println(a2);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
    
        }
        public static void printLine(){
            System.out.println("===============================");
        }
    }

    XStream使用(序列号xml)
    导入pom文件
            <!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
            <dependency>
                <groupId>com.thoughtworks.xstream</groupId>
                <artifactId>xstream</artifactId>
                <version>1.4.11.1</version>
            </dependency>

    测试代码

            XStream stream=new XStream();
            stream.alias("hellowolrd",String.class);
            String str = stream.toXML("str");
            System.out.println(str);
        //     结果:
        //    <hellowolrd>str</hellowolrd>

    这是基本用法,然后可以根据自己的需求扩展


    Document使用拼接xml格式的文件
    导入pom文件
            <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>1.6.1</version>
            </dependency>

    测试代码和结果:

            Document root= DocumentHelper.createDocument();
            Element html = root.addElement("html");
            html.addElement("body").addText("txt_body");
            html.addElement("head").addText("txt_head");
            System.out.println(root.asXML());
    //        结果
    //        <?xml version="1.0" encoding="UTF-8"?>
    //      <html><body>txt_body</body><head>txt_head</head></html>
  • 相关阅读:
    2021“MINIEYE杯”中国大学生算法设计超级联赛2
    2021“MINIEYE杯”中国大学生算法设计超级联赛1
    2021牛客暑期多校训练营3
    2021牛客暑期多校训练营1
    对点分治的一些新理解
    使用均摊分析证明Splay复杂度
    从实际项目中学设计模式:策略模式与模板模式的应用
    ueditor编辑器html模式下无法保存内容
    记录一次项目开发中遇到的问题
    加解密代码
  • 原文地址:https://www.cnblogs.com/windy13/p/11794527.html
Copyright © 2011-2022 走看看