博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java DOM解析XML
阅读量:5060 次
发布时间:2019-06-12

本文共 6973 字,大约阅读时间需要 23 分钟。

1、xml测试文件:data.xml

GA
int
1
100
GB
string
100
GC
enum
1-100,255
2、接收实体类Entity.java:
import java.util.ArrayList; import java.util.List; /**  * Created by Administrator on 2017/5/28.  */ public class Entity {
private String fieldName; private String type; private int min; private int max; private List
range = new ArrayList
(); private int length; public String getFieldName() {
return fieldName; } public void setFieldName(String fieldName) {
this.fieldName = fieldName; } public String getType() {
return type; } public void setType(String type) {
this.type = type; } public int getMin() {
return min; } public void setMin(int min) {
this.min = min; } public int getMax() {
return max; } public void setMax(int max) {
this.max = max; } public List
getRange() {
return range; } public void setRange(List
range) {
this.range = range; } public int getLength() {
return length; } public void setLength(int length) {
this.length = length; } @Override public String toString() {
return "Entity{" + "fieldName='" + fieldName + '\'' + ", type='" + type + '\'' + ", min=" + min + ", max=" + max + ", range=" + range + ", length=" + length + '}'; } } 3、测试程序
import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /**  * Created by Administrator on 2017/5/28.  */ public class DOMTest {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
File file = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(file); NodeList nodeList = document.getElementsByTagName("ENTITY"); Map
resultMap = new HashMap
(); for (int i = 0; i < nodeList.getLength(); i++) {
Element entityElement = (Element) nodeList.item(i); NodeList childNodes = entityElement.getChildNodes(); Entity entity = new Entity(); for (int j = 0; j < childNodes.getLength(); j++) {
if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
if ("FIELDNAME".equals(childNodes.item(j).getNodeName())) {
entity.setFieldName(childNodes.item(j).getFirstChild().getNodeValue()); } else if ("TYPE".equals(childNodes.item(j).getNodeName())) {
entity.setType(childNodes.item(j).getFirstChild().getNodeValue()); } else if ("MIN".equals(childNodes.item(j).getNodeName())) {
entity.setMin(Integer.valueOf(childNodes.item(j).getFirstChild().getNodeValue())); } else if ("MAX".equals(childNodes.item(j).getNodeName())) {
entity.setMax(Integer.valueOf(childNodes.item(j).getFirstChild().getNodeValue())); } else if ("LENGTH".equals(childNodes.item(j).getNodeName())) {
entity.setLength(Integer.valueOf(childNodes.item(j).getFirstChild().getNodeValue())); } else if ("RANGE".equals(childNodes.item(j).getNodeName())) {
String range = childNodes.item(j).getFirstChild().getNodeValue(); String[] commaStrArr = range.split(","); List
rangeList = new ArrayList
(); for (String str : commaStrArr) {
String[] hyphenStrArr = str.split("-"); if (hyphenStrArr.length == 2) {
int min = Integer.valueOf(hyphenStrArr[0]); int max = Integer.valueOf(hyphenStrArr[1]); for (int k = min; k <= max; k++) {
rangeList.add(k); } } else if (hyphenStrArr.length == 1) {
rangeList.add(Integer.valueOf(hyphenStrArr[0])); } } entity.setRange(rangeList); } } } System.out.println(entity); resultMap.put(entity.getFieldName(), entity); System.out.println("===================="); } System.out.println(resultMap); } } 4、测试结果:

Entity{fieldName='GA', type='int', min=1, max=100, range=[], length=0}

====================
Entity{fieldName='GB', type='string', min=0, max=0, range=[], length=100}
====================
Entity{fieldName='GC', type='enum', min=0, max=0, range=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 255], length=0}
====================
{GC=Entity{fieldName='GC', type='enum', min=0, max=0, range=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 255], length=0}, GA=Entity{fieldName='GA', type='int', min=1, max=100, range=[], length=0}, GB=Entity{fieldName='GB', type='string', min=0, max=0, range=[], length=100}}

 

转载于:https://www.cnblogs.com/liulongzhiyu/p/6916092.html

你可能感兴趣的文章
Linux 根文件系统制作
查看>>
IOS--沙盒机制
查看>>
使用 JointCode.Shuttle 访问任意 AppDomain 的服务
查看>>
sqlite的坑
查看>>
digitalocean --- How To Install Apache Tomcat 8 on Ubuntu 16.04
查看>>
linux swoole
查看>>
【题解】[P4178 Tree]
查看>>
Jquery ui widget开发
查看>>
更改git仓库地址
查看>>
有标号DAG计数 [容斥原理 子集反演 组合数学 fft]
查看>>
Recipe 1.4. Reversing a String by Words or Characters
查看>>
Rule 1: Make Fewer HTTP Requests(Chapter 1 of High performance Web Sites)
查看>>
sql注入
查看>>
「破解」Xposed强
查看>>
src与href的区别
查看>>
ABAP工作区,内表,标题行的定义和区别
查看>>
《xxx重大需求征集系统的》可用性和可修改性战术分析
查看>>
Python 中 创建类方法为什么要加self
查看>>
关于indexOf的使用
查看>>
【转】JS生成 UUID的四种方法
查看>>