Java对象转xml(Object to Xml)

devtools/2024/9/23 11:16:07/
xmlns="http://www.w3.org/2000/svg" style="display: none;">

目录

  • 第一章、Java对象转xml
    • 1.1)Java实体类
    • 1.2)Xml中需要包含的字段
    • 1.3)设置根标签和属性序列化方式
    • 1.4)使用JAXBContext和Marshaller进行转换

友情提醒:

先看目录,了解文章结构,点击目录可跳转到文章指定位置。

xml_6">第一章、Java对象转xml

1.1)Java实体类

这是需要转xml的实体类

java">import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;@Data
@TableName(value="productionissue")
public class ProductionIssue {@TableId(value = "id", type = IdType.AUTO)private Integer  id;@TableField("userid")private String userId;@TableField("title")private String title;@TableField("content")private String content;@TableField("orderNumber")private String orderNumber;@TableField("file")private byte[] file;@TableField("date")private String date;@TableField("sta")private String sta;
}

1.2)Xml中需要包含的字段

因为有些XML字段不在数据库对应的实体类中,所以需要额外建一个XML字段对应的实体类

java">import com.baomidou.mybatisplus.annotation.TableField;
import lombok.Data;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;@Data
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class NewInc {@XmlElement(name = "WSUSER")private String wsUser;@XmlElement(name = "WSPASSWORD")private String wsPassword;@XmlElement(name = "CREATOR")private String userId;@XmlElement(name = "CREATORCONTACT")private String creatorContact;@XmlElement(name = "SOURCE")private String source;@XmlElement(name = "SUMMARY")private String title;@XmlElement(name = "DESCRIPTION")private String content;@XmlElement(name = "PRIORITY")private String priority;@XmlElement(name = "URGENCY")private String urgency;@XmlElement(name = "REFLECTCANAL")private String reflectCanal;@XmlElement(name = "HAPPENTIME")private String date;@XmlElement(name = "ISEFFECTBUSINESS")private String isEffectBusiness;@XmlElement(name = "ISEFFECTBUSINESS")private String effectSituation;@XmlElement(name = "ISACCOUNT")private String isAccount;@XmlElement(name = "ISREGULATOR")private String isRegulator;@XmlElement(name = "ISBIGCUSTOMER")private String isBigCustomer;}

1.3)设置根标签和属性序列化方式

@XmlRootElement和@XmlAccessorType是JAXB中用于控制Java对象与XML之间映射关系的注解。@XmlRootElement用于指定生成的XML的根标签名,而@XmlAccessorType用于指定属性的序列化方式。

java">
import lombok.Data;
import javax.xml.bind.annotation.*;
import java.util.List;@Data
@XmlRootElement(name="HAPPY")   //根标签名
@XmlAccessorType(XmlAccessType.FIELD)  //属性序列化
public class NewIncXml {@XmlElement(name = "IN")private List<NewInc> newIncs;public List<NewInc> getNewIncs() {return newIncs;}public void setNewIncs(List<NewInc> newIncs) {this.newIncs = newIncs;}
}

1.4)使用JAXBContext和Marshaller进行转换

将从数据库中获取的NewInc对象转换为XML格式,并输出每个NewInc对象的XML表示。方便地将Java对象转换为XML数据进行处理或传输。

具体逻辑:
遍历List中的每个NewInc对象:
创建一个新的List对象newIncsOne,并将当前NewInc对象添加到这个List中。
创建一个NewIncXml对象xml,并设置其List newIncs属性为newIncsOne。
创建一个StringWriter对象writer用于将XML数据写入。
使用JAXBContext和Marshaller将NewIncXml对象转换为XML格式,并将结果写入到StringWriter中。
打印输出转换后的XML数据

java">package com.icbc.coresd.service.impl;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icbc.coresd.entity.NewInc;
import com.icbc.coresd.entity.NewIncXml;
import com.icbc.coresd.entity.ProductionIssue;
import com.icbc.coresd.mapper.ProductionIssueMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;@Service
public class ProductionIssueServiceImpl {@AutowiredProductionIssueMapper productionIssueMapper;/*查询数据库productionissue表数据,设置到NewInc*/public List<NewInc> selectAll() {LambdaQueryWrapper<ProductionIssue> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(ProductionIssue::getSta,0 ).select(ProductionIssue::getTitle,ProductionIssue::getDate,ProductionIssue::getContent);List<ProductionIssue> productionIssuesList = productionIssueMapper.selectList(queryWrapper);List<NewInc> newIncList = new ArrayList<>();for (ProductionIssue pi : productionIssuesList) {NewInc newInc = new NewInc();// 将User的属性映射到Product的属性newInc.setUserId(pi.getUserId());newInc.setTitle(pi.getTitle());newInc.setContent(pi.getContent());newInc.setDate(pi.getDate());newInc.setWsUser("name");newInc.setWsPassword("pass123");newInc.setCreatorContact("user@mail");newInc.setSource("固定");newInc.setPriority("6");newInc.setUrgency("02");newInc.setReflectCanal("14");newInc.setIsEffectBusiness("是");newInc.setEffectSituation("file");newInc.setIsAccount("否");newInc.setIsRegulator("否");newInc.setIsBigCustomer("否");newIncList.add(newInc);}return newIncList;}public void build() {List<NewInc> newIncsList = selectAll();System.out.println(newIncsList.size());for (int i = 0; i < newIncsList.size(); i++) {NewInc newInc = newIncsList.get(i);// 对取出的数据进行操作List<NewInc> newIncsOne = new ArrayList<>();newIncsOne.add(newInc);NewIncXml xml = new NewIncXml();xml.setNewIncs(newIncsOne);StringWriter writer = null;try {JAXBContext context = JAXBContext.newInstance(NewIncXml.class);Marshaller marshaller = context.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);writer = new StringWriter();writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");marshaller.marshal(xml, writer);} catch (JAXBException e) {e.printStackTrace();}System.out.println(writer.toString());}}
}

http://www.ppmy.cn/devtools/8168.html

相关文章

一个 .net 8 + Azure 登录 + Ant Design Blazor 的基本后台框架

一个 .net 8 Azure 登录 Ant Design Blazor 的基本后台框架 主界面使用了 Ant Design Blazor 项目模板搭建 后台技术是 .net 8 Blazor run at server 模式 登录方式使用 Azure 实现了菜单导航和路由 此外实现了读取和修改本地Json文件的功能&#xff0c;不是必须的&#x…

【C语言】指针篇-深入探索数组名和指针数组(2/5)- 必读指南

&#x1f308;个人主页&#xff1a;是店小二呀 &#x1f308;C语言笔记专栏&#xff1a;C语言笔记 &#x1f308;C笔记专栏&#xff1a; C笔记 &#x1f308;喜欢的诗句:无人扶我青云志 我自踏雪至山巅 文章目录 认识指针与数组之间的关系(涉及二级指针)**数组名****指针访问…

前端入门:HTML(CSS边框综合案例)

案例&#xff1a; 源代码&#xff1a; css-borders.html: <body> <div id"square"> </div> <br> <div id"triangle"> </div> <br> <div id"trapezium"> </div> <br> <div id…

用户权限—— u+s\g+s\o+t三个特殊权限说明

linux中一个文件有三个权限&#xff0c;分别时用户权限&#xff0c;群组权限以及其他权限&#xff0c;是用wrxwrxwrx表示的&#xff0c;w代表可写&#xff0c;r代表可读&#xff0c;x代表可执行。但在一些特殊情况中还会出现s,或者t。 这种情况主要分为三种&#xff1a; rwsrw…

C#队列(Queue)简单使用方法

队列作为一种基础且实用的数据结构&#xff0c;遵循“先进先出”&#xff08;First-In, First-Out, FIFO&#xff09;原则&#xff0c;广泛应用于各种编程场景。 1.创建Queue 在C#中&#xff0c;创建一个队列首先要引用System.Collections.Generic命名空间&#xff0c;并声明…

大话数据结构学习笔记-绪论

概念 数据:是描述客观事物的符号&#xff0c;是计算机中可以操作的对象&#xff0c;是能被计算机识别&#xff0c;并输入给计算机处理的符号集合。 数据元素:是组成数据的、有一定意义的基本单位&#xff0c;在计算机中通常作为整体处理&#xff0c;也被称为记录。 数据项:一…

安信可 ESP_01SWIFI模块的使用 (电脑通过usb转tll模块连接wifi模块进行调试)

一&#xff1a;需要用到的模块 &#xff08;1&#xff09;安信可的ESP_01wifi模块 ESP-01是深圳安信可科技基于ESP8266芯片开发的串口wifi模块&#xff0c;模组集成了透传功能&#xff0c;即买即用&#xff0c;支持串口指令集&#xff0c;用户通过串口即可实现网络访问…

基于IIoT的设备预测性维护设计

基于IIoT的设备预测性维护设计 一、引言 在工业物联网&#xff08;IIoT&#xff09;的背景下&#xff0c;设备预测性维护成为了一种关键的战略&#xff0c;能够帮助企业提前发现并解决设备故障&#xff0c;从而提高生产效率、减少停机时间&#xff0c;并降低总体维护成本。为了…