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");
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值