Java和NodeJS解析XML对比
Java解析XML
1、接收xml文件或者字符串,转为InputStream
2、使用DocumentBuilderFactory对象将InputStream转为document对象
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//创建DocumentBuilder对象
DocumentBuilder builder = factory.newDocumentBuilder();
Document d = builder.parse(inputStream);
//NodeList sList = d.getChildNodes();
NodeList sList = d.getElementsByTagName("Service_Header");
3、使用工具类遍历解析xml文档
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DomXmlParse {
// 用Element方式
public static void element(NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Element element = (Element) list.item(i);
NodeList childNodes = element.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
// 获取节点
System.out.print(childNodes.item(j).getNodeName() + ":");
// 获取节点值
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
// 用node方式
public static void node(NodeList list) {
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
System.out.print(childNodes.item(j).getNodeName() + ":");
System.out.println(childNodes.item(j).getFirstChild().getNodeValue());
}
}
}
}
}
NodeJS方式
方式一:xml2js
1、cmd控制台安装xml2js模块
npm install xml2js
2、实例代码,复制以下代码,保存为xmlparse.js
var parseString = require('xml2js').parseString;
var xml_string = ''
+'<?xml version="1.0" encoding="UTF-8" ?>'
+'<business>'
+ '<company>Code Blog</company>'
+ '<owner>Nic Raboy</owner>'
+ '<employee>'
+ '<firstname>Nic</firstname>'
+ '<lastname>Raboy</lastname>'
+ '</employee>'
+ '<employee>'
+ '<firstname>Maria</firstname>'
+ '<lastname>Campos</lastname>'
+ '</employee>'
+'</business>';
parseString(xml_string, function (err, result) {
var json = JSON.stringify(result);
console.log(json);
});
3、控制台执行
node xmlparse.js
4、输出
{"business":{"company":["Code Blog"],"owner":["Nic Raboy"],"employee":[{"firstname":["Nic"],"lastname":["Raboy"]},{"firstname":["Maria"],"lastname":["Campos"]}]}}
方式二:xmlreader
1、cmd控制台安装xmlreader模块
npm install xmlreader
2、实例代码,复制以下代码,保存为xmlparse.js
var xmlreader = require("xmlreader");
var fs = require("fs");
var xml_string = '<response id="1" shop="aldi">'
+ 'This is some other content'
+ '<who name="james">James May</who>'
+ '<who name="sam">'
+ 'Sam Decrock'
+ '<location>Belgium</location>'
+ '</who>'
+ '<who name="jack">Jack Johnsen</who>'
+ '<games age="6">'
+ '<game>Some great game</game>'
+ '<game>Some other great game</game>'
+ '</games>'
+ '<note>These are some notes</note>'
+ '</response>';
xmlreader.read(xml_string, function(errors, response){
if(null !== errors ){
console.log(errors)
return;
}
console.log( response.response );
});
3、控制台执行
node xmlparse.js
4、输出
{
attributes: [Function: attributes],
parent: [Function],
count: [Function],
at: [Function],
each: [Function],
text: [Function],
who: {
array: [ [Object], [Object], [Object] ],
count: [Function],
at: [Function],
each: [Function]
},
games: {
attributes: [Function: attributes],
parent: [Function],
count: [Function],
at: [Function],
each: [Function],
game: {
array: [Array],
count: [Function],
at: [Function],
each: [Function]
}
},
note: {
attributes: [Function: attributes],
parent: [Function],
count: [Function],
at: [Function],
each: [Function],
text: [Function]
}
}
总结
Java解析xml要自己遍历dom树,如果自己封装方法或工具类,会更加灵活;
NodeJS解析xml只要引入相应的包,解析更加便捷。
Java和NodeJS解析XML对比的更多相关文章
- Java用SAX解析XML
要解析的XML文件:myClass.xml <?xml version="1.0" encoding="utf-8"?> <class> ...
- JAVA通过XPath解析XML性能比较(原创)
(转载请标明原文地址) 最近在做一个小项目,使用到XML文件解析技术,通过对该技术的了解和使用,总结了以下内容. 1 XML文件解析的4种方法 通常解析XML文件有四种经典的方法.基本的解析方式有两种 ...
- JAVA通过XPath解析XML性能比较
转自[http://www.cnblogs.com/mouse-coder/p/3451243.html] 最近在做一个小项目,使用到XML文件解析技术,通过对该技术的了解和使用,总结了以下内容. 1 ...
- JAVA使用SAX解析XML文件
在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...
- java使用sax解析xml
目的:解析xml文件,并存入mysql,并且要解析的字段能一一对应.这里解析的是微博的文件,想要利用里面的article和person_id字段. 思路: 为了能得到person_id和article ...
- java实战之解析xml
在java中解析xml有现成的包提供方法,常用的有四类:Dom,JDom,Sax以及Dom4j.其中前者是java中自带的,后三者需要大家从开源诸如sourceforge这样的网站下载jar包,然后在 ...
- Java 创建过滤器 解析xml文件
今天写了一个过滤器demo,现在是解析actions.xml文件,得到action中的业务规则:不需要导入任何jar包 ActionFilter过滤器类: package accp.com.xh.ut ...
- 【java】:解析xml
==========================================xml文件<?xml version="1.0" encoding="GB231 ...
- java使用dom4j解析xml文件
关于xml的知识,及作用什么的就不说了,直接解释如何使用dom4j解析.假如有如下xml: dom4j解析xml其实很简单,只要你有点java基础,知道xml文件.结合下面的xml文件和java代码, ...
随机推荐
- Mybatis时间范围查询,亲测有效
Md2All export document .output_wrapper pre code{font-family: Consolas, Inconsolata, Courier, monospa ...
- 【Python】Django2.0集成Celery4.1详解
环境准备 Python3.6 pip install Django==2.0.1 pip install celery==4.1.0 pip install eventlet (加入协程支持) 安装e ...
- 【UEFI】---史上最全的X86平台启动流程分析(软硬结合)
最近研究了下X86处理器的启动流程分析,相比于常见的ARM来说,X86平台启动流程真的复杂了很多,本次基于项目实际的两个问题入手,研究了包括以下几个部分的内容: 1. 从硬件角度看启动流程 2. 从软 ...
- Hadoop-wordCount实例代码编写(Hadoop学习第四天)
1.新建一个maven项目2.pom文件中引入以下jar包<dependency> <groupId>org.apache.hadoop</groupId> < ...
- js上传文件过大导致上传失败原因以及解决办法
背景:项目需要用到上传视频功能,由于视频有知识产权,要求必须上传到自己的服务器上不允许用第三方视频网站接口上传,于是一开始开始用的是input type=file去上传,小的视频上传没有问题,上传将近 ...
- 【漫画】JAVA并发编程 J.U.C Lock包之ReentrantLock互斥锁
在如何解决原子性问题的最后,我们卖了个关子,互斥锁不仅仅只有synchronized关键字,还可以用什么来实现呢? J.U.C包中还提供了一个叫做Locks的包,我好歹英语过了四级,听名字我就能马上大 ...
- Quartus II 与modelsim连接不上的问题
在Quartus II 中tools>options>General>EDA Tool Options 设置modelsim 路径 说明:不管是Quartus II 与modelsi ...
- [hdu5215]无向图找奇偶环
题意:如标题 思路:对于奇环,一个二分图判定就ok了,有奇环<=>非二分图.对于偶环,考虑环必定出现在双联通分量里面,可以先求出图的双联通分量,对于一个双联通分量,对于双联通分量里面的每个 ...
- JS中各种变量类型在条件判断为false的情况
var a = undefined; ->false var a = 0; ->false var a = 0.0; ->false var a = NaN; ->false ...
- vue.use()方法从源码到使用
在做 vue 开发的时候大家一定经常接触 Vue.use() 方法,官网给出的解释是: 通过全局方法 Vue.use() 使用插件:我觉得把使用理解成注册更合适一些,首先看下面常见的注册场景. 1 2 ...