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