JAVAX的使用(Xml和Java对象间的任意转换)

 

Maven的依赖包:

 

<dependency>
 <groupId>javax.xml.bind</groupId>
 <artifactId>jaxb-api</artifactId>
 <version>2.2.12</version>
 </dependency>
 

//要转换的类。用注解的方式

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement 
public class Customer { 
    String name; 
    int age; 
    int id; 
 
    @XmlElement 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
     
    @XmlElement 
    public int getAge() { 
        return age; 
    } 
    public void setAge(int age) { 
        this.age = age; 
    } 
     
    @XmlAttribute 
    public int getId() { 
        return id; 
    } 
    public void setId(int id) { 
        this.id = id; 
    } 
     
    @Override 
    public String toString() { 
        return "Customer [id=" + id + ",name=" + name + ",age=" + age + "]"; 
    } 

 

 

 

//Java对象转XML

 @Test
 public void testXml() throws Exception{
  
  Customer customer = new Customer(); 
        customer.setId(100); 
        customer.setName("suo"); 
        customer.setAge(29);
       
        File file = new File("D:\\file1.xml"); 
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); 
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
       
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
        jaxbMarshaller.marshal(customer, file);  //写入到XMl文件
        jaxbMarshaller.marshal(customer, System.out); //打印到控制台
  
  
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将XML数据转换Java对象,你可以使用Java的内置XML解析器,例如DOM或SAX,或者使用第三方库如Jackson或JAXB。 这里我提供一个使用JAXB的例子,步骤如下: 1. 创建XML文件,并定义对应的Java类。 例如,我们有以下XML文件: ```xml <?xml version="1.0" encoding="UTF-8"?> <book> <title>Java Programming</title> <author>John Smith</author> <isbn>1234567890</isbn> </book> ``` 我们可以定义一个名为Book的Java类,如下: ```java import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Book { private String title; private String author; private String isbn; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getIsbn() { return isbn; } public void setIsbn(String isbn) { this.isbn = isbn; } } ``` 注意,我们需要在Book类上添加@XmlRootElement注解,以指定XML根元素的名称。 2. 使用JAXB将XML文件转换Java对象。 ```java import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import java.io.File; public class Main { public static void main(String[] args) { try { File file = new File("book.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Book book = (Book) jaxbUnmarshaller.unmarshal(file); System.out.println(book.getTitle()); System.out.println(book.getAuthor()); System.out.println(book.getIsbn()); } catch (JAXBException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个File对象,用于指定XML文件的位置。然后,我们使用JAXBContext创建了一个Unmarshaller对象,并使用对象XML文件转换为Book对象。最后,我们可以访问Book对象的属性并将其打印到控制台上。 以上就是使用JAXB将XML数据转换Java对象的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值