XML读写实例
来源:互联网 发布:通裕重工历史交易数据 编辑:程序博客网 时间:2024/06/04 19:06
一个读写XML操作的例子:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class testXml {
public static void main(String[] args) {
try {
testXml test = new testXml();
test.testWrite();
test.testRead();
test.testWrite();
test.testRead();
} catch (Exception e) {
e.printStackTrace();
}
e.printStackTrace();
}
}
public void testRead() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(new FileInputStream("src/output.xml"));
Document document = reader.read(new FileInputStream("src/output.xml"));
Element root = document.getRootElement();
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
System.out.println(element.getText());
System.out.println(element.attributeValue("name"));
System.out.println(element.attributeValue("blog"));
}
Element element = (Element) iter.next();
System.out.println(element.getText());
System.out.println(element.attributeValue("name"));
System.out.println(element.attributeValue("blog"));
}
}
public void testWrite() throws Exception{
Document document = DocumentHelper.createDocument();
Element root = document.addElement("root");
for(int i=0;i<10;i++){
Element root = document.addElement("root");
for(int i=0;i<10;i++){
Element element1 = root.addElement("user")
.addAttribute("name","Alex"+i)
.addAttribute("id", "id"+i)
.addText("我是信息");
}
.addAttribute("name","Alex"+i)
.addAttribute("id", "id"+i)
.addText("我是信息");
}
XMLWriter writer = new XMLWriter(new FileOutputStream("src/output.xml"));
writer.write(document);
writer.close();
writer.close();
}
}
(1)
读
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File(filename));
写
XMLWriter output = null;
/** 格式化输出,类型IE浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码 */
format.setEncoding("GBK");
output = new XMLWriter(new FileWriter(new File(filename)),format);
output.write(document);
output.close();
(2)
Document document=DocumentHelper.parseText(responseBody);
XMLWriter output = null;
/** 格式化输出,类型IE浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();
/** 指定XML字符集编码 */
format.setEncoding("GBK");
output = new XMLWriter(new FileWriter(new File(filename)),format);
output.write(document);
output.close();
(2)
Document document=DocumentHelper.parseText(responseBody);
DocumentHelper.parseText(document.asXML()));
doc.asXML()
(3)
return document.selectSingleNode(//xx/yy).getStringValue();
return document.selectSingleNode(//xx/yy).getStringValue();
- struts读写xml实例
- XML读写实例
- xml读写文件实例
- Java读写XML实例
- xml文件读写实例
- C#读写xml文件实例
- C#读写xml文件实例
- dom4j读写xml文档实例
- JDOM读写XML文件实例
- c#读写xml文件(实例)
- dom4j读写XML文件实例
- c# xml读写 操作 实例
- C#读写XML简单实例
- Qt读写xml文件实例
- java读写XML文件实例
- 使用dom4j读写xml实例
- JAXB方式读写xml实例
- struts开发实践—读写xml实例
- USACO:frac1
- Windows平台cx_Oracle各种连接不上Oracle服务端的问题总结
- Linux 设备驱动 Edition 3(书摘)
- OSI
- 工作笔记
- XML读写实例
- ASC Java代码编写注意(Every Day Mind)
- Java随机生成数字的方法
- Details on individual partitions
- Android Pmem
- birt导出excel支持图片的实现
- 超级实用且不花哨的js代码大全
- 開機當黑屏 79 root cause 分析
- 解决javascript跨域问题