欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

xml格式化工具有哪些(ue格式化xml快捷键)

程序员文章站 2023-12-03 13:31:46
使用的javax这个jar下的类,jaxb(java architecture for xml binding) 这个java和xml的互转标准如果感兴趣可以自行百度这里就不介绍概念,直接上代码。首先...

使用的javax这个jar下的类,jaxb(java architecture for xml binding) 这个java和xml的互转标准如果感兴趣可以自行百度这里就不介绍概念,直接上代码。


首先是工具类,这里比较简单,就没做生成实体文件,就是两个,一个对象转xml,一个xml转对像。

import javax.xml.bind.jaxbcontext;
import javax.xml.bind.marshaller;
import javax.xml.bind.unmarshaller;
import java.io.stringreader;
import java.io.stringwriter;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;

/**
 * @desc: xml 工具类
 * @author: wangzhh
 * @date: 2019-06-13
 */
public class xmlutils {

    /**
     * description bean对像转换为xml字符串,
     * 对像属性如果不设置值,xml字符串没有节点,如果需要空节点,可以设置为空字符串
     * @param obj 需要转换的对像,转换的对像需要做对应的注解
     * @return java.lang.string
     * @author wangzhh
     * @date 2019-06-13
     **/
    public static string converttoxml(object obj) {
        // 输出流
        stringwriter writer = new stringwriter();
        try {
            jaxbcontext context = jaxbcontext.newinstance(obj.getclass());
            marshaller marshaller = context.createmarshaller();
            // 格式化xml输出的格式
            marshaller.setproperty(marshaller.jaxb_formatted_output, boolean.true);
            //marshaller.setproperty(marshaller.jaxb_formatted_output, boolean.false);
            // 设置编码集
            marshaller.setproperty(marshaller.jaxb_encoding, "utf-8");
            // 将对象转换成输出流形式的xml
            marshaller.marshal(obj, writer);
        } catch (exception e) {
            e.printstacktrace();
        }
        return writer.tostring();
    }

    /**
     * description xml字符串转换为bean对像
     * @author wangzhh
     * @date 2019-06-13
     * @param clazz
     * @param xmlstr
     * @return java.lang.object
     **/
    public static object convertxmlstrtoobject(class clazz, string xmlstr) {

        object xmlobject = null;
        try {
            jaxbcontext context = jaxbcontext.newinstance(clazz);
            // 进行将xml转成对象
            unmarshaller unmarshaller = context.createunmarshaller();
            stringreader reader = new stringreader(xmlstr);
            xmlobject = unmarshaller.unmarshal(reader);
        } catch (exception e) {
            e.printstacktrace();
        }
        return xmlobject;
    }

    public static void main(string[] args) {
        testxmlbean t = new testxmlbean();
        t.setname("张三");
        t.setage("");
        map<string,object> map = new hashmap<string, object>();
        map.put("map","mapvalue");
        t.setmsgmap(map);
        list<testlistbean> listbean = new arraylist<testlistbean>();
        testlistbean t1 = new testlistbean();
        t1.setaddress("一号地址");
        testlistbean t2 = new testlistbean();
        t2.setaddress("二号地址");
        listbean.add(t1);
        listbean.add(t2);
        t.setlistbean(listbean);
        system.out.println(xmlutils.converttoxml(t));
        string xmlstr  = xmlutils.converttoxml(t);
        xmlstr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><test><name>张三</name><age></age><msgmap><entry><key>map</key><value>mapvalue</value></entry></msgmap><listbean><address>一号地址</address></listbean><listbean><address>二号地址</address></listbean></test>\n";
        testxmlbean bean = (testxmlbean) xmlutils.convertxmlstrtoobject(testxmlbean.class,xmlstr);
        system.out.println(bean.tostring());
    }
}

测试用的类,饱含集合

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlrootelement;
import javax.xml.bind.annotation.xmltype;
import java.io.serializable;
import java.util.list;
import java.util.map;

/**
 * @desc: 工具类测试对像
 * @author: wangzhh
 * @date: 2019-06-13
 */
@xmlaccessortype(xmlaccesstype.field)
// 根节点
@xmlrootelement(name = "test")
// 需要转换的属性,顺序
@xmltype(proporder = {"name","age","msgmap","listbean"})
public class testxmlbean implements serializable {

    private static final long serialversionuid = -6854694430588003755l;

    private  string name;
    private string age;
    private map<string,object> msgmap;
    private list<testlistbean> listbean;

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }

    public string getage() {
        return age;
    }

    public void setage(string age) {
        this.age = age;
    }

    public map<string, object> getmsgmap() {
        return msgmap;
    }

    public void setmsgmap(map<string, object> msgmap) {
        this.msgmap = msgmap;
    }

    public list<testlistbean> getlistbean() {
        return listbean;
    }

    public void setlistbean(list<testlistbean> listbean) {
        this.listbean = listbean;
    }

    @override
    public string tostring() {
        return "testxmlbean{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", msgmap=" + msgmap +
                ", listbean=" + listbean +
                '}';
    }
}

import javax.xml.bind.annotation.xmlaccesstype;
import javax.xml.bind.annotation.xmlaccessortype;
import javax.xml.bind.annotation.xmlrootelement;
import javax.xml.bind.annotation.xmltype;

/**
 * @desc:
 * @author: wangzhh
 * @date: 2019-06-13
 */
@xmlaccessortype(xmlaccesstype.field)
// 根节点
@xmlrootelement(name = "listbean")
// 需要转换的属性
@xmltype(proporder = {"address"})
public class testlistbean {
    private string address;

    public string getaddress() {
        return address;
    }

    public void setaddress(string address) {
        this.address = address;
    }

    @override
    public string tostring() {
        return "testlistbean{" +
                "address='" + address + '\'' +
                '}';
    }
}

测试结果,控制台成功输出

connected to the target vm, address: '127.0.0.1:49443', transport: 'socket'
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<test>
    <name>张三</name>
    <age></age>
    <msgmap>
        <entry>
            <key>map</key>
            <value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">mapvalue</value>
        </entry>
    </msgmap>
    <listbean>
        <address>一号地址</address>
    </listbean>
    <listbean>
        <address>二号地址</address>
    </listbean>
</test>

testxmlbean{name='张三', age='', msgmap={map=[value: null]}, listbean=[testlistbe