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代码, ...
随机推荐
- win7乱码问题解决方法(cmd变小,plsql客户端乱码)
1.点击控制面板:时钟.语言和区域:区域和语言:管理点击更改系统区域设置,选中英语(英国):重启 2.点击控制面板:时钟.语言和区域:区域和语言:管理点击更改系统区域设置,选中中文(简体,中国):重启 ...
- turtle库应用实例2-六芒星的绘制
六芒星的绘制 ...
- Codeforces Round #632 (Div. 2)
Codeforces Round #632 (Div. 2) 这一场打的好差呀,这几次艰难上的分全部掉回去了,感觉就像一夜回到了解放前. 说实话,就是被B卡到了,没看到只能从小的放到大的... Lit ...
- JAVA基础篇 之 方法的重载
任何程序语言都具备了一项重要的特性就是对名字的运用.当创建一个对象时,也就给此对象分配到的存储空间取了一个名字.所谓方法则是给某个动作取的名字.通过使用名字你可以引用所有的对象和方法. 将人类 ...
- STM32 标准库3.5修改默认外部8M晶振为16M晶振
ST官方标准库V3.5默认的外部晶振频率为8M,实际使用中外部晶振需要修改为16M: 经过实验,修改有效,具体的patch如下: 修改 HSE_VALUE 值 diff --git "a/L ...
- Linux设备子系统初始化
本文介绍的内容是基于Linux3.1源码,并参考了很多网上找来的资料 Linux内核的启动的流程如下: start_kernel->rest_init->kernel_init->d ...
- 错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplic
错误 在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的.如果在 IIS 中没有将虚拟目录配置为应用程序,则可能导致此错误. 如果 ...
- 搭建私有镜像仓库registry 2.0
搭建 docker run -d -p 5000:5000 --restart=always --name registry2 registry:2 就可以将自己的镜像 push到这个私有的镜像仓库 ...
- input输入框直接拉起九宫格数字键盘
<input type="number" pattern="\d*">
- ArrayList详解-源码分析
ArrayList详解-源码分析 1. 概述 在平时的开发中,用到最多的集合应该就是ArrayList了,本篇文章将结合源代码来学习ArrayList. ArrayList是基于数组实现的集合列表 支 ...