场景
- 文件的写入目前没有发现可以增量写入的,只能是完全重新写入。对于大量数据需要写入
XML
文件,还是和读XML
文件一样,不需要生成DOM
模型能节省不少的内存和指令。
说明
- 在
java
标准库里,也是有相关的SAX类来写入数据流,StreamResult(OutputStream)
负责把数据写入输出流。
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
FileOutputStream fo = new FileOutputStream(file);
StreamResult streamResult = new StreamResult(fo);
-
之后创建一个
TransformerHandler
来负责绑定StreamResult
并开始写入元素名称,元素值,属性值,子元素等。 -
Handler
还可以设置输出是否带缩进,输出的编码格式。
例子
- 这里例子是生成
sitemap.xml
文件,这里的Sitemap
是Map
对象。SitemapReader
是
SitemapWriter
import Sitemap;
import StringUtils;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
public class SitemapWriter {
public boolean writeToFile(File file, List<Sitemap> sitemaps){
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
URL resource = this.getClass().getResource("/");
String path = resource.getPath();
boolean result = false;
try(FileOutputStream fo = new FileOutputStream(file);) {
TransformerHandler handler = factory.newTransformerHandler();
Transformer transformer = handler.getTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
StreamResult streamResult = new StreamResult(fo);
handler.setResult(streamResult);
handler.startDocument();
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("","","xmlns","","http://www.sitemaps.org/schemas/sitemap/0.9");
attributes.addAttribute("","","xmlns:xhtml","","http://www.w3.org/1999/xhtml");
handler.startElement("", "", "urlset", attributes);
attributes.clear();
// url
for (int i = 0; i < sitemaps.size(); i++) {
Sitemap one = sitemaps.get(i);
handler.startElement("","","url",null);
handler.startElement("","","loc",null);
String loc = StringUtils.nullToEmpty(one.getStr("loc"));
handler.characters(loc.toCharArray(), 0, loc.length());
handler.endElement("","","loc");
handler.startElement("","","priority",null);
String priority = StringUtils.nullToEmpty(one.getStr("priority"));
handler.characters(priority.toCharArray(), 0, priority.length());
handler.endElement("","","priority");
handler.startElement("","","lastmod",null);
String lastmod = StringUtils.nullToEmpty(one.getStr("lastmod"));
handler.characters(lastmod.toCharArray(), 0, lastmod.length());
handler.endElement("","","lastmod");
handler.startElement("","","changefreq",null);
String changefreq = StringUtils.nullToEmpty(one.getStr("changefreq"));
handler.characters(changefreq.toCharArray(), 0, changefreq.length());
handler.endElement("","","changefreq");
handler.endElement("","","url");
}
handler.endElement("","","urlset");
handler.endDocument();
result = true;
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return result;
}
}
TestSitemapWriter
import Sitemap;
import SitemapReader;
import SitemapWriter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import javax.xml.transform.sax.SAXTransformerFactory;
import java.io.File;
import java.net.URL;
import java.util.List;
@RunWith(JUnit4.class)
public class TestSitemapWriter {
@Test
public void testSitemapWriter(){
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
URL resource = this.getClass().getResource("/");
String path = resource.getPath();
File file = new File(path+"..\\..\\..\\..\\doc\\tests\\xml\\sitemap.xml");
SitemapReader sr = new SitemapReader();
if(sr.readAll(file)){
List<Sitemap> sitemaps = sr.getSitemaps();
file = new File(path+"\\sitemap.xml");
SitemapWriter writer = new SitemapWriter();
writer.writeToFile(file,sitemaps);
}
}
}
参考
-
Java——SAX方式生成XML
-
SAXTransformerFactory文章来源:https://www.toymoban.com/news/detail-692992.html
-
以SAX流的方式高效读取XML大文件文章来源地址https://www.toymoban.com/news/detail-692992.html
到了这里,关于[Java]_[初级]_[使用SAX流的方式写入XML文件]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!