java xml文件对比_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" ?>'

+''

+'Code Blog'

+'Nic Raboy'

+''

+'Nic'

+'Raboy'

+''

+''

+'Maria'

+'Campos'

+''

+'';

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 = ''

+ 'This is some other content'

+'James May'

+ ''

+'Sam Decrock'

+'Belgium'

+''

+ 'Jack Johnsen'

+''

+'Some great game'

+'Some other great game'

+''

+'These are some notes'

+'';

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只要引入相应的包,解析更加便捷。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值