Sax,Pull,Dom解析

9802 ワード

Sax解析:
イベント処理クラス:MyHandler.java
public class MyHandler extends DefaultHandler
{
    private String mapName;
    
    //        Map
    private Map<String, String> map;
    
    //       map   
    private List<Map<String, String>> list;
    
    //      ,  map k 
    private String currentTag;
    
    //       ,  map value 
    private String currentValue;
    
    public List<Map<String, String>> getList()
    {
        return list;
    }
    
    public MyHandler(String mapName)
    {
        this.mapName = mapName;
    }
    
    @Override
    public void startDocument()
        throws SAXException
    {
        super.startDocument();
        list = new ArrayList<Map<String, String>>();
    }
    
    @Override
    public void endDocument()
        throws SAXException
    {
        super.endDocument();
    }
    /**
     * 
     */
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
        throws SAXException
    {
        super.startElement(uri, localName, qName, attributes);
        if (qName.equals(mapName))
        {
            map = new HashMap<String, String>();
        }
        if (attributes != null && map != null)
        {
            for (int i = 0; i < attributes.getLength(); i++)
            {
                //     (<Person id ="1")     id
                map.put(attributes.getQName(i), attributes.getValue(i));
            }
        }
        currentTag = qName;
    }
    
    @Override
    public void endElement(String uri, String localName, String qName)
        throws SAXException
    {
        super.endElement(uri, localName, qName);
        if (qName.equals(mapName))
        {
            System.out.println("endElement-->qName ==mapName set map null");
            list.add(map);
            map = null;
        }
    }
    
    @Override
    public void characters(char[] ch, int start, int length)
        throws SAXException
    {
        super.characters(ch, start, length);
        if (currentTag != null && map != null)
        {
            currentValue = new String(ch, start, length);
            //   map   "","
" if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("
")) { map.put(currentTag, currentValue); } } currentTag = null; currentValue = null; } }

XmlParserService.xml
public class XmlParserService
{
    public static List<Map<String, String>> readXml(InputStream in, String mapName)
    {
        //   static  newInstance  SaxParser   
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
            //   saxparser
            SAXParser parser = factory.newSAXParser();
            MyHandler myHandler = new MyHandler(mapName);
            //         Handler  SAXParser  
            parser.parse(in, myHandler);
            // notice:   inputStream     close
            in.close();
            return myHandler.getList();
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        return null;
    }
}

Pull解析:
public static List<Map<String, Object>> PullParserXml(InputStream in, String mapName,String inputEncoding)
    {
        List<Map<String, Object>> list = null;
        Map<String, Object> map = null;
        try
        {
            //   pullparserfactory  pullparser  
            XmlPullParserFactory fac = XmlPullParserFactory.newInstance();
            XmlPullParser pullParser = fac.newPullParser();
            pullParser.setInput(in, inputEncoding);
            //      
            String currentTag = null;
            //          
            String currentVal = null;
            int EventType =pullParser.getEventType();
            while (EventType != XmlPullParser.END_DOCUMENT)
            {
                switch (EventType)
                {
                    case XmlPullParser.START_DOCUMENT:
                    {
                        list = new ArrayList<Map<String, Object>>();
                        break;
                    }
                    case XmlPullParser.START_TAG:
                    {
                        if (mapName.equals(pullParser.getName()))
                        {
                            map = new HashMap<String, Object>();
                        }
                        //         
                        if (map != null && pullParser.getAttributeCount() > 0)
                        {
                            for (int i = 0; i < pullParser.getAttributeCount(); i++)
                            {
                                map.put(pullParser.getAttributeName(i), pullParser.getAttributeValue(i));
                            }
                        }
                        currentTag = pullParser.getName();
                        break;
                    }
                    case XmlPullParser.TEXT:
                    {
                        if (map != null && currentTag != null)
                        {
                            if (map != null && !pullParser.getText().trim().equals("")
                                && !pullParser.getText().trim().equals("
")) { currentVal = pullParser.getText().trim(); map.put(currentTag, currentVal); } } currentVal =null; currentTag =null; break; } case XmlPullParser.END_TAG: { if(mapName.equals(pullParser.getName())) { list.add(map); map =null; } break; } case XmlPullParser.END_DOCUMENT: { break; } } // EventType =pullParser.next(); } } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return list; }

Dom解析:
 public static List<Map<String, Object>> DomParser(InputStream in, String mapName)
    {
        List<Map<String, Object>> list = null;
        try
        {
            DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = fac.newDocumentBuilder();
            //   DocumentBuilder    Document
            Document doc = builder.parse(in);
            list = parseDocument(doc, mapName);
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        }
        catch (SAXException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return list;
    }
    
    private static List<Map<String, Object>> parseDocument(Document doc, String mapName)
    {
        List<Map<String, Object>> list = null;
        Map<String, Object> map = null;
        //      
        Element root = doc.getDocumentElement();
        System.out.println("-->new list");
        list = new ArrayList<Map<String, Object>>();
        NodeList childList = root.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++)
        {
            Node child = childList.item(i);
            //             map
            if (child.getNodeName().equals(mapName))
            {
                map = new HashMap<String, Object>();
            }
            //        
            if (child.hasAttributes())
            {
                NamedNodeMap attr = child.getAttributes();
                if (map != null)
                {
                    for (int j = 0; j < attr.getLength(); j++)
                    {
                        //notice:    j     i
                        map.put(attr.item(j).getNodeName().trim(), attr.item(j).getNodeValue().trim());
                    }
                }
                
            }
            //       Element   
            NodeList child2 = child.getChildNodes();
            for (int k = 0; k < child2.getLength(); k++)
            {
                //notice:    k     i
                Node child2_item = child2.item(k);
                if (child2_item instanceof Element)
                {
                    Element child2_ele = (Element)child2_item;
                    map.put(child2_ele.getTagName().trim(), child2_ele.getTextContent().trim());
                }
            }
            if (map != null)
            {
                list.add(map);
                map = null;
            }
            
        }
        return list;
    }