JAVA解析xml
java中有四种解析方法 dom、sax、dom4j、jdom
jdom 解析
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import java.io.*;
import java.util.List;
public class Test {
// <dependency>
// <groupId>org.jdom</groupId>
// <artifactId>jdom</artifactId>
// <version>1.1</version>
// </dependency>
public static void createXml() {
//创建一个xml
Document doc = new Document();
//创建一个节点
Element root = new Element("data");
// 设置根节点
doc.setRootElement(root);
//设置属性
root.setAttribute("flag", "true");
root.setAttribute("tipmsg", "12313");
//创建一个节点
Element initOptionsElement = new Element("initOptions");
Element optionElement = new Element("Control");
optionElement.setAttribute("id", "213123");
optionElement.setText("213123");
initOptionsElement.addContent(optionElement);
root.addContent(initOptionsElement);
XMLOutputter xmlOutConsole = new XMLOutputter();
Format fmtConsole = Format.getPrettyFormat();
fmtConsole.setIndent(" ");// 首航缩进为4个空格
fmtConsole.setEncoding("UTF-8");
// 10.设置格式与输出相关
xmlOutConsole.setFormat(fmtConsole);
System.out.println(xmlOutConsole.outputString(doc));
}
public static void main(String[] args) throws JDOMException, IOException {
createXml();
// resloveXML();
}
private static void resloveXML() throws JDOMException, IOException {
//1.创建SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
//2.创建输入流
//InputStream is = new FileInputStream(new File("src/main/resources/demo.xml"));
String resourse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <data flag=\"true\" tipmsg=\"12313\"><initOptions> <Control id=\"213123\">213123</Control> </initOptions> </data>";
ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(resourse.getBytes());
//3.将输入流加载到build中
Document document = saxBuilder.build(tInputStringStream);
//4.获取根节点
Element rootElement = document.getRootElement();
//5.获取子节点
List<Element> children = rootElement.getChildren();
for (Element child : children) {
System.out.println("通过rollno获取属性值:"+child.getAttribute("rollno"));
List<Attribute> attributes = child.getAttributes();
//打印属性
for (Attribute attr : attributes) {
System.out.println(attr.getName()+":"+attr.getValue());
}
List<Element> childrenList = child.getChildren();
System.out.println("======获取子节点-start======");
for (Element o : childrenList) {
System.out.println("节点名:"+o.getName()+"---"+"节点值:"+o.getValue());
}
System.out.println("======获取子节点-end======");
}
}
}
dom4j解析
package com.cxx.xml;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.Iterator;
import java.util.List;
/**
* @Author: cxx
* Dom4j解析xml
* @Date: 2018/5/30 12:21
*/
public class Dom4JDemo {
public static void main(String[] args) throws Exception {
//1.创建Reader对象
SAXReader reader = new SAXReader();
//2.加载xml
Document document = reader.read(new File("src/main/resources/demo.xml"));
//3.获取根节点
Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator();
while (iterator.hasNext()){
Element stu = (Element) iterator.next();
List<Attribute> attributes = stu.attributes();
System.out.println("======获取属性值======");
for (Attribute attribute : attributes) {
System.out.println(attribute.getValue());
}
System.out.println("======遍历子节点======");
Iterator iterator1 = stu.elementIterator();
while (iterator1.hasNext()){
Element stuChild = (Element) iterator1.next();
System.out.println("节点名:"+stuChild.getName()+"---节点值:"+stuChild.getStringValue());
}
}
}
}