java read xml

package com.solq.test;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ToolXML {
    public static Map<String,Document> cacheXML= new HashMap<String,Document>();
    public static Document getXMLDom(String xml)
    {
        if(cacheXML.containsKey(xml)) return cacheXML.get(xml);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        File file = new File(xml);
        try {
            builder = factory.newDocumentBuilder();
            doc=builder.parse(file);
        } 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();
        }
        cacheXML.put(xml, doc);
        return doc;
    }

    public static NodeList getXmlNodeList(String xml,String attr,String v)
    {
        Document doc=getXMLDom(xml);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        javax.xml.xpath.XPathExpression expr = null;
        NodeList  ar = null;
        try {
            expr = xpath.compile("//datas/data[@"+attr+"='"+v+"']");
            ar = (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return ar;
    }
    public static void main(String[] args)
    {
        
        NodeList  nodes =getXmlNodeList("skill.xml","skillType","1");
        NodeList  nodes1 =getXmlNodeList("skill.xml","skillType","1");
         for (int i = 0; i < nodes.getLength(); i++) {
             Element el = (Element) nodes.item(i);
              System.out.println(el.getAttribute("skillName")); 
          }

    }
}
package com.solq.tool;

import java.io.File;

class XmlDomFileInfo{
    public long lastModified=0;
    public Document dom;
}
public class XMLHelper {
    private static Map<String,XmlDomFileInfo> cacheXML= new HashMap<String,XmlDomFileInfo>();
    private static int updateTime=24*60*60*1000;
    public static Document getXMLDom(String xml)
    {
        //Long lastModified = file.lastModified();
        //cache
        if(cacheXML.containsKey(xml)){
        
            if(cacheXML.get(xml).lastModified+updateTime>new Date().getTime())
            //if(cacheXML.get(xml).lastModified==lastModified) // new Date().getTime()+updateTime
            {
                //System.out.println("load cache xml");
                //System.out.println("return lastModified: "+cacheXML.get(xml).lastModified+" new Date().getTime()+updateTime :"+lastModified);
                return cacheXML.get(xml).dom;
            }
        }
        //System.out.println("one load");
        //no cache reRead xml
        File file = new File("xml/"+xml);
        Document doc = null;
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;

        try {
            builder = factory.newDocumentBuilder();
            doc=builder.parse(file);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        XmlDomFileInfo info=new XmlDomFileInfo();
        info.dom=doc;
        info.lastModified=new Date().getTime();

        cacheXML.put(xml, info);
        return doc;
    }

    public static NodeList getXmlNodeList(String xml,String attr,String id)
    {
        Document doc=getXMLDom(xml);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        //javax.xml.xpath.XPathExpression expr = null;
        NodeList  ar =null;
        try {
            //expr = xpath.compile("//datas/data[@"+attr+"='"+v+"']");
            ar = (NodeList)xpath.evaluate("//datas/data[@"+id+"='"+attr+"']",doc, XPathConstants.NODESET);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return ar;
    }
    public static Element getXmlElement(String xml,String attr,String id)
    {
        Document doc=getXMLDom(xml);
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        Element  el =null;
        try {
            el=(Element)xpath.evaluate("//datas/data[@"+id+"='"+attr+"']",  doc, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            e.printStackTrace();
        }
        return el;
    }
    public static String getXmlAttr(String xml,String rv,String attr,String id)
    {
        Element el=getXmlElement(xml,attr,id);
        if(el==null) return "";
        return el.getAttribute(rv);
        
    }
    public static void test()
    {
        NodeList nodes =getXmlNodeList("skill.xml","1","skillType");
        if(nodes!=null)
         for (int i = 0; i < nodes.getLength(); i++) {
             Element el = (Element) nodes.item(i);
             System.out.println(el.getAttribute("skillName")); 
         }
        
        Element el =getXmlElement("skill.xml","2","skillType");
        
        if(el!=null)
            System.out.println(el.getAttribute("skillName")); 

    }
    public static void main(String[] args)
    {
        test();
    }
}

//加了文件修改时间判断

原文地址:https://www.cnblogs.com/solq/p/2565812.html