Java and XML mapping tool

Java and XML mapping tool

Friends familiar with Hibernate know that it can map Java classes to database tables, and table records can be updated by manipulating Java objects. This can greatly increase our development efficiency and avoid the tedious process of directly operating database tables through JDBC. In fact, Mybatis is similar, except that it is semi-automatic and needs to write SQL by itself. When using Java to develop XML-based operations, will you also want a mapping relationship that can directly establish a corresponding XML based on Java classes, and then can directly convert Java objects to corresponding XML, or can directly convert XML to What about the corresponding Java object tools? If you are looking for such a tool, then JAXB can meet your needs. JAXB is a Java and XML binding (mapping) tool provided by Java. After the corresponding relationship is established, the mutual conversion between Java objects and XML can be realized directly through a simple API.

Suppose there is a User class with the following structure. Its object needs to be converted into XML whose root node is user, and each attribute of the User object needs to be mapped to a byte point under the user node of XML. Then we only need to simply mark @XmlRootElement on the User class, which means that the class User needs to be mapped to a root node of XML, and the corresponding node name is user (this is the default policy of JAXB, the first letter of the class name is taken by default lowercase as the root node name), if you do not want to use the default name, you can also specify the name you want through the name attribute, as in the following example. Just add a comment and you're done. Isn't it very simple?

@XmlRootElement(name="user")
public class User {
    
    private Integer id;
    private String name;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

At this time, we use the following code to test. The core code is JAXB.marshal(user, System.out)that it means that the user object needs to be converted into the corresponding XML form and output to the console (System.out), where the JAXB class is Java self-contained. With a JAXB tool class.

@Test 
public  void testXml() throws Exception {
     // Construct User object 
    User user =  new  User ();
    user.setId(1);
    user . setName( " Zhang San " );
    
    JAXB.marshal(user, System.out);
    
}

Applying the above test code will output the following XML content in the console. Is it very simple to convert Java objects to XML? If we are programming based on DOM or dom4j, we need to call a lot of APIs step by step. In comparison, this way of mapping based on annotations is much simpler.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
    <id>1</id>
    <name>张三</name>
</user>

If our root node is still user, but the id attribute of the User class does not want to be used as a byte point under the user node, but as an attribute of the user node, this is also very simple, directly in the getId() method Add the @XmlAttribute annotation to it.

@XmlRootElement(name="user")
public static class User {
    
    private Integer id;
    private String name;
    @XmlAttribute
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

At this time, run the above test code again, and the generated XML will be as follows.

@XmlRootElement(name="user")
public static class User {
    
    private Integer id;
    private String name;
    @XmlAttribute
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

If the id attribute of the User class wants to be an attribute of the user node, but the attribute name does not want to use the default id, you can specify a name we want through the name attribute of @XmlAttribute, for example, id1 is specified below.

@XmlRootElement(name="user")
public static class User {
    
    private Integer id;
    private String name;
    @XmlAttribute(name="id1")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

The XML generated at this time will be as follows.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user id1="1">
    <name>张三</name>
</user>

Similarly, if the node name mapped by the name attribute does not wish to be name, but rather name1, we can specify it through @XmlElement(name="name1").

@XmlRootElement(name="user")
public static class User {
    
    private Integer id;
    private String name;
    @XmlAttribute(name="id1")
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    @XmlElement(name="name1")
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

The XML generated at this time will be as follows.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user id1="1">
    < name1 >Zhang San</ name1 >
</user>

The above are examples of converting Java objects to XML after defining the mapping relationship between Java classes and XML. Next, let's look at the following example of converting XML to Java objects.

@Test
public void testXml() throws Exception {
    String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n" + 
            "<user id1=\"1\">\r\n" + 
            "    <name1>张三</name1>\r\n" + 
            "</user>";
    User user = JAXB.unmarshal(new StringReader(xml), User.class);
    Assert.assertTrue(user.getId() == 1);
    Assert.assertEquals("张三", user.getName());
}

Is it very simple? The above is just an introduction to some of the most basic operations of JAXB. If you want to know more, you can refer to the author's detailed explanation of JAXB development , which will allow you to gain more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264238&siteId=291194637