把xml格式的字符串写入到一个xml文件中
- package demo;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.StringReader;
- import java.util.Date;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import javax.xml.parsers.ParserConfigurationException;
- import javax.xml.transform.OutputKeys;
- import javax.xml.transform.Transformer;
- import javax.xml.transform.TransformerFactory;
- import javax.xml.transform.dom.DOMSource;
- import javax.xml.transform.stream.StreamResult;
- import org.w3c.dom.Document;
- import org.xml.sax.InputSource;
- import org.xml.sax.SAXException;
- public class DocXml {
- public static void main(String[] args) {
- String s1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><img src=\"http://b0.upaiyun.com/1523978382096.jpg\"><h1>百度</h1></img>";
- Document doc = str2Document(s1);
- String fileName = "E:\\learn\\a.html";
- createXml(fileName, doc);
- }
- public static Document str2Document(String xmlStr) {
- Document doc = null;
- StringReader sr = new StringReader(xmlStr);
- InputSource is = new InputSource(sr);
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- DocumentBuilder builder;
- try {
- builder = factory.newDocumentBuilder();
- doc = builder.parse(is);
- } catch (ParserConfigurationException e) {
- System.out.println("ParserConfiguration错误"+e);
- } catch (SAXException e) {
- System.out.println("SAX错误"+e);
- } catch (IOException e) {
- System.out.println("IO错误"+e);
- }
- return doc;
- }
- public synchronized static void createXml(String fileName, Document document) {
- System.out.println("============进入生成xml方法:" + new Date().toLocaleString() + "=================");
- try {
- // 判断文件是否存在,如存在就删掉它
- File file = new File(fileName);
- if (file.exists()) {
- file.delete();
- System.out.println("==============删除xml文件==============");
- }
- /** 将document中的内容写入文件中 */
- TransformerFactory tFactory = TransformerFactory.newInstance();
- Transformer transformer = tFactory.newTransformer();
- transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
- transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- DOMSource source = new DOMSource(document);
- StreamResult result = new StreamResult(new FileOutputStream(fileName));
- transformer.transform(source, result);
- System.out.println("--------------------------------" + "更新 XML文件成功" + "-------------------------------------");
- } catch (final Exception exception) {
- System.out.println("更新" + fileName + "出错:"+exception);
- }
- System.out.println("============退出生成xml方法:" + new Date().toLocaleString() + "=================");
- }
- }
把xml格式的字符串写入到一个xml文件中的更多相关文章
- (转载)将一段符合XML格式规范字符串插入已有XML文档当中
想我们已经存在一个XML文档,结构如下: < xmlversion="1.0"encoding="utf-8">< employees&g ...
- 使用WCF传输DataTable:DataTable和Xml格式的字符串相互转换(C#)
背景:项目中要用到客户端向服务端传数据,使用WCF,绑定webHttpBinding,做了一个小例子. 业务逻辑简介:客户端在a表中添加了几条数据,从SQL Server数据库直接取出新添加的数据(D ...
- Java中XML格式的字符串4读取方式的简单比较
Java中XML格式的字符串4读取方式的简单比较 1.java自带的DOM解析. import java.io.StringReader; import javax.xml.parsers.Docum ...
- Dom4j解析xml格式的字符串【java】
一般我们会使用dom4j.SAX.w3c来解析xml文件,网上也大多提供此类解决方案. 但在实际项目中,也有会解析xml格式的字符串报文的. 比如,有如下字符串: String = "< ...
- soapui如何发送xml格式的字符串
一个服务需要的xml格式的字符串参数,用soapUI传递参数时要这样写: <![CDATA[<?xml version="1.0" encoding="UTF ...
- 读取xml格式的字符串和上下文中的xml数据
1.读取xml格式的字符串 假设有一段下面的xml格式的字符串: <xml> <return_code><![CDATA[SUCCESS]]></re ...
- java:利用java的输入/输出流将一个文件的每一行+行号复制到一个新文件中去
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- 怎样将多个CSS文件导入一个CSS文件中
问题: 在HTML中引入css的其中的两个方法: 导入式和链接式的目的都是将一个独立的css文件引入一个文件中,二者的区别不大,事实上,二者最大的区别在于链接式使用html的标记引入外部css文 ...
- 在VS中让一个JS文件智能提示另一个JS文件中的成员
“在VS中如何让一个JS文件智能提示另一个JS文件中的成员” 有时候会有这种情况:当我的一个Web页面引用了两个JS文件(假如分别叫common.js和JScript1.js),如果JScript1. ...
随机推荐
- 使用caffe的HDF5数据完毕回归任务
一直在研究怎样用caffe做行人检測问题.然而參考那些经典结构比方faster-rcnn等,都是自己定义的caffe层来完毕的检測任务. 这些都要求对caffe框架有一定程度的了解.近期看到了怎样用c ...
- [Exception Android 19] - android.widget.HeaderViewListAdapter.isEnabled
java.lang.IndexOutOfBoundsException: Invalid index , size at java.util.ArrayList.throwIndexOutOfBoun ...
- bootstrap的两种在input框后面增加一个图标的方式
第一种: <div class="input-group"> <div class="input-icon-group"> <di ...
- hibernate 多对一关联
(转自尚学堂教学内容) 注解多对一: package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.pers ...
- ASP.NET MVC3 系列教程 - 模型
I:基础绑定的实现 1.在前面的两篇基础文章(路由 及 控制器&视图)当中,还没对QueryString的绑定进行介绍,因为我觉得它更适合放在这一章节中去介绍.我们在用WebForm去开发的时 ...
- 让网页自动调用双核浏览器的极速模式(<meta name="renderer" content="webkit">)
背景:最近做了一个网站,传单服务器之后,每次打开都是乱码,不对啊,我在本地测得明明是排版很整齐啊,然后发现,360用的是“兼容模式”打开的,这就尴尬了,用户一打开就是乱码,这用户体验得有多差!用户可不 ...
- 阿里云web播放器
原文地址:https://help.aliyun.com/document_detail/51991.html?spm=5176.doc61109.6.703.ZTCYoi 一.概念说明 1. pla ...
- NYOJ 10 skiing (深搜和动归)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描写叙述 Michael喜欢滑雪百这并不奇怪. 由于滑雪的确非常刺激.但是为了获得速度.滑的区域必须向下倾斜.并且 ...
- python3 JSON对象的学习
import jsonname = { 'alex':[22,'M'], 'tony':[21,'F'],} name_after = json.dumps(name)name_after = nam ...
- Windows下Python添加MySQLdb扩展模块
[更新 2012-09-16] 这里可以下载已经打包好的EXE文件,http://sourceforge.net/projects/mysql-python/(国内需穿越才可访问) DBank备份下载 ...