有没有做家具特卖的网站,天津网站建设兼职,国家信用信息企业公示网官网,iis7 asp网站 503场景
XML文件作为默认utf8格式的文件#xff0c;它的作用和JSON文件相当。比如可以做为简单的数据存储格式#xff0c;配置文件#xff0c;网站的sitemap.xml导航等。它比json强的一点是它还有样式描述文件dtd,可以实现让XML里的结构化数据显示表格样式。
?xml versi…场景
XML文件作为默认utf8格式的文件它的作用和JSON文件相当。比如可以做为简单的数据存储格式配置文件网站的sitemap.xml导航等。它比json强的一点是它还有样式描述文件dtd,可以实现让XML里的结构化数据显示表格样式。
?xml version1.0 encodingUTF-8?sitemap.xml作为网站的站点地图提供了固定的格式化数据, 也方便了搜索引擎进行索引。 因此对sitemap.xml的读写功能是比较重要的。那么如何实现大数据量的XML读写才可以节省内存和减少指令的执行
?xml version1.0 encodingUTF-8?
urlset xmlnshttp://www.sitemaps.org/schemas/sitemap/0.9xmlns:xhtmlhttp://www.w3.org/1999/xhtmlurllochttps://blog.csdn.net/infoworld/locpriority1.0/prioritylastmod2023-07-28/lastmodchangefreqDaily/changefreq/url
/urlset说明
和JSON一样对XML读用流的方式可以减少中间的DOM模型的生成也不需要读取整个XML文件到内存。这样的API不需要借助第三方库Java标准库里就有。
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;流式读取使用SAX模型的方式从开头按起始标签和结束标签的方式进行读取数据。并需要实现自己的Handler来处理需要的元素。这个Handler就是模板类它的相关方法会在识别到XML特定的对象是调用比如属性起始结束元素元素值等。
public boolean readAll(File file){SAXParserFactory sf SAXParserFactory.newInstance();boolean result false;try {SAXParser sp sf.newSAXParser();sp.parse(file,this);logger.info(sitemaps.size());result true;} catch (ParserConfigurationException e) {logger.error(e.getMessage());} catch (SAXException e) {logger.error(e.getMessage());} catch (IOException e) {logger.error(e.getMessage());}return result;
}例子
以下是读取sitemap.xml文件的Reader实现Sitemap对象实际就是Map对象。
SitemapReader import Sitemap;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class SitemapReader extends DefaultHandler {private static Logger logger Logger.getLogger(SitemapReader.class);private ArrayListSitemap sitemaps new ArrayList();private Sitemap current;private String cPValue;private String cPName;/*** 重置Reader,可以再次读取*/public void reset(){sitemaps.clear();cPName null;cPValue null;current null;}/**** return 读取到的sitemaps;*/public ListSitemap getSitemaps(){return sitemaps;}/**** param file* return*/public boolean readAll(File file){SAXParserFactory sf SAXParserFactory.newInstance();boolean result false;try {SAXParser sp sf.newSAXParser();sp.parse(file,this);logger.info(sitemaps.size());result true;} catch (ParserConfigurationException e) {logger.error(e.getMessage());} catch (SAXException e) {logger.error(e.getMessage());} catch (IOException e) {logger.error(e.getMessage());}return result;}Overridepublic void characters(char[] ch, int start, int length) throws SAXException {if(cPName ! null current ! null) {cPValue new String(ch, start, length);current.put(cPName,cPValue);}}Overridepublic void endElement(String uri, String localName, String qName) throws SAXException {cPName null;cPValue null;switch(qName) {case url: {sitemaps.add(current);current null;break;}}}Overridepublic void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {cPName null;cPValue null;switch(qName){case url:{current new Sitemap();break;}default:cPName qName;}}
}
TestSitemapReader
import Sitemap;
import SitemapReader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.logging.Logger;RunWith(JUnit4.class)
public class TestSitemapReader{Logger logger Logger.getLogger(TestSitemapReader.class.getName());Testpublic void testXmlSaxReader(){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)){ListSitemap sitemaps sr.getSitemaps();logger.info(sitemaps.size());}}
}参考 SAX方式读取XML文件 SAXParser