将json对象转为xml进行操作属性

news2024/9/9 5:44:58

将json对象转为xml进行操作属性

文章目录

    • 将json对象转为xml进行操作属性
      • 前端发送json数据格式
      • 写入数据库格式-content字段存储(varchar(2000))
      • Question实体类-接口映射对象
      • QuestionContent 接收参数对象
      • DAO持久层
      • Mapper层
      • Service层
      • Controller控制层接收
      • xml与对象互转处理
      • pom.xml引入
      • 查询功能-xml以json返回页面

前端发送json数据格式

{
    "questionContent": {
        "title": "3. Fran正在构建一个取证分析工作站,并正在选择一个取证磁盘控制器将其包含在设置中。 以下哪些是取证磁盘控制器的功能? (选择所有适用的选项。)",
        "choiceImgList": {},
        "choiceList": {
            "A": "A. 防止修改存储设备上的数据",
            "B": "B. 将数据返回给请求设备",
            "C": "C. 将设备报告的错误发送给取证主机",
            "D": "D. 阻止发送到设备的读取命令"
        }
    }
}

写入数据库格式-content字段存储(varchar(2000))

在这里插入图片描述

content字段落入库中的格式

<QuestionContent>
  <title>Lisa正在试图防止她的网络成为IP欺骗攻击的目标,并防止她的网络成为这些攻击的源头。 以下哪些规则是Lisa应在其网络边界配置的最佳实践?(选择所有适用的选项)
</title>
  <titleImg></titleImg>
  <choiceList>
    <entry>
      <string>A</string>
      <string>阻止具有内部源地址的数据包进入网络</string>
    </entry>
    <entry>
      <string>B</string>
      <string>阻止具有外部源地址的数据包离开网络</string>
    </entry>
    <entry>
      <string>C</string>
      <string>阻止具有公共IP地址的数据包进入网络</string>
    </entry>
    <entry>
      <string>D</string>
      <string> 阻止带有私有IP地址的数据包离开网络</string>
    </entry>
  </choiceList>
  <choiceImgList/>
</QuestionContent>

Question实体类-接口映射对象

@XmlRootElement指定一个类为 XML 根元素。JAXB 是一种允许 Java 开发者将 Java 对象映射为 XML 表示形式,以及从 XML 还原为 Java 对象的技术。
Question 类被注解为 XML 根元素。当你使用 JAXB 序列化一个 Question 对象时,它将生成一个以 <Question> 为根元素的 XML 文档

import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Question implements Serializable {

	private String content;
	private QuestionContent questionContent;
	
}

QuestionContent 接收参数对象

使用@XStreamAlias("QuestionContent")为类指定别名。例如,为QuestionContent类指定别名,当使用XStream序列化对象时,<QuestionContent类指定别名>将作为根元素,而<title>将作为name字段的元素名。


import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("QuestionContent")
public class QuestionContent {

	@XStreamAlias("title")
	private String title;
	@XStreamAlias("titleImg")
	private String titleImg = "";
	@XStreamAlias("choiceList")
	private LinkedHashMap<String, String> choiceList;
	@XStreamAlias("choiceImgList")
	private LinkedHashMap<String, String> choiceImgList;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitleImg() {
		return titleImg;
	}

	public void setTitleImg(String titleImg) {
		this.titleImg = titleImg;
	}

	public LinkedHashMap<String, String> getChoiceList() {
		return choiceList;
	}

	public void setChoiceList(LinkedHashMap<String, String> choiceList) {
		this.choiceList = choiceList;
	}

	public LinkedHashMap<String, String> getChoiceImgList() {
		return choiceImgList;
	}

	public void setChoiceImgList(LinkedHashMap<String, String> choiceImgList) {
		this.choiceImgList = choiceImgList;
	}

}

DAO持久层

public interface QuestionMapper {

	public void insertQuestion(Question question) throws Exception;
	
	public void addQuestionKnowledgePoint(@Param("questionId") int questionId,
			@Param("pointId") int pointId) throws Exception;
}	

Mapper层

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.extr.persistence.QuestionMapper">

<insert id="addQuestionKnowledgePoint">
		insert into et_question_2_point
		(question_id,point_id)
		values
		(#{questionId},#{pointId})
	</insert>

	<insert id="insertQuestion" parameterType="com.extr.domain.question.Question"
		useGeneratedKeys="true" keyProperty="id">
		insert into et_question
		(name,content,question_type_id,create_time,creator,
		answer,analysis,reference,examing_point,keygetQuestionListword,points)
		values
		(#{name},#{content},#{question_type_id},#{create_time},#{creator},
		#{answer},#{analysis},#{referenceName},#{examingPoint},#{keyword},#{points})
	</insert>
</mapper>	

Service层

	
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {	

    @Autowired
	private QuestionMapper questionMapper;
    
	@Override
	@Transactional
	public void addQuestion(Question question) {
		// TODO Auto-generated method stub
		try {
			questionMapper.insertQuestion(question);
			for (Integer i : question.getPointList()) {
				questionMapper.addQuestionKnowledgePoint(question.getId(), i);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}
}
		

Controller控制层接收

import com.extr.util.xml.Object2Xml;	
@Controller
public class QuestionController {

	@RequestMapping(value = "/admin/questionAdd", method = RequestMethod.POST)
	public @ResponseBody Message addQuestion(@RequestBody Question question) {
	
		question.setContent(Object2Xml.toXml(question.getQuestionContent()));
		Message message = new Message();
		try {
			questionService.addQuestion(question);
		} catch (Exception e) {
			message.setResult("error");
			log.Info(e.getClass().getName());
			log.info(e);
		}
		return message;
	}
}

xml与对象互转处理

package com.extr.util.xml;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Object2Xml {
	public static String toXml(Object obj){
		XStream xstream=new XStream();
		xstream.processAnnotations(obj.getClass());
		
		return xstream.toXML(obj);
	}
	
	public static <T> T toBean(String xmlStr,Class<T> cls){
		XStream xstream=new XStream(new DomDriver());
		xstream.processAnnotations(cls);
		@SuppressWarnings("unchecked")
		T obj=(T)xstream.fromXML(xmlStr);
		return obj;
	}

pom.xml引入

用于将Java对象序列化为XML,以及从XML反序列化为Java对象。它提供了一种直观的方式来处理Java对象和XML之间的转换,而无需编写大量的映射代码或配置。

		<!-- Xstream -->
		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>1.4.2</version>
		</dependency>

查询功能-xml以json返回页面

public class QuestionAdapter {
    
	private QuestionContent questionContent;
    
    @GetMapping("/some-endpoint")  
    public @ResponseBody String getQuestionContentAsJson((Question question)) {  
         
        this.questionContent = Object2Xml.toBean(question.getContent(),
                        QuestionContent.class);
        question.setQuestionContent(this.questionContent);
        try {  
            return objectMapper.writeValueAsString(this.questionContent);  
        } catch (Exception e) {  
            // 处理异常并返回适当的错误响应  
        }  
        // 如果没有错误,但您仍然想返回一个默认的或空的JSON,您可以这样做:  
        return question; // 或任何其他您想要的默认JSON  
    }
}    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1878019.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

谈一下MySQL的两阶段提交机制

文章目录 为什么需要两阶段提交&#xff1f;两阶段提交流程&#xff1f;两阶段提交缺点&#xff1f; 为什么需要两阶段提交&#xff1f; 为了保证事务的持久性和一致性&#xff0c;MySQL需要确保redo log和binlog的同步持久化。MySQL通过“两阶段提交”的机制来实现在事务提交…

MyBatis第一节

目录 1. 简介2. 配置3. doing3.1 创建一个表3.2 打开IDEA&#xff0c;创建一个maven项目3.3 导入依赖的jar包3.4 创建entity3.5 编写mapper映射文件(编写SQL)3.6 编写主配置文件3.7 编写接口3.8 测试 参考链接 1. 简介 它是一款半自动的ORM持久层框架&#xff0c;具有较高的SQ…

【Kubernetes】搭建工具Kubeadm环境配置

架构&#xff1a;服务器采用Master-nodes&#xff08;3台&#xff09; Worker-nodes(2台) 一&#xff0c;服务准备工作 &#xff08;1&#xff09;在所有&#xff08;5台&#xff09;机器配置 主机名绑定&#xff0c;如下&#xff1a; cat /etc/hosts192.168.0.100 k8s-m…

【智能算法】决策树算法

目录 一、基本概念 二、工作原理 三、决策树算法优点和缺点 3.1 决策树算法优点 3.2 决策树算法缺点 四、常见的决策树算法及matlab代码实现 4.1 ID3 4.1.1 定义 4.1.2 matlab代码实现 4.2 C4.5 4.2.1 定义 4.2.2 matlab代码实现 4.3 CART 4.3.1 定义 4.3.2 mat…

leetcode-20-回溯-切割、子集

一、[131]分割回文串 给定一个字符串 s&#xff0c;将 s 分割成一些子串&#xff0c;使每个子串都是回文串。 返回 s 所有可能的分割方案。 示例: 输入: "aab" 输出: [ ["aa","b"], ["a","a","b"] ] 分析&…

springboot是否可以代替spring

Spring Boot不能直接代替Spring&#xff0c;但它是Spring框架的一个扩展和增强&#xff0c;提供了更加便捷和高效的开发体验。以下是关于Spring Boot和Spring关系的详细解释&#xff1a; Spring框架&#xff1a; Spring是一个广泛应用的开源Java框架&#xff0c;提供了一系列模…

Nosql期末复习

mongodb基本常用命令&#xff08;只要掌握所有实验内容就没问题&#xff09; 上机必考&#xff0c;笔试试卷可能考&#xff1a; 1.1 数据库的操作 1.1.1 选择和创建数据库 &#xff08;1&#xff09;use dbname 如果数据库不存在则自动创建&#xff0c;例如&#xff0c;以下…

ElementUI的基本搭建

目录 1&#xff0c;首先在控制终端中输入下面代码&#xff1a;npm i element-ui -S 安装element UI 2&#xff0c;构架登录页面&#xff0c;login.vue​编辑 3&#xff0c;在官网获取对应所需的代码直接复制粘贴到对应位置 4&#xff0c;在继续完善&#xff0c;从官网添加…

【工具分享】Nuclei

文章目录 NucleiLinux安装方式Kali安装Windows安装 Nuclei Nuclei 是一款注重于可配置性、可扩展性和易用性的基于模板的快速漏洞验证工具。它使用 Go 语言开发&#xff0c;具有强大的可配置性、可扩展性&#xff0c;并且易于使用。Nuclei 的核心是利用模板&#xff08;表示为简…

oracle 11g rac安装grid 执行root脚本add vip -n 。。。on node= ... failedFailed 错误处理

问题&#xff1a; CRS-4402: The CSS daemon was started in exclusive mode but found an active CSS daemon on node racdg1-1, number 1, and is terminating An active cluster was found during exclusive startup, restarting to join the cluster PRCN-2050 : The requ…

[OtterCTF 2018]Name Game

Name Game 题目描述&#xff1a;我们知道这个帐号登录到了一个名为Lunar-3的频道。账户名是什么&#xff1f;猜想&#xff1a;既然登陆了游戏&#xff0c;我们尝试直接搜索镜像中的字符串 Lunar-3 。 直接搜索 Lunar-3 先把字符串 重定向到 txt文件里面去然后里面查找 Lunar-3…

阐述Python:except的用法和作用?

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:「stormsha的主页」…

C++特殊类设计单例模式...

文章目录 请设计一个类&#xff0c;不能被拷贝请设计一个类&#xff0c;只能在堆上创建对象请设计一个类&#xff0c;只能在栈上创建对象请设计一个类&#xff0c;不能被继承请设计一个类&#xff0c;只能创建一个对象(单例模式)单例模式&#xff1a;饿汉模式&#xff1a;懒汉模…

在vs上远程连接Linux写服务器项目并启动后,可以看到服务启动了,但是通过浏览器访问该服务提示找不到页面

应该是被防火墙挡住了&#xff0c;查看这个如何检查linux服务器被防火墙挡住 • Worktile社区 和这个关于Linux下Nginx服务启动&#xff0c;通过浏览器无法访问的问题_linux无法访问nginx-CSDN博客 的提示之后&#xff0c;知道防火墙开了&#xff0c;想着可能是我写的服务器的…

SpringDataJPA系列(1)JPA概述

SpringDataJPA系列(1)JPA概述 SpringDataJPA似乎越来越流行了&#xff0c;我厂的mysql数据库和MongoDB数据库持久层都依赖了SpringDataJPA。为了更好的使用它&#xff0c;我们内部还对MongoDB的做了进一步的抽象和封装。为了查漏补缺&#xff0c;温故而知新&#xff0c;整理下…

企业本地大模型用Ollama+Open WebUI+Stable Diffusion可视化问答及画图

最近在尝试搭建公司内部用户的大模型,可视化回答,并让它能画图出来, 主要包括四块: Ollama 管理和下载各个模型的工具Open WebUI 友好的对话界面Stable Diffusion 绘图工具Docker 部署在容器里,提高效率以上运行环境Win10, Ollama,SD直接装在windows10下, 然后安装Docker…

c++ 设计模式 的课本范例(中)

&#xff08;10&#xff09;单例模式 singleton 。整个应用程序执行时&#xff0c;只有一个单例模式的对象。 class GameConfig // 懒汉式&#xff0c;啥时候用单例对象&#xff0c;啥时候创建。 { private:static GameConfig* ptrGameConfig; // 这些函数都作为私有函数&…

二叉树的层序遍历/后序遍历(leetcode104二叉树的最大深度、111二叉树的最小深度)(华为OD悄悄话、数组二叉树)

104二叉树的最大深度 给定一个二叉树 root &#xff0c;返回其最大深度。 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。 本题可以使用前序&#xff08;中左右&#xff09;&#xff0c;也可以使用后序遍历&#xff08;左右中&#xff09;&#xff0c;…

自闭症早期风险判别和干预新路径

谷禾健康 自闭症谱系障碍 (ASD) 是一组神经发育疾病&#xff0c;其特征是社交互动和沟通的质量障碍、兴趣受限以及重复和刻板行为。 环境因素在自闭症中发挥重要作用&#xff0c;多项研究以及谷禾队列研究文章表明肠道微生物对于自闭症的发生和发展以及存在明显的菌群和代谢物的…

JVM专题十一:JVM 中的收集器一

上一篇JVM专题十&#xff1a;JVM中的垃圾回收机制专题中&#xff0c;我们主要介绍了Java的垃圾机制&#xff0c;包括垃圾回收基本概念&#xff0c;重点介绍了垃圾回收机制中自动内存管理与垃圾收集算法。如果说收集算法是内存回收的方法论&#xff0c;那么垃圾收集器就是内存回…