xml 解析

package xmltool;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathException;
import javax.xml.xpath.XPathExpression;
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 ParseXml {
	
	public static Document getDocument(File file) throws ParserConfigurationException, SAXException, IOException  {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		return builder.parse(file);
	}
	
	public static Document getDocument(String  file) throws ParserConfigurationException, SAXException, IOException  {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		return builder.parse(file);
	}
	
	public static void writeToXml(Document document,String fileName) throws TransformerException, Exception  {
		TransformerFactory factory = TransformerFactory.newInstance();
		Transformer tf = factory.newTransformer();
		tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(fileName)));
	}
	
	public static NodeList xpath(String compile,Document document) throws XPathException 
	{
		XPathFactory xPathfactory = XPathFactory.newInstance();
		XPath xpath = xPathfactory.newXPath();
		XPathExpression expr = xpath.compile("/repository/sqls/sql[contains(@name,'update')]");
	
	//xpath 2.0 目前java api 只支持xpath1.0
	//	XPathExpression expr = xpath.compile("/repository/sqls/sql[matches(@name,'update*')]");

	//	XPathExpression expr = xpath.compile("/repository/sqls/sql[@name='updategl_drill_stem_test']");
		return (NodeList) expr.evaluate(document, XPathConstants.NODESET);
		
	}
	
	public static SqlConfig parseSql(Document document) throws XPathExpressionException
	{
		XPathFactory xPathfactory = XPathFactory.newInstance();
		XPath xpath = xPathfactory.newXPath();
		XPathExpression expr = xpath.compile("/repository/sqls/sql");
		NodeList nodeList=(NodeList) expr.evaluate(document, XPathConstants.NODESET);
		for(int i=0;i<nodeList.getLength();i++)
		{
			Element element=(Element)nodeList.item(i);
		    System.out.println(	element.getAttribute("outTypes"));
		     System.out.println(element.getTextContent());
			element.setTextContent("a");
		}
		return null;

	}
	
	public static void dealverModifyTime(NodeList nl)
	{
		if(nl.getLength()>0)
		{	
		String s=nl.item(0).getTextContent();
		s=s.replace(":modifyTime", "getdate()");
		s=s.replace(":ver", "ver+1");
		nl.item(0).setTextContent(s);
		//filename="C:\\Users\\Administrator\\Desktop\\gl_drill_stem_test.xml";
		//writeToXml(document,);
		}
	}
	public static void main(String args[]) throws Exception
	{
	
		Document document=getDocument("src/xmltool/gl_drill_stem_test.xml");
		parseSql(document);
	String	filename="C:\\Users\\Administrator\\Desktop\\gl_drill_stem_test.xml";
	//	writeToXml(document,filename);
	/*	NodeList nl=xpath("s",document);
		System.out.println(nl.getLength());
		System.out.println(nl.item(0).getTextContent());
	*/
		/*for(int i=0;i<nodeList.getLength();i++)
		{
			Element e=(Element)nodeList.item(i);	
			System.out.println(e.getAttribute("name"));
		    System.out.println( nodeList.item(i).getTextContent());
		}*/
	
		System.out.println("s");
	}

}

你可能感兴趣的:(xml 解析)