1、xml测试文件:data.xml
2、接收实体类Entity.java: GA int 1 100 GB string 100 GC enum 1-100,255
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 Listrange = 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"); MapresultMap = 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}}