xml字符串转json

[Json依赖] JSONObject的依赖包 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency> <dependency> &lt... 阅读详情
方法一:
import org.json.XML;
import org.json.JSONObject; 

jsonObject = XML.toJSONObject(result);


方法二:
工具类调用
JSONObject jsonObject1 = XmlTool.documentToJSONObject(result);

----------------------------------------------------------------------
com.alibaba.fastjson.JSONArray的接参数的工具类
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class XmlTool {
   /**
    * String 转 org.dom4j.Document
    * @param xml
    * @return
    * @throws DocumentException
    */
   public static Document strToDocument(String xml){
      try {
         //加上xml标签是为了获取最外层的标签,如果不需要可以去掉
//       return DocumentHelper.parseText("<xml>"+xml+"</xml>");
         return DocumentHelper.parseText(xml);
      } catch (DocumentException e) {
         return null;
      }
   }

   /**
    * org.dom4j.Document 转  com.alibaba.fastjson.JSONObject
    * @param xml
    * @return
    * @throws DocumentException
    */
   public static JSONObject documentToJSONObject(String xml){
      return elementToJSONObject(strToDocument(xml).getRootElement());
   }

   /**
    * org.dom4j.Element 转  com.alibaba.fastjson.JSONObject
    * @param node
    * @return
    */
   public static JSONObject elementToJSONObject(Element node) {
      JSONObject result = new JSONObject();
      // 当前节点的名称、文本内容和属性
      List<Attribute> listAttr = node.attributes();// 当前节点的所有属性的list
      for (Attribute attr : listAttr) {// 遍历当前节点的所有属性
         result.put(attr.getName(), attr.getValue());
      }
      // 递归遍历当前节点所有的子节点
      List<Element> listElement = node.elements();// 所有一级子节点的list
      if (!listElement.isEmpty()) {
         for (Element e : listElement) {// 遍历所有一级子节点
            if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
               result.put(e.getName(), e.getTextTrim());// 沒有则将当前节点作为上级节点的属性对待
            else {
               if (!result.containsKey(e.getName())) // 判断父节点是否存在该一级节点名称的属性
                  result.put(e.getName(), new JSONArray());// 没有则创建
               ((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));// 将该一级节点放入该节点名称的属性对应的值中
            }
         }
      }
      return result;
   }
}
Java下的json解析工具包:org.json.jar包 Java下的json构造和解析工具包:org.json.jar,轻量级,且它还包含JSON与XML, HTTP headers, Cookies, CDL的转换。 立即下载

相关推荐

org.json的依赖包

org.json的依赖包

com.alibaba和org.json两种方式xml转json

第一种方式:maven引入org.json包pom.xml&lt;dependency&gt; &lt;groupId&gt;org.json&lt;/groupId&gt; &lt;artifactId&gt;json&lt;/artifactId&gt; &lt;version&gt;20171018&lt;/version&gt; &lt;/dependency&gt;

涛哥是个大帅比 2万+

从临床试验到互联网AB测试:边缘结构模型(MSM)如何解决‘时依性混杂’这个老大难问题

本文探讨边缘结构模型(MSM)如何解决时依性混杂问题,适用于临床试验和互联网AB测试。通过逆概率加权(IPTW)等方法,MSM能有效剥离混杂效应,准确评估长期干预效果。文章结合跨领域案例,详细解析MSM的核心原理、实施步骤及在推荐算法效果评估中的实战应用,帮助读者掌握这一先进因果推断工具。

weixin_28725949的博客 206

org.json.XML 依赖哪个包

org.json.XML 是一个 Java 类,它所依赖的包是 json.org 的 JSON 包。你可以在这里下载它:https://mvnrepository.com/artifact/org.json/json。 要使用这个类,你需要在你的项目中添加 json 包的依赖,并在你的代码中导入 org.json.XML 类。例如: import org.json.XML; ...

weixin_35754676的博客 1379

Fluent UDF实战:除了速度入口,你的DEFINE_PROFILE宏还能搞定这些边界条件(温度、组分、壁面接触角全解析)

本文深入解析Fluent UDF中DEFINE_PROFILE宏在复杂边界条件中的高阶应用,涵盖温度场、组分浓度和壁面接触角等关键场景。通过实战代码示例,展示如何实现空间梯度温度场、动态组分控制和接触角动态调整,助力CFD工程师突破标准界面限制,提升仿真精度和效率。

weixin_28688385的博客 449

java json转xml字符串_json字符串和xml互转(亲测可用)

看到一个json字符串和xml互转工具类感觉挺好用的xml转json org.jdom用 dom 提取节点 转成mapjson 转 xml com.alibaba.fastjson.JSONObject遍历key代码如下import java.io.File;import java.io.IOException;import java.io.StringReader;imp...

weixin_29706351的博客 2691

AI工作流革命:从n8n到Sora2,揭秘自动化视频创作的底层逻辑

本文深入探讨了AI工作流在自动化视频创作中的应用,重点介绍了n8n、Coze和Sora2的组合如何实现从链接输入到视频输出的全流程自动化。通过三层架构解析、关键技术模块优化及成本控制策略,这套方案显著提升了视频生产效率,适用于电商、热点追踪等多种场景。

gg901的博客 1082

dom4j实现xml转map,xml转json字符串

dom4j实现xml转map,再使用 fastjson 将map转为json字符串。map转为json字符串方法很多,这里使用fastjson。xml转换为json后效果。

u014644574的博客 496

Starlink第三代终端和第二代终端的差异性有哪些?

starlink第三代终端和第二代终端的额差异

chen2jiang3的博客 2924

硬件工程师笔记——运算放大电路Multisim电路仿真实验汇总

本文系统介绍了运算放大电路的基础知识及其应用。主要内容包括:1)运算放大器的基本结构、理想特性及分析方法(虚短、虚断、叠加定理);2)比例运算电路(同向、反向、差分)的工作原理与仿真分析;3)求和运算、加减运算电路的实现方法;4)积分、微分、指数和对数运算电路的基本原理;5)过零电压比较器的功能特点。通过理论分析和仿真实验,详细阐述了各类运算放大电路的输入输出特性、相位关系和增益计算,为硬件工程师提供了实用的电路设计参考。

XU157303764的博客 2478

Java如何xml转json字符串_xml字符串转json字符串

XML字符串转JSON字符串网上的方法很多,这里主要推荐两种:1、使用org.json包jar地址:http://mvnrepository.com/artifact/org.json/json示例代码:import org.json.JSONException;import org.json.JSONObject;import org.json.XML;public class JsonUtil...

weixin_26642481的博客 1901

跨平台CMake AutoSAR

AutoSAR是汽车行业采用的一种软件架构标准,它旨在提供一个通用的软件平台,使得汽车软件的开发和集成变得更加高效和灵活。总结一下,CMake AutoSAR是一个强大的跨平台工具,为AutoSAR开发提供了便利。通过简单的配置文件和命令,开发人员可以实现高效、灵活的跨平台开发,并加快自动驾驶系统的开发进程。上述示例中,我们定义了一个名为AutoSAR的项目,指定了源代码文件的路径和依赖库的路径。通过简单的配置文件和命令,开发人员可以实现高效、灵活的跨平台开发,并加快自动驾驶系统的开发进程。

EcmFsharp的博客 232

C# json 转 xml 字符串

本文告诉大家如何将 json 转 xml 或将 xml 转 json 字符串

lindexi 5635

库存管理系统项目部署方案(基于jdk8)

不需要安装到服务器,运维本地安装就行,主要是用来编译前端代码的。构建完成后将dist文件夹复制到服务器内。

程序员诚哥 2574

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

java json转xml字符串_Java JSON 之 Xml 转 JSON 字符串

http://www.verejava.com/?id=16998704382152下载依赖 jar 包json.jardom4j-1.6.1.jarjaxen-1.1-beta-6.jarproduct.xml1电脑24000package com.json19;import java.io.File;import java.util.ArrayList;import java.util.Ite...

weixin_33329746的博客 186

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

dom4J xml字符串转json字符串

/** * 将xml格式的字符串转换成Map对象 * * @param xmlStr xml格式的字符串 * @return json字符串 * @throws Exception 异常 */ public static String xmlStrToJson(String xmlStr) throws Exception { Map<String, Object> map = new HashMap<.

送我一块糖的博客 705

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

JavaScript中字符串转JSON或者XML

JavaScript中字符串转JSON或者XMLJS中经常需要将数据格式从字符串类型转换为JSON或者XML,尤其是string到JSON转换,下面简单介绍下数据的转换方法:string转换为JSON使用eval函数实现:var json=eval('('+str+')');使用JSON对象的parse()方法实现,测试使用IE9,chrome,Firefox,Opera均能实现JSON格式的转换:

扭来不叫牛奶 740

xml字符串转json字符串 JSON字符串转XML字符串

package json.test; import net.sf.json.JSONObject; import net.sf.json.xml.XMLSerializer; /**  * 所需要的jar包为 JSONObject-lib  * commons-lang-2.1.jar  * commons-beanutils.jar  * ezmorph-1.0.2.jar  

tianlong1569的专栏 1639

Xml字符串转Json,并将Json的Key值转大写

最近开发任务中有一项是将获取到的xml请求报文转成Json字符串,并将json中全部的key转为大写存入表中,看着是很简单的一个需求,于是便开始了愉快的敲代码;

a1812233832的博客 2447

xml字符串转json字符串

XML字符串转JSON字符串网上的方法很多,这里主要推荐两种: 1、使用org.json包 jar地址:http://mvnrepository.com/artifact/org.json/json 示例代码: import org.json.JSONException; import org.json.JSONObject; import org.json.XML; public

敲键盘的猫 2123

Java xml字符串转json

Java xml字符串转json import net.sf.json.xml.XMLSerializer; import net.sf.json.JSONArray; import net.sf.json.JSONObject; { ... String xml;//假设为xml字符串 XMLSerializer xmlSerializer = new XM...

weixin_39793646的博客 3039

xml格式的String字符串转json字符串

1.引入pom依赖 <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20180130</version> </dependency> 2.需要转换的xml代码示例 ...

ourLang 2万+

c语言json转xml,Json对象转换为xml字符串

解析前的json数据,数据中嵌套json数组,字符串等:{"sfsyxxwlqy": "Y","sjlreLj": "-769.13","byjynssdeLj": "0","people": {"name": "laowang","sex": "nan","age": 12,"parents": [{"father": "laowang","mother": "laozhang"}]}}解析后生...

weixin_39852121的博客 335
上一篇: localDateTime 转date
下一篇: 身份证号码校验
Trank-Lw
博客等级 码龄9年 103粉丝 96原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值