xml文件解析-JDOM
2007-06-06 17:01
411 查看
1.构造解析器
方法1:
import org.jdom.*;
import org.jdom.input.*;
SAXBuilder saxBuilder = new SAXBuilder();
File file = new File("src/c03/students.xml");
Document doc = saxBuilder.build(file);
方法2:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("src/c03/students.xml");
org.w3c.dom.Document doc = db.parse(file);
DOMBuilder domBuilder = new DOMBuilder ();
org.jdom.Document doc = domBuilder.build(doc);
2.jdom函数
(1)创建xml文档
Document doc = new Document();
Element eltRoot = new Element("student");
doc.setRootElement(eltRoot);
(2)添加元素和属性
Element eltStu = new Element("student");
Element eltName = new Element("name");
Element eltAge = new Element("age");
eltName.setText("王五");
eltAge.setText("19");
eltStu.addContent(eltName);
eltStu.addContent(eltAge);
eltStu.setAttribute("sn", "03");
(3)访问元素
访问根元素: Element root = doc.getRootElement();
得到某元素下所有子元素:LIst children = element.getChildren();
得到指定元素下所有子元素:LIst children = element.getChildren("student");
得到指定元素的第一个子元素:Element child = element.getChild("student");
返回指定子节点的内容文本值: getChildText("childname")
返回该元素的内容文本值:getText()
取得元素的子元素(为最低层元素)的值:String name=book.getChildTextTrim("name");
(4)访问属性
得到指定元素下所有属性:List attrList = element.getAttributes();
得到指定属性:Attribute attr = element.getAttribute("sn");
得到指定属性的值:String attrValue = element.getAttributeValue("sn");
(5)删除元素和属性
删除指定元素下所有子元素:element.removeChildren("student");
删除指定元素下第一个子元素:element.removeChild("student");
删除指定属性:element.removeAttribute("sn");
(6)输出文档
XMLOutputter xmlOut = new XMLOutputter();
Format fmt = Format.getPrettyFormat();
fmt.setEncoding("GB2312");
fmt.setIndent(" ");
xmlOut.setFormat(fmt);
xmlOut.output(doc, new FileOutputStream("src/c03/students.xml"));
或者输出到xmlOut.output(doc, System.out);
3.JDOM实例
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class JDOMConvert {
/**
* @param args
*/
public static void main(String[] args) {
SAXBuilder saxBuilder = new SAXBuilder();
File file = new File("src/c03/students.xml");
try {
Document doc = saxBuilder.build(file);
// add element
Element eltStu = new Element("student");
Element eltName = new Element("name");
Element eltAge = new Element("age");
eltName.setText("王五");
eltAge.setText("19");
eltStu.addContent(eltName);
eltStu.addContent(eltAge);
eltStu.setAttribute("sn", "03");
Element root = doc.getRootElement();
root.addContent(eltStu);
// remove element
root.removeChild("student");
// modify element
root.getChild("student").getChild("age").setText("23");
// output to screen
XMLOutputter xmlOut = new XMLOutputter();
Format fmt = Format.getPrettyFormat();
fmt.setEncoding("GB2312");
fmt.setIndent(" ");
xmlOut.setFormat(fmt);
xmlOut.output(doc, new FileOutputStream("src/c03/students.xml"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
catch (JDOMException e) {
System.out.println(e.getMessage());
}
}
}
方法1:
import org.jdom.*;
import org.jdom.input.*;
SAXBuilder saxBuilder = new SAXBuilder();
File file = new File("src/c03/students.xml");
Document doc = saxBuilder.build(file);
方法2:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("src/c03/students.xml");
org.w3c.dom.Document doc = db.parse(file);
DOMBuilder domBuilder = new DOMBuilder ();
org.jdom.Document doc = domBuilder.build(doc);
2.jdom函数
(1)创建xml文档
Document doc = new Document();
Element eltRoot = new Element("student");
doc.setRootElement(eltRoot);
(2)添加元素和属性
Element eltStu = new Element("student");
Element eltName = new Element("name");
Element eltAge = new Element("age");
eltName.setText("王五");
eltAge.setText("19");
eltStu.addContent(eltName);
eltStu.addContent(eltAge);
eltStu.setAttribute("sn", "03");
(3)访问元素
访问根元素: Element root = doc.getRootElement();
得到某元素下所有子元素:LIst children = element.getChildren();
得到指定元素下所有子元素:LIst children = element.getChildren("student");
得到指定元素的第一个子元素:Element child = element.getChild("student");
返回指定子节点的内容文本值: getChildText("childname")
返回该元素的内容文本值:getText()
取得元素的子元素(为最低层元素)的值:String name=book.getChildTextTrim("name");
(4)访问属性
得到指定元素下所有属性:List attrList = element.getAttributes();
得到指定属性:Attribute attr = element.getAttribute("sn");
得到指定属性的值:String attrValue = element.getAttributeValue("sn");
(5)删除元素和属性
删除指定元素下所有子元素:element.removeChildren("student");
删除指定元素下第一个子元素:element.removeChild("student");
删除指定属性:element.removeAttribute("sn");
(6)输出文档
XMLOutputter xmlOut = new XMLOutputter();
Format fmt = Format.getPrettyFormat();
fmt.setEncoding("GB2312");
fmt.setIndent(" ");
xmlOut.setFormat(fmt);
xmlOut.output(doc, new FileOutputStream("src/c03/students.xml"));
或者输出到xmlOut.output(doc, System.out);
3.JDOM实例
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class JDOMConvert {
/**
* @param args
*/
public static void main(String[] args) {
SAXBuilder saxBuilder = new SAXBuilder();
File file = new File("src/c03/students.xml");
try {
Document doc = saxBuilder.build(file);
// add element
Element eltStu = new Element("student");
Element eltName = new Element("name");
Element eltAge = new Element("age");
eltName.setText("王五");
eltAge.setText("19");
eltStu.addContent(eltName);
eltStu.addContent(eltAge);
eltStu.setAttribute("sn", "03");
Element root = doc.getRootElement();
root.addContent(eltStu);
// remove element
root.removeChild("student");
// modify element
root.getChild("student").getChild("age").setText("23");
// output to screen
XMLOutputter xmlOut = new XMLOutputter();
Format fmt = Format.getPrettyFormat();
fmt.setEncoding("GB2312");
fmt.setIndent(" ");
xmlOut.setFormat(fmt);
xmlOut.output(doc, new FileOutputStream("src/c03/students.xml"));
}
catch (IOException e) {
System.out.println(e.getMessage());
}
catch (JDOMException e) {
System.out.println(e.getMessage());
}
}
}
相关文章推荐
- 用JDOM解析xml文件
- JDom解析xml文件
- Jdom和Dom4J解析xml文件
- 用DOM/JDOM解析XML文件
- Jdom解析多层xml文件
- 使用jdom解析xml文件和xml格式的字符串
- Java&Xml教程(六)使用JDOM解析XML文件
- XML_Java_ XML_利用Jdom(Java)解析xml文件 示例
- Java&Xml教程(六)使用JDOM解析XML文件
- jdom (dom4j)解析xml出错、校验dtd文件不生效
- 应用JDOM解析XML文件
- JDOM解析xml文件
- jdom (dom4j)解析xml出错、校验dtd文件不生效
- 用 jdom 解析 xml 文件时如何解决中文问题?如何解析?
- 解析XML文件——JDOM基本操作
- 用DOM/JDOM解析XML文件
- 完整版jdom解析xml文件
- jdom解析xml文件
- 使用JDOM解析xml文件