jom寫檔案實現,jdom讀取XML檔案,解析某一element並改值存入文

來源:互聯網
上載者:User

1.建立一個xml檔案
TestJDOM.java
package com.jdom.write;

import java.io.FileWriter;
import java.io.IOException;

import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class TestJDOM {

public static void main(String[] args) {
Element rootElement = new Element("MyInfo");
Document myDocument = new Document(rootElement);

// Attribute rootAttri = new Attribute("comment","introduce myself");
// rootElement.setAttribute(rootAttri);

// comment屬性名稱,"introduce myself"屬性值,作用等同上面兩句。
rootElement.setAttribute("comment", "introduce myself");
// rootElement.setAttribute(new Attribute("comment","introduce
// myself"));
// Element sexElement = new Element("sex");
// rootElement.addContent(sexElement);

// Element nameElement = new Element("name");
// nameElement.addContent("kingwong");
// rootElement.addContent(nameElement);

for (int i = 0; i < 5; i++) {
// 三種加入內容的不同形式addContent方法可以嵌套。
rootElement.addContent((Content) (new Element("name")
.addContent("kingwong")));
Element guo = new Element("sex");
guo.setAttribute("value", "male");
guo.setAttribute("v", String.valueOf(i));
rootElement.addContent(guo);
rootElement.addContent((Content) (new Element("contract")
.addContent((Content) (new Element("telephone")
.setAttribute("telephone", "87654321")))));
}
// 刪除
// rootElement.removeChild("sex");

XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
try {
xmlOut.output(myDocument, System.out);
// // xmlOut.output(rootElement.getChild("name"),System.out);
// String outString = xmlOut.outputString(myDocument);
FileWriter writer = new FileWriter("src/myFile.xml");
xmlOut.output(myDocument, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
執行結果如下:
<?xml version="1.0" encoding="UTF-8"?>
<MyInfo comment="introduce myself">
<name>kingwong</name>
<sex value="male" v="0" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="1" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="2" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="3" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="4" />
<contract>
<telephone telephone="87654321" />
</contract>
</MyInfo>

讀取有條件並修改xml檔案
TestJDOM2.java
package com.jdom.read;

import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
* @author kingwong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class TestJDOM2 {
public static void main(String[] args) {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build("src/myFile.xml");

Element root = doc.getRootElement();

String str1 = root.getAttributeValue("comment");
System.out.println("Root Element's comment attribute is : " + str1);

String str2 = root.getChild("sex").getAttributeValue("value");
System.out.println("sex Element's value attribute is : " + str2);
List list = root.getChildren("sex");
Iterator it = list.iterator();
while (it.hasNext()) {
Element guo = (Element) it.next();
if ("2".equals(guo.getAttribute("v").getValue()))
guo.getAttribute("value").setValue("郭秀志實驗品");
System.out.println(guo.getAttribute("v").getValue());
}

// root.removeChildren("sex");
// root.getChild("sex").getChildren().setAttribute("value","guo");
// String str3 = root.getChildText("name");
String str3 = root.getChild("name").getText();
System.out.println(list.size() + "name Element's content is :"
+ str3);

// System.out.println("contact Element's telephone subelement
// content is : " + str4 + "/n");
// Element inputElement = root.getChild("contact");
// inputElement.addContent(new
// Element("email").setAttribute("value","wanghua@cyberobject.com"));

// 設定字元集後就可以讀寫漢字
XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()
.setEncoding("gb2312"));
String outStr = xmlOut.outputString(root);
System.out.println(outStr);
// 寫入檔案
FileWriter writer = new FileWriter("src/myFile.xml");
xmlOut.output(doc, writer);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
執行效果如下:
Root Element's comment attribute is : introduce myself
sex Element's value attribute is : male
0
1
2
3
4
5name Element's content is :kingwong
<MyInfo comment="introduce myself">
<name>kingwong</name>
<sex value="male" v="0" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="1" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="郭秀志實驗品" v="2" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="3" />
<contract>
<telephone telephone="87654321" />
</contract>
<name>kingwong</name>
<sex value="male" v="4" />
<contract>
<telephone telephone="87654321" />
</contract>
</MyInfo>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.