一个最最简单的xml sax writer.

博客讲述因Java中未找到提供SAX writer功能的包,作者自行编写了一个约100行的代码来按SAX方式输出XML文件,给出了使用示例和代码实现,还表示代码虽可优化,但能满足简单需求,同时对JAXP未提供该功能表示疑惑。

JAVA 的xml包不少,可就没找到一个包提供了SAX writer的功能,正好需要按照SAX方式输出xml文件,所以自己写了一个,包含注释大约100行,使用起来也颇为简单。
/*
* Created on 2005-1-29
*
* Mininal sax writer. you can use it like that:
* XmlWriter.startDocument();
* ...
* XmlWriter.startElement("Book");
* XmlWriter.addAttribute("title","book title");
* XmlWriter.addAttribute("price","$100");
* XmlWriter.character("This is a good book");
* XmlWriter.endElement("Book");
* ...
* XmlWriter.endDocument();
*/
package com.dyz.xfe;

import java.io.*;

public class XmlWriter{

private BufferedWriter writer = null;
private StringBuffer buffer = new StringBuffer();
private boolean isRoot = true;
private boolean closed = false;

public XmlWriter(String filename)
{
try {
writer = new BufferedWriter( new FileWriter(filename) );
} catch (IOException e) {
e.printStackTrace();
}
}

public void startDocument()
{
try {
writer.write("<?xml version=/"1.0/"?>");
} catch (IOException e) {
e.printStackTrace();
}
}

public void endDocument()
{
try {
if( buffer.length() > 0 )
writer.write(buffer.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public void startElement(String elem)
{
if( isRoot )
isRoot = false;
else
{
if( !closed )
buffer.append(">");
}
if( buffer.length() > 4096 )
{
try {
writer.write(buffer.toString());
} catch (IOException e) {
e.printStackTrace();
}
buffer.delete(0,buffer.length());
}
buffer.append("<" + elem);
closed = false;
}

public void endElement(String elem)
{
if( !closed )
buffer.append(">");
buffer.append("</" + elem + ">");
closed = true;
}

/**
* Output a xml attribute.
* @param name attribute name.
* @param value attribute value.
*/
public void addAttribute(String name, String value)
{
buffer.append( " " + name + "=/"" + value + "/"" );
}

/**
* Output a text node.
* @param value
*/
public void character(String value)
{
if( !closed )
buffer.append(">");
buffer.append(value);
closed = true;
}
}

其实还可以做进一步优化,不过对于一些简单的需求完全足够,只是不明白JAXP就不提供?

经DataX智能分析,该任务可能的错误原因是: com.alibaba.datax.common.exception.DataXException: Code:[Framework-02], Description:[DataX引擎运行过程出错,具体原因请参看DataX运行结束时的错误诊断信息 .]. - java.lang.RuntimeException: org.xml.sax.SAXParseException; Premature end of file. at org.apache.hadoop.conf.Configuration.loadResource(Configuration.java:2645) at org.apache.hadoop.conf.Configuration.loadResources(Configuration.java:2492) at org.apache.hadoop.conf.Configuration.getProps(Configuration.java:2405) at org.apache.hadoop.conf.Configuration.set(Configuration.java:1143) at org.apache.hadoop.conf.Configuration.set(Configuration.java:1115) at com.alibaba.datax.plugin.writer.hdfswriter.HdfsHelper.getFileSystem(HdfsHelper.java:62) at com.alibaba.datax.plugin.writer.hdfswriter.HdfsWriter$Job.init(HdfsWriter.java:49) at com.alibaba.datax.core.job.JobContainer.initJobWriter(JobContainer.java:704) at com.alibaba.datax.core.job.JobContainer.init(JobContainer.java:304) at com.alibaba.datax.core.job.JobContainer.start(JobContainer.java:113) at com.alibaba.datax.core.Engine.start(Engine.java:93) at com.alibaba.datax.core.Engine.entry(Engine.java:175) at com.alibaba.datax.core.Engine.main(Engine.java:208) Caused by: org.xml.sax.SAXParseException; Premature end of file. at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:150) at org.apache.hadoop.conf.Configuration.parse(Configuration.java:2480) at org.apache.hadoop.conf.Configuration.parse(Configuration.java:2468) at org.apache.hadoop.conf.Configuration.loadResource(Configuration.java:2539) ... 12 more - java.lang.RuntimeException: org.xml.sax.SAXParseException; Premature end of file. at org.apache.hadoop.conf.Configuration.loadResource(Configuration.java:2645) at org.apache.hadoop.conf.Configuration.loadResources(Configuration.java:2492) at org.apache.hadoop.conf.Configuration.getProps(Configuration.jav
最新发布
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值