//获取所有的子元素,不包括回车
package com.dom4jDemo;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class Dom4jDemo1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "src/scores.xml";
File file = new File(path);
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(file);
Element root = doc.getRootElement();
//获取所有的子元素,不包括回车
List<Element> eles = root.elements();
for(Element ele : eles){
System.out.println(ele.getName());
List<Element> children = ele.elements();
for(Element e : children){
System.out.println(e.getName() + "\t" + e.getText());
}
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DOM4j
``package com.dom4jDemo;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class Dom4jDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "src/scores.xml";
File file = new File(path);
SAXReader saxReader = new SAXReader();
try {
Document doc = saxReader.read(file);
//获取根元素
Element root = doc.getRootElement();
//获取一个指定名字的子元素
Element eleStu = root.element("student");
//复制节点
Element newStu = (Element) eleStu.clone();
//获取子元素并设置值
newStu.element("id").setText("5");
newStu.element("name").setText("孙七");
newStu.element("lesson").setText("hibernate");
newStu.element("score").setText("92");
root.add(newStu);
//删除节点
root.remove(eleStu);
//设置输出编码
OutputFormat of = OutputFormat.createCompactFormat();
of.setEncoding("GBK");
//构建输出流对象
XMLWriter xw
= new XMLWriter(new FileWriter(file),of);
xw.write(doc);
xw.close();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.dom;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
//文件路径
String path = "src/scores.xml";
//创建解析器工厂
DocumentBuilderFactory dbf
= DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {//使用解析器工厂创建解析器
db = dbf.newDocumentBuilder();
//将XML文件映射成文档对象
Document doc = db.parse(path);
//根元素
Element root = doc.getDocumentElement();
System.out.println(root.getNodeName());
//获取所有的子元素,包含换行和标签元素
NodeList children = //root.getChildNodes();
//获取标签名为student的元素集合,不包括换行
root.getElementsByTagName("student");
for(int i = 0; i < children.getLength();i++){
Node node = children.item(i);
//跳过换行
// if(node.getNodeType() == Node.TEXT_NODE){
// continue;
// }
System.out.println(node.getNodeName() + “\t” + node.getNodeValue());
Element ele = (Element)node;
//获取子元素的子元素
NodeList childNodes = ele.getChildNodes();
for(int j = 0;j < childNodes.getLength();j++){
Node n = childNodes.item(j);
if(n.getNodeType() == Node.TEXT_NODE){
continue;
}
System.out.println(n.getNodeName() + “\t” + n.getTextContent());
}
System.out.println(childNodes.getLength());
}
System.out.println(“子元素的数量:” + children.getLength());
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.dom;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
public class DomDemo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "src/scores.xml";
DocumentBuilderFactory dbf
= DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
Document doc = db.parse(path);
Element root = doc.getDocumentElement();
//第一种添加元素的办法
// Element eleStu = doc.createElement(“student”);
// Element eleId = doc.createElement(“id”);
// eleId.setTextContent(“3”);//设置文本值
// Element eleName = doc.createElement(“name”);
// eleName.setTextContent(“王五”);
// Element eleLesson = doc.createElement(“lesson”);
// eleLesson.setTextContent(“oracle”);
// Element eleScore = doc.createElement(“score”);
// eleScore.setTextContent(“78”);
// eleStu.appendChild(eleId);
// eleStu.appendChild(eleName);
// eleStu.appendChild(eleLesson);
// eleStu.appendChild(eleScore);
// root.appendChild(eleStu);//添加元素至根元素
//第二种添加元素的办法
//获取要添加的元素的模板,获取一个现有元素
Element eleStu = (Element) root.getElementsByTagName(“student”).item(0);
//复制现有元素创造一个新元素,包含换行
Element newEle = (Element) eleStu.cloneNode(true);
Node nodeId = newEle.getElementsByTagName(“id”).item(0);
nodeId.setTextContent(“4”);
Node nodeName = newEle.getElementsByTagName(“name”).item(0);
nodeName.setTextContent(“赵六”);
Node nodeLesson = newEle.getElementsByTagName(“lesson”).item(0);
nodeLesson.setTextContent(“JQuery”);
Node nodeScore = newEle.getElementsByTagName(“score”).item(0);
nodeScore.setTextContent(“88”);
root.appendChild(newEle);//添加元素至根元素
//保存文件
TransformerFactory tff =
TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
//设置要保存哪些内容到XML文件,默认是整个
DOMSource ds = new DOMSource(doc);
//设置保存编码
tf.setOutputProperty(OutputKeys.ENCODING, “UTF-8”);
StreamResult sr = new StreamResult(new FileOutputStream(path));
tf.transform(ds, sr);
System.out.println(“保存完成”);
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
“`