`
balaschen
  • 浏览: 192235 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

利用sax和xslt转换csv文件内容

阅读更多
package jaxp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;

public class SAX_XSL {
	
	public static void main(String[] args) throws Exception{
		transform_by_saxsource();
		transform_by_saxtransformfactory();
	}


	private static void transform_by_saxtransformfactory() throws TransformerFactoryConfigurationError, TransformerConfigurationException, IOException, SAXException {
		TransformerFactory factory = TransformerFactory.newInstance();
		if(factory.getFeature(SAXTransformerFactory.FEATURE)){
			SAXTransformerFactory saxfactory = (SAXTransformerFactory) factory;
			TransformerHandler handler = saxfactory.newTransformerHandler(new StreamSource(loadxsl()));
			handler.setResult(new StreamResult(System.out));
			CSVParser parser = new CSVParser();
			parser.setContentHandler(handler);
			System.out.println("//------------华丽的分割线----------------//");
			parser.parse(new InputSource(loadcsv()));
		}
	}


	private static void transform_by_saxsource() throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
		TransformerFactory factory = TransformerFactory.newInstance();
		Templates templates=  factory.newTemplates(new StreamSource(loadxsl()));
		Transformer transformer = templates.newTransformer();
		
		Source source = new SAXSource(new CSVParser(),new InputSource(loadcsv()));
		Result result = new StreamResult(System.out);
		transformer.transform(source, result);
		
	}


	private static InputStream loadxsl() {
		InputStream xsl = SAX_XSL.class.getClassLoader().getResourceAsStream("jaxp/csv.xsl");
		return xsl;
	}
	
	private static InputStream loadcsv() {
		InputStream xsl = SAX_XSL.class.getClassLoader().getResourceAsStream("jaxp/book.csv");
		return xsl;
	}
	
	public static class CSVParser implements XMLReader {
		private static final Attributes EMPTY_ATTRS = new AttributesImpl();
		private static final String HTTP_XML_ORG_SAX_FEATURES_NAMESPACES="http://xml.org/sax/features/namespaces";
		private ContentHandler contentHandler;
		private DTDHandler dtdHandler;
		private EntityResolver entityResolver;
		private ErrorHandler errorhandler;
		
		private Map featureMap = new HashMap();
		private Map propertyMap = new HashMap();
		
		public CSVParser() {
			featureMap.put(HTTP_XML_ORG_SAX_FEATURES_NAMESPACES, new Boolean(true));
		}

		public ContentHandler getContentHandler() {
			return this.contentHandler;
		}

		public void setContentHandler(ContentHandler contenthandler) {
			this.contentHandler = contenthandler;
		}
		
		public DTDHandler getDTDHandler() {
			return this.getDTDHandler();
		}

		public void setDTDHandler(DTDHandler dtdhandler) {
			this.dtdHandler = dtdhandler;
		}
		
		public EntityResolver getEntityResolver() {
			return this.entityResolver;
		}
		
		public void setEntityResolver(EntityResolver entityresolver) {
			this.entityResolver = entityresolver;
		}

		public ErrorHandler getErrorHandler() {
			return this.errorhandler;
		}

		public void setErrorHandler(ErrorHandler errorhandler) {
			this.errorhandler = errorhandler;
		}
		
		public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
			Boolean value = (Boolean) featureMap.get(name);
			if(value == null){
				return false;
			}
			return value.booleanValue();
		}

		public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException {
			this.featureMap.put(name, new Boolean(value));			
		}
		
		public void setProperty(String name, Object value) throws SAXNotRecognizedException, SAXNotSupportedException {
			propertyMap.put(name, value);
		}
		
		public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
			return propertyMap.get(name);
		}

		public void parse(String systemId) throws IOException, SAXException {
			parse(new InputSource(systemId));			
		}
		
		private boolean requiredNameSpace() throws SAXException {
			return getFeature(HTTP_XML_ORG_SAX_FEATURES_NAMESPACES);
		}
		
		public void parse(InputSource inputsource) throws IOException, SAXException {
			ContentHandler handler = getContentHandler();
			if(handler == null){
				return;
			}
			BufferedReader reader = null;
			if(inputsource.getCharacterStream()!=null){
				reader = new BufferedReader(inputsource.getCharacterStream());
			} else if (inputsource.getByteStream()!=null){
				reader = new BufferedReader(new InputStreamReader(inputsource.getByteStream()));
			} else if (inputsource.getSystemId() !=null && inputsource.getSystemId().trim().length()>0){
				URL url = new URL(inputsource.getSystemId());
				reader = new BufferedReader(new InputStreamReader(url.openStream()));
			}
			if(reader == null){
				throw new SAXException("invalid inputsource object");
			}
			handler.startDocument();
			if(requiredNameSpace()){
				handler.startElement("", "books", "books", EMPTY_ATTRS);
			} else {
				handler.startElement("", "", "books", EMPTY_ATTRS);
			}
			
			String line = null;
			while((line = reader.readLine())!=null){
				handler.startElement("", "book", "book", EMPTY_ATTRS);
				String[] values = line.split(",");
				if(values!=null && values.length>0){
					handler.startElement("", "name", "name", EMPTY_ATTRS);
					handler.characters(values[0].toCharArray(), 0, values[0].length());
					handler.endElement("", "name", "name");
					if(values.length>=2){
						handler.startElement("", "authors", "authors", EMPTY_ATTRS);
						handler.startElement("", "author", "author", EMPTY_ATTRS);
						handler.characters(values[1].toCharArray(), 0, values[1].length());
						handler.endElement("", "author", "author");
						handler.endElement("", "authors", "authors");
					}
				}
				handler.endElement("", "book", "book");
			}
			if(requiredNameSpace()){
				handler.endElement("", "books", "books");
			} else {
				handler.endElement("", "", "books");
			}
			handler.endDocument();
		}
	}
}

 

book.csv:

java & xml,dava
java xslt,martin
hibernate in action,gavin king
spring in action,test

 csv.xsl:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	
	<xsl:template match="/">
		<html>
		<head>
			<title>books</title>
		</head>
		<body>
		<xsl:apply-templates/>
		</body>
		</html>
	</xsl:template>
	
	<xsl:template match="books">
		<table border="1">
			<tr>
				<td>no</td>
				<td>book name</td>
				<td>authors</td>
			</tr>
			<xsl:apply-templates select="book">
				<xsl:sort order="desending" data-type="text" select="name"/>
			</xsl:apply-templates>
		</table>
	</xsl:template>
	
	<xsl:template match="book">
		<tr>
			<td><xsl:value-of select="position()"/></td>
			<td><xsl:value-of select="name"/></td>
			<td><xsl:value-of select="authors"/></td>
		</tr>
	</xsl:template>
</xsl:stylesheet>
 

 

分享到:
评论

相关推荐

    XML数据转换

    六、XSLT转换 XSLT(eXtensible Stylesheet Language Transformations)是一种专门用于转换XML文档的编程语言。通过编写XSL样式表,可以定义XML到XML、XML到HTML或其他格式的转换规则。XSLT具有强大的节点处理能力,...

    解析Excel和XML

    解析Excel文件意味着将其中的数据提取出来,或者将其内容转换为其他格式。这可以通过多种方式实现: 1. **使用APIs**:例如,Microsoft提供的COM接口(如Excel Interop)允许在.NET环境中直接操作Excel文件。此外,...

    完整版xml转换objectdemo,转化率100%非常好用

    这可能包含XML文件、对应的Java对象类、转换脚本或测试代码,帮助用户理解工具的使用方法和验证转换效果。 5. **实际应用**:在软件开发中,XML转换常用于API交互、配置文件读取、数据库数据交换等。例如,Web服务...

    xml java tutorial

    本文将深入探讨如何利用Java处理XML文档,通过使用Document Object Model (DOM),Simple API for XML (SAX),以及Extensible Stylesheet Language Transformations (XSLT)等技术来解析、操作和转换XML数据。...

    XmlAnalysis.zip

    8. **XSLT**:XML样式表转换(XSLT)用于转换XML文档的结构和内容。它可以将XML转换为HTML、另一个XML格式,甚至是文本或CSV文件。 9. **编程库与API**:在不同编程语言中,如Java(JAXB, DOM, SAX, XSLT),C#...

    PyPI 官网下载 | lxml-4.6.1-cp36-cp36m-macosx_10_9_x86_64.whl

    3. **XPath和XSLT转换**: 除了解析,`lxml`还支持XSLT转换,允许将一个XML文档转换为另一个XML或HTML文档,极大地扩展了数据处理能力。 4. **ElementTree API兼容**: `lxml`与Python的内置`xml.etree.ElementTree`...

    工作草案征求意见单-XML使用指南.doc

    2. **规范性引用文件**:XML遵循一系列标准和规范,如W3C的XML 1.0或1.1版本规范,以及XML Namespaces、XPath、XSLT和XSD(XML Schema Definition)等。这些规范定义了XML的语法、命名规则和验证机制。 3. **术语和...

    数据库与xml的导入导出

    XML的导入导出涉及XML解析、XPath查询、XSLT转换等技术,对于大型XML文件,还需要考虑性能优化,如使用DOM、SAX或StAX解析器。在实际应用中,根据数据量、复杂性和性能需求选择合适的方法。 总结,数据库与XML的...

    XML大全

    通过XSLT,我们可以将XML数据转换成HTML,便于网页展示,或者转换成其他格式,如CSV,适应不同的应用场景。 XML大全的学习涵盖XML的基础语法、解析方法、验证机制(如DTD、XML Schema)、XPath(XML Path Language...

    学生信息管理系统(基于XML)

    信息更新是对XML文档进行修改,而导出报表则可能需要将XML数据转换为更友好的格式,如CSV或PDF,这可以通过Java的转换库如XSLT(Extensible Stylesheet Language Transformations)来完成。 在实际开发中,为了保证...

    aduna-commons-xml-2.6.0.jar.zip

    5. **XML转换**:库还包括了一些XML转换工具,如将XML转换为其他格式,如HTML或CSV,或者执行XSLT(Extensible Stylesheet Language Transformations)转换。 在实际应用中,Aduna Commons XML常被用于需要大量处理...

Global site tag (gtag.js) - Google Analytics