xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
<person-list>
<!-- 只是一个注释 -->
<person id="110">
<name>杨磊</name>
<age>24</age>
<email>6666666@qq.com</email>
<address>深圳南山</address>
</person>
<person id="220">
<name>李四</name>
<age>20</age>
<email>12345678@qq.com</email>
<address>广州番禺</address>
</person>
</person-list>
SAXReader读取标签元素,节点,属性的基本操作
public static void readXml() {
//创建解析器
SAXReader reader = new SAXReader();
try {
//加载文件读到Document中
Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
//获取根元素的信息
Element element = document.getRootElement();
//打印根元素名称
System.out.println(element.getName());
//获取子元素
List<Element> eleList = element.elements();
for (Element element2 : eleList) {
//打印子元素名称
System.out.println(element2.getName());
//获取元素属性
Attribute attribute = element2.attribute(0);
//获取元素属性名和属性值
System.out.println(attribute.getName() + " " + attribute.getValue());
//获取下一级子元素
List<Element> eleList2 = element2.elements();
for (Element element3 : eleList2) {
//获取下一级子元素的名称和值
System.out.println(element3.getName() + ": " + element3.getText());
}
}
//根据元素名称获取根元素下的子元素(下一级子元素,没有找到会报空指针异常)
Element ele = element.element("person");
System.out.println(ele.getName());
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void readXml() {
//创建解析器
SAXReader reader = new SAXReader();
try {
//加载文件读到Document中
Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
//获取根元素的信息
Element element = document.getRootElement();
//打印根元素名称
System.out.println(element.getName());
//获取根元素的直接子节点
Iterator<Node> nodeIterator = element.nodeIterator();
while (nodeIterator.hasNext()) {
Node node = nodeIterator.next();
if (node instanceof Comment) {//是否为注释
System.out.println("注释内容:" + node.getText());
}else if (node instanceof Element) {//是否为元素
System.out.println("标签名:" + node.getName());
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void readXml() {
//创建解析器
SAXReader reader = new SAXReader();
try {
//加载文件读到Document中
Document document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
//获取根元素的信息
Element element = document.getRootElement();
//打印根元素名称
System.out.println(element.getName());
//获取所有的直接子元素
List<Element> childs = element.elements();
//遍历直接子元素
for (Element element2 : childs) {
//获取属性集合迭代器
Iterator<Attribute> attrIterator = element2.attributeIterator();
while (attrIterator.hasNext()) {
Attribute attribute = attrIterator.next();
System.out.println(attribute.getName() + " " + attribute.getValue());
}
}
//获取根元素的person子元素的第一个属性
System.out.println(element.element("person").attribute(0).getName());
//获取根元素的person子元素的第一个属性值
System.out.println(element.element("person").attribute(0).getValue());
//获取根元素的person子元素的属性个数
System.out.println(element.element("person").attributeCount());
//按名称获取根元素的person子元素的属性值
System.out.println(element.element("person").attributeValue("id"));
} catch (DocumentException e) {
e.printStackTrace();
}
}
通过递归获取所有节点的标签名字
public static void main(String[] args) {
//创建解析器
SAXReader reader = new SAXReader();
Document document;
try {
//加载文件读到Document中
document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
//获取根元素信息
Element element = document.getRootElement();
readXml(element);
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void readXml(Element element) {
System.out.println(element.getName());
List<Element> eleList = element.elements();
for (Element element2 : eleList) {
readXml(element2);
}
}
把xml文件的所有标签名名字、属性、标签体、注释、空白处 等...的内容全部打印出来,格式要求与xml文件的内容一致。
public static void main(String[] args) {
//创建解析器
SAXReader reader = new SAXReader();
Document document;
try {
//加载文件读到Document中
document = reader.read(new File("./src/com/szjoint/eir/task/testXml.xml"));
//获取根元素信息
Element element = document.getRootElement();
StringBuilder builder = new StringBuilder();
readXml(element, builder);
System.out.println(builder);
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void readXml(Element element, StringBuilder builder) {
builder.append("<" + element.getName());
//获取到属性迭代器,添加所有属性
Iterator<Attribute> attrIterator = element.attributeIterator();
while (attrIterator.hasNext()) {
Attribute attribute = attrIterator.next();
builder.append(" " + attribute.getName() + "=\"" + attribute.getValue() + "\"");
}
//添加结束标签的符号
builder.append(">");
//获取所有的子节点
Iterator<Node> nodeIterator = element.nodeIterator();
while (nodeIterator.hasNext()) {
Node node = nodeIterator.next();
if (node instanceof Text) {//文本
builder.append(node.getText());
}else if (node instanceof Comment) {
builder.append("<!--" + node.getText() + "-->");
}else if (node instanceof Element) {
//迭代读下一个元素
readXml((Element)node, builder);
}
}
//添加结束标签
builder.append("</" + element.getName() + ">");
}
dom4j生成xml文件
public static void main(String[] args) {
File file = new File("E:\\test.xml");
createXml(file);
}
public static void createXml(File file) {
try {
//创建document
Document document = DocumentHelper.createDocument();
//创建根节点
Element element = document.addElement("root");
//在根节点下添加第一个子节点
Element firstElement = element.addElement("person").addAttribute("attr1", "value1");
firstElement.addElement("people")
.addAttribute("attr", "first child")
.addText("person one child one");
firstElement.addElement("people")
.addAttribute("attr", "second child")
.addText("person one child two");
//在根节点下添加第二个子节点
Element secondElement = element.addElement("person").addAttribute("attr2", "value2");
secondElement.addElement("people")
.addAttribute("attr", "second child")
.addText("person two child one");
secondElement.addElement("people")
.addAttribute("attr", "second child")
.addText("person two child two");
OutputFormat format = OutputFormat.createPrettyPrint();
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
writer.write(document);
writer.close();
System.out.println("create successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<person attr1="value1">
<people attr="first child">person one child one</people>
<people attr="second child">person one child two</people>
</person>
<person attr2="value2">
<people attr="second child">person two child one</people>
<people attr="second child">person two child two</people>
</person>
</root>