package com.test.csv;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;public class TestReadXML
{
    public static HashMap parseXml(String xmlpath)
    {
     HashMap keys=null;
        File xmlfile = new File(xmlpath);
        Document doc=null;
        DocumentBuilder builder=null;
        try
        {
            builder = new DocumentBuilderFactoryImpl()
                    .newDocumentBuilder();
            doc = builder.parse(xmlfile);
            keys=getMapsByPath(doc, "Dev/ItemMarshal/Cat/SubCat/Item");
        }
        catch (ParserConfigurationException e)
        {
            e.printStackTrace();
        } catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
        return keys;
    }
    public static HashMap getMapsByPath(Document doc, String path)
    {
     HashMap xml=new HashMap();
        ArrayList nlist = new ArrayList();
        String[] paths = path.split("/");
        getNodesByPath(nlist, doc.getChildNodes(), paths);
        for (int i = 0; i < nlist.size(); i++)
        {
         Item item = new Item();
            Node node = (Node) nlist.get(i);
            if(null!=node.getAttributes().getNamedItem("GlobalNo").getNodeValue()){
             item.setNumber(node.getAttributes().getNamedItem("GlobalNo").getNodeValue());
            }
            if(null!=node.getAttributes().getNamedItem("name").getNodeValue()){
             item.setName(node.getAttributes().getNamedItem("name").getNodeValue());
            }
            NodeList list = node.getChildNodes();
                for (int k = 0; k < list.getLength(); k++)
                {
                    if (list.item(k).getNodeType() == Node.ELEMENT_NODE)
                    {
                        if (list.item(k).getNodeName().equals("CheckMethod"))
                        {
                         item.setMethod(list.item(k).getChildNodes().item(0).getNodeValue());
                        }
                        if (list.item(k).getNodeName().equals("std"))
                        {
                         item.setStand(list.item(k).getChildNodes().item(0).getNodeValue());
                        }
                        if (list.item(k).getNodeName().equals("Suggestion"))
                        {
                         item.setSuggestion(list.item(k).getChildNodes().item(0).getNodeValue());
                        }
                    }
                }
                xml.put(item.getNumber(), item);
                System.out.println(item.toString());
        }
        return xml;
    }
    public static void getNodesByPath(ArrayList al, NodeList nl, String[] paths)
    {
        for (int i = 0; i < nl.getLength(); i++)
        {
            if (paths.length > 1 && paths[0] != null
                    && nl.item(i).getNodeName().equals(paths[0]))
            {
                Node _node = nl.item(i);
                NodeList list = _node.getChildNodes();
                String[] _paths = new String[paths.length - 1];
                System.arraycopy(paths, 1, _paths, 0, paths.length - 1);
                getNodesByPath(al, list, _paths);
            }
            else if (paths.length == 1 && paths[0] != null
                    && nl.item(i).getNodeName().equals(paths[0]))
            {
                al.add(nl.item(i));
            }
        }
    }
}

解决方案 »

  1.   

    package com.test.csv;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;public class TestReadXML
    {
        public static HashMap parseXml(String xmlpath)
        {
         HashMap keys=null;
            File xmlfile = new File(xmlpath);
            Document doc=null;
            DocumentBuilder builder=null;
            try
            {
                builder = new DocumentBuilderFactoryImpl()
                        .newDocumentBuilder();
                doc = builder.parse(xmlfile);
                keys=getMapsByPath(doc, "Dev/ItemMarshal/Cat/SubCat/Item");
            }
            catch (ParserConfigurationException e)
            {
                e.printStackTrace();
            } catch (SAXException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
            return keys;
        }
        public static HashMap getMapsByPath(Document doc, String path)
        {
         HashMap xml=new HashMap();
            ArrayList nlist = new ArrayList();
            String[] paths = path.split("/");
            getNodesByPath(nlist, doc.getChildNodes(), paths);
            for (int i = 0; i < nlist.size(); i++)
            {
             Item item = new Item();
                Node node = (Node) nlist.get(i);
                if(null!=node.getAttributes().getNamedItem("GlobalNo").getNodeValue()){
                 item.setNumber(node.getAttributes().getNamedItem("GlobalNo").getNodeValue());
                }
                if(null!=node.getAttributes().getNamedItem("name").getNodeValue()){
                 item.setName(node.getAttributes().getNamedItem("name").getNodeValue());
                }
                NodeList list = node.getChildNodes();
                    for (int k = 0; k < list.getLength(); k++)
                    {
                        if (list.item(k).getNodeType() == Node.ELEMENT_NODE)
                        {
                            if (list.item(k).getNodeName().equals("CheckMethod"))
                            {
                             item.setMethod(list.item(k).getChildNodes().item(0).getNodeValue());
                            }
                            if (list.item(k).getNodeName().equals("std"))
                            {
                             item.setStand(list.item(k).getChildNodes().item(0).getNodeValue());
                            }
                            if (list.item(k).getNodeName().equals("Suggestion"))
                            {
                             item.setSuggestion(list.item(k).getChildNodes().item(0).getNodeValue());
                            }
                        }
                    }
                    xml.put(item.getNumber(), item);
                    System.out.println(item.toString());
            }
            return xml;
        }
        public static void getNodesByPath(ArrayList al, NodeList nl, String[] paths)
        {
            for (int i = 0; i < nl.getLength(); i++)
            {
                if (paths.length > 1 && paths[0] != null
                        && nl.item(i).getNodeName().equals(paths[0]))
                {
                    Node _node = nl.item(i);
                    NodeList list = _node.getChildNodes();
                    String[] _paths = new String[paths.length - 1];
                    System.arraycopy(paths, 1, _paths, 0, paths.length - 1);
                    getNodesByPath(al, list, _paths);
                }
                else if (paths.length == 1 && paths[0] != null
                        && nl.item(i).getNodeName().equals(paths[0]))
                {
                    al.add(nl.item(i));
                }
            }
        }
    }