`

xml解析

    博客分类:
  • XML
xml 
阅读更多
package com;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.jdom.input.SAXBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.helpers.DefaultHandler;

public class ReadXmlFile {
        String filePath="E:\\workspace\\test\\src\\fileClassCountSql.xml";
        //第一种 用DOM解析XML文档
        public String getValueByAttrForDom(String attrId) throws Exception {
                DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory
                                .newInstance();
                DocumentBuilder dBuilder = dBuilderFactory.newDocumentBuilder();
                Document document = dBuilder
                                .parse(getFilePath()+File.separator+"fileClassCountSql.xml");
                // 得到根元素
                Element rootElement = document.getDocumentElement();
                System.out.println("rootElement=" + rootElement);

                NodeList nameList2 = rootElement.getElementsByTagName("name");

                for (int i = 0; i < nameList2.getLength(); i++) {
                        Element nameElement = (Element) nameList2.item(i);
                        if ((nameElement.getAttribute("id")).equals(attrId)) {
                                return nameElement.getTextContent();
                        }
                }
                return null;
        }
       
        //第二种用SAX解析XML文档
        public String getValueByAttrForSax(String attrId)throws Exception{
                SAXParserFactory sFactory=SAXParserFactory.newInstance();
                SAXParser sp= sFactory.newSAXParser();
                sp.parse(filePath, new DefaultHandler());
                return null;
        }
       
        //使用Jdom对XML进行解析
        public String getValueByAttrForJdom(String attrName){
/*                SAXBuilder saBuilder=new SAXBuilder();
                String sqlString="";
                org.jdom.Document document=saBuilder.build(getCurrentPath()+File.separator+fileName);
                Element root=document.getRootElement();
                List list=root.getChildren();
                for (int i = 0; i < list.size(); i++) {
                        Element element=(Element)list.get(i);
                        if(element.getAttributeValue("id").equals(attrName)){
                                sqlString=element.getValue();
                                return sqlString;
                        }
                }
                return sqlString;*/
               
                return null;
        }
        public String getFilePath() throws Exception{
                String pathString=ReadXmlFile.class.getProtectionDomain().getCodeSource().getLocation().getPath();
                pathString=java.net.URLDecoder.decode(pathString, "UTF-8");
                int index = pathString.lastIndexOf("/classes");
                pathString = pathString.substring(0, index);
                System.out.println("pathString="+pathString);
                return pathString;
        }
       
        static public String getCurrentPath() throws UnsupportedEncodingException {
                String path = ReadXmlFile.class.getProtectionDomain().getCodeSource().getLocation().getFile();
                path = java.net.URLDecoder.decode(path, "UTF-8");

                int index = path.lastIndexOf("/classes");
                path = path.substring(0, index);
               
                System.out.println("CB xml path = [ " + path + " ]");
               
                return path;
        }
       
        public static void main(String[] args) throws Exception {
                        StringBuilder builder=new StringBuilder(new ReadXmlFile().getValueByAttrForDom("ivpnFirstInsertSql"));
                        String sqlString=new ReadXmlFile().getValueByAttrForDom("ivpnFirstInsertSql");
                        String s1= sqlString.replaceAll("p_lastCycle","20130731");
                        String s2=s1.replaceAll("p_preCycle","20130701");
                        System.out.println(s2);
                       
        }

}

                                                                                                                2013-08-01 19:37 记 @jinrongdajie31.xichengqu.beijing
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics