栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

同时写入XML文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

同时写入XML文件

这是最终生效的代码!有关不同部分的说明,请参阅问题更新。

import java.io.*;import java.nio.channels.*;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import javax.xml.xpath.*;import org.w3c.dom.*;import org.xml.sax.SAXException;public class Test2{     String path = "...Test 2.xml";    public Test2(){        document doc = null;        documentBuilderFactory dbFactory;        documentBuilder dBuilder;        NodeList itemList;        Transformer transformer;        FileChannel channel;         Element newElement;        int prevNumber;        TransformerFactory transformerFactory ;        DOMSource source;        StreamResult result;        NonClosingInputStream ncis = null;        try { channel = new RandomAccessFile(new File(path), "rw").getChannel(); FileLock lock = channel.lock(0L, Long.MAX_VALUE, false); try {     dbFactory = documentBuilderFactory.newInstance();     dBuilder = dbFactory.newdocumentBuilder();     ncis = new NonClosingInputStream(Channels.newInputStream(channel));     doc = dBuilder.parse(ncis); } catch (SAXException | IOException | ParserConfigurationException e) {     e.printStackTrace(); } doc.getdocumentElement().normalize(); itemList = doc.getElementsByTagName("Item"); newElement = doc.createElement("Item"); prevNumber = Integer.parseInt(((Element) itemList.item(itemList.getLength() - 1)).getAttribute("Number")); newElement.setAttribute("Number", (prevNumber + 1) + ""); doc.getdocumentElement().appendChild(newElement); transformerFactory = TransformerFactory.newInstance(); transformer = transformerFactory.newTransformer(); source = new DOMSource(doc); channel.truncate(0); result = new StreamResult(Channels.newOutputStream(channel)); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); channel.close();        } catch (IOException | TransformerException e) { e.printStackTrace();        } finally { try {     ncis.reallyClose(); } catch (IOException e) {     e.printStackTrace(); }        }    }    class NonClosingInputStream extends FilterInputStream {        public NonClosingInputStream(InputStream it) { super(it);        }        @Override        public void close() throws IOException { // Do nothing.        }        public void reallyClose() throws IOException { // Actually close. in.close();        }    }    public static void main(String[] args){        new Test2();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506444.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号