导航:首页 > 编程语言 > javaxml写入

javaxml写入

发布时间:2022-07-29 22:17:03

java怎么读写xml文件

public List<Student> doReadXML(String path) throws Exception {
List<Student> empvoList = new ArrayList<Student>();

File file = new File(path);
//输入流对象
FileInputStream fis = new FileInputStream(file);
//jdom解析器
SAXBuilder sb = new SAXBuilder();
Document doc= sb.build(fis);
//获得XML的根元素
Element root = doc.getRootElement();
//获得根元素下的所有子元素
List<Element> employees = root.getChildren();
for(int i=0;i<employees.size();i++){
Element employee =employees.get(i);
Student stu= new Student();
String name = employee.getChildText("name");
String sex = employee.getChildText("sex");
String agetemp = employee.getChildText("age");
String home = employee.getChildText("home");
String email = employee.getChildText("email");

stu.setName(name);
stu.setSex(sex);
int age = 0;
if(agetemp.equals("")){
age = 0;
} else {
age = Integer.parseInt(agetemp);
}
stu.setAge(age);
stu.setHome(home);
stu.setEmail(email);
System.out.println(name+"\t"+i);
empvoList.add(stu);
}
return empvoList;
}

⑵ java xml如何写

这个是要求命令行程序吧
首先是选择一个xml库,然后是实现基本的集合查询,最后是实现输入输出

⑶ java如何向xml文件写入内容

解析(也就是读取)的话,可以采用SAX解析,或者DOM4J (dom for java)解析,写入的话,可以采用dom写入,SAX和DOM4J都是Apache的开源项目,你可以从官网上搜到相关的Jar包,导入的Eclipse即可使用,至于代码示例的话,网络谷歌一下,栗子很多,希望能帮到你。

⑷ JAVA如何写XML文件

import java.io.*;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class DOM4JTest {
public static void main(String[] args) {
Document doc = DocumentHelper.createDocument();
doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");
Element root = doc.addElement("students");

Element eltStu1 = root.addElement("student").addAttribute("sn", "01");
Element eltName1 = eltStu1.addElement("name");
Element eltAge1 = eltStu1.addElement("age");
eltName1.setText("张三");
eltAge1.setText("20");

Element eltStu2 = root.addElement("student").addAttribute("sn", "02");
Element eltName2 = eltStu2.addElement("name");
Element eltAge2 = eltStu2.addElement("age");
eltName2.setText("李四");
eltAge2.setText("18");

try {
OutputFormat format = new OutputFormat("
", true);
format.setEncoding("gb2312");
// 可以把System.out改为你要的流。
XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);
xmlWriter.write(doc);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

⑸ java如何将一段xml的字符串文件写入到xml文件 然后再解析出来

用dom4j,获取指定的字节 List rootList = root.selectNodes("RETURN_CD"); rootList.get(0);
获取的就是成功了哦

⑹ JAVA如何写XML文件

呵呵,你这个问题,其实写XML很简单的,氛围以下几步:
1.导入DOM4J包,在此我使用dom4j来做。
2.创建dom4j的实例:Document document = DocumentHelper.createDocument();
3.一级一级的添加节点或者属性,这个具体参照dom4j帮助:
Element rootGen = document.addElement("root");
4.定义以下3个对象,然后进行操作:
Writer writer = null;
OutputFormat format = null;
XMLWriter xmlwriter = null;

***********************************************************************************
附上代码:
public void CreateXMl(StudentBean sn){
//创建document对象
Document document = DocumentHelper.createDocument();
//定义根节点Element
Element rootGen = document.addElement("root");
//定义根节点ROOT的子节点们
Element nameGen = rootGen.addElement("Name");
nameGen.addAttribute("name", "我是中文");
Element ageGen = rootGen.addElement("Age");
Element addrGen = rootGen.addElement("Address");
Writer writer = null;
OutputFormat format = null;
XMLWriter xmlwriter = null;
//将定义好的内容写入xml文件中
try {
//使用这个writer也可以,只不过遇到中文会乱码哦
// writer = new FileWriter("d:/test.xml");
//进行格式化
format = OutputFormat.createPrettyPrint();
//设定编码
format.setEncoding("UTF-8");
xmlwriter = new XMLWriter(new FileOutputStream("d:/test.xml"), format);
xmlwriter.write(document);
xmlwriter.flush();
xmlwriter.close();
System.out.println("-----------Xmlfile successfully created-------------");
} catch (Exception e) {
e.printStackTrace();
System.out.println("-----------Exception occured ring of create xmlfile -------");
}
}

⑺ java中如何写xml

您好,提问者:
String xml = “<?xml version=\"1.0\" encoding=\"GBK\"?><父节点><子节点></子节点></父节点>”;
一般是接口传输数据用的。

⑻ 如何用Java实现对xml文件的读取和写入以及保存

直接附源码import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;import org.dom4j.*;
import org.dom4j.io.XMLWriter;
public class Dom4jSample { public static void main(String[] args) {
Dom4jSample dom4jSample = new Dom4jSample();
Document document = dom4jSample.createDocument();
try{
dom4jSample.FileWrite(document);

Document documentStr = dom4jSample.StringToXML("<China>I Love!</China>");
dom4jSample.XMLWrite(documentStr);

Element legend = dom4jSample.FindElement(document);
System.out.println(legend.getText());
}
catch(Exception e)
{

}
}

/*
* Create a XML Document
*/
public Document createDocument()
{
Document document = DocumentHelper.createDocument();

Element root = document.addElement("root");

Element author1 = root.addElement("Lynch");
author1.addAttribute("Age","25");
author1.addAttribute("Country","China");
author1.addText("I am great!");

Element author2 = root.addElement("Legend");
author2.addAttribute("Age","25");
author2.addAttribute("Country","China");
author2.addText("I am great!too!");

return document;
}

/*
* Create a XML document through String
*/
public Document StringToXML(String str) throws DocumentException
{
Document document = DocumentHelper.parseText(str);
return document;
}
public Element FindElement(Document document)
{
Element root = document.getRootElement();
Element legend = null;
for(Iterator i=root.elementIterator("legend");i.hasNext();)
{
legend = (Element)i.next();
}
return legend;
}

/*
* Write a XML file
*/
public void FileWrite(Document document) throws IOException
{
FileWriter out = new FileWriter("C:/Dom2jSample.xml");
document.write(out);
out.close();
}

/*
* Write a XML format file
*/
public void XMLWrite(Document document) throws IOException
{
XMLWriter writer = new XMLWriter(new FileWriter("C:/Dom2jSampleStr.xml"));
writer.write(document);
writer.close();
}
}

阅读全文

与javaxml写入相关的资料

热点内容
curlphp爬虫 浏览:872
python按日期循环 浏览:108
php三个等号 浏览:758
培训班出来的程序员解决问题很差 浏览:961
程序员那么可爱25集 浏览:753
服务器地址和ip地址一样不 浏览:664
php中括号定义数组 浏览:602
php打印堆栈 浏览:516
华为adb命令行刷机 浏览:965
人像摄影pdf 浏览:761
解压文件密码怎样重新设置手机 浏览:1001
高考指南pdf 浏览:695
爬虫python数据存储 浏览:240
u盘怎么取消加密 浏览:431
567除以98的简便算法 浏览:342
pdf手机如何解压 浏览:21
python描述器 浏览:60
战地联盟3解压密码 浏览:805
s型命令 浏览:25
php年薪5年 浏览:71