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代码, ...
随机推荐
- MySQL 中 on与where筛选条件的区别
在两张表连接的时候才会有on的筛选条件,那么on和where的区别是什么呢? 在inner join中是没有区别的,但是在左连接和右连接中,区别就体现出来了,下面以左连接为例: 1.用on的时候,只对 ...
- QT入门指导
罗列一些QT学习资料 1. http://www.qter.org/ 包含很多教程,包括著名的<学习之路>系列. 2. http://www.qtcn.org/bbs/index-htm- ...
- php使用curl post josn数据
今天在工作中使用到要使用("Content-Type", "application/json;charset=UTF-8")格式传送和接受数据,再次做个记录 p ...
- Qt编程基础入门之二
QMainWindow 菜单栏 菜单栏 最多有一个 //菜单栏创建,一个 QMenuBar *menu = new QMenuBar(this); // this->setMenuBar(men ...
- jquery注册页面的判断及代码的优化
今天主要负责完成注册页面的jquery代码的写入与优化,基本代码和登录页面差不多,复制修改一下代码就行了,主要区别在于多了一个重复密码与密码是否一致的判断,刚开始写出来的代码导致每个框的后面都追加重复 ...
- HMM-前向后向算法
基本要素 状态 \(N\)个 状态序列 \(S = s_1,s_2,...\) 观测序列 \(O=O_1,O_2,...\) \(\lambda(A,B,\pi)\) 状态转移概率 \(A = \{a ...
- [zoj 3416/hdu 3709]数位DP
题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何 ...
- python100例 21-30
021 猴子吃桃 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一半零一个.到第10天早 ...
- RunLoop总结
参考学习了YY大神的http://blog.ibireme.com/2015/05/18/runloop/#mode RunLoop: 看做是一个对象,这个对象管理了其需要处理的事件和消息,并提供了一 ...
- python之Python内置函数一览表
Python 解释器自带的函数叫做内置函数,这些函数可以直接使用,不需要导入某个模块. 如果你熟悉 Shell 编程,了解什么是 Shell 内置命令,那么你也很容易理解什么是 Python 内置函数 ...