- package org.csc.xml;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import org.xml.sax.SAXException;
- import pojos.DicProvinceCity;
- import pojos.DicProvinceCityDAO;
- public class ViewXml {
- private Document doc;
- /**
- * 初始化
- * @param xmlFile
- * @throws ParserConfigurationException
- * @throws IOException
- * @throws SAXException
- */
- public void init(String xmlFile) throws ParserConfigurationException, SAXException, IOException{
- DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
- doc = documentBuilder.parse(new File(xmlFile));
- }
- /**得到省市***/
- public Map<String,Integer> getProvinceMap(String xmlFile) throws Exception{
- init(xmlFile);
- Map<String,Integer> map = new HashMap<String, Integer>();
- /***根节点**/
- Element rootElement = doc.getDocumentElement();
- System.out.println("根节点:"+rootElement.getTagName());
- /**得到元素名为classic1的节点**/
- NodeList nodeList = rootElement.getElementsByTagName("class1");
- for(int i=0;i<nodeList.getLength();i++){
- Integer id = Integer.parseInt(nodeList.item(i).getAttributes().item(0).getNodeValue());
- String name = nodeList.item(i).getAttributes().item(1).getNodeValue();
- System.out.println(id+name);
- map.put(name, id);
- }
- return map;
- }
- }