導航:首頁 > 編程語言 > 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寫入相關的資料

熱點內容
如何上網上設個人加密賬戶 瀏覽:44
linux打開ssh服務 瀏覽:78
微信位置可以加密嗎 瀏覽:470
演算法蠻力法 瀏覽:438
隨機排練命令 瀏覽:147
python多進程並發 瀏覽:41
安卓軟體安裝如何躲避安全檢測 瀏覽:647
奇幻潮翡翠台源碼百度雲盤 瀏覽:187
什麼軟體可以免費pdf轉word 瀏覽:15
php正則表達式大全 瀏覽:394
androidntp時間 瀏覽:299
輪機長命令簿英文 瀏覽:148
oppo鈴聲設置被加密怎麼處理 瀏覽:548
粵苗app圖形驗證碼怎麼填 瀏覽:899
管家婆架設雲伺服器 瀏覽:254
php的登錄界面代碼 瀏覽:997
php開發客戶端 瀏覽:998
theisle測試服怎麼搜伺服器 瀏覽:447
廣播PDF 瀏覽:218
單片機編程300例匯編百度 瀏覽:35