1.规范:
2.XML和HTML的差异
XML是对HTML的补充,即对数据进行存储和传输,可自行定义标签,不能通过浏览器显示,区别于HTML。
3.命名规范
]>
//DTD约束
<java1705>
<student id="1001" user="zhangsan">
<name>张三name>
<age>19age>
<sex>男sex>
<height>180height>
student>
<student id="1002" user="lisi">
<name>李四name>
<age>21age>
<sex>男sex>
<height>178height>
student>
<student id="1003" user="wangwu">
<name>王五name>
<age>22age>
<sex>男sex>
<height>182height>
student>
java1705>
W3C认可的规范。适合于小型文件。基于树形结构。非一次性处理,简单。
public class DOM4jParse {
public static void main(String[] args) {
try {
///创建 XML 的DOM4J解析器
SAXReader saxReader = new SAXReader();
//解析XML文件
Document document = saxReader.read(new File("student.xml"));
//得到根节点
Element rootElement = document.getRootElement();
//得到根节点的所有子元素节点
List elements = rootElement.elements();
ArrayList list = new ArrayList<>();
for (int i=0;iget(i);
//获取属性值
String id = element.attributeValue("id");//得到节点内队形属性名的值
String user = element.attributeValue("user");
// Element name = element.element("name");//得到element节点下的name子元素子节点
// String text = name.getText();
String name = element.elementText("name");//得到节点的内容
String age = element.elementText("age");
String sex = element.elementText("sex");
String height = element.elementText("height");
list.add(new Student(id,user,name,age,sex,height));
}
for (int i=0;iget(i);
System.out.println(student.toString());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
public class SAXParse {
public static void main(String[] args) {
try {
//NO.1 创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//NO.2 通过解析工厂,创建解析构造器
SAXParser saxParser = factory.newSAXParser();
//No.3 指定解析的文件,并指定解析规则(DefaultHandler)
MyHandler myHandler = new MyHandler();
saxParser.parse(new File("student.xml"), myHandler);
//解析结束
ArrayList list = myHandler.getList();
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student.toString());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* SAX 解析过程处理器
*/
private static class MyHandler extends DefaultHandler {
ArrayList list = new ArrayList<>();
public ArrayList getList() {
return list;
}
private Student student;
@Override
public void startDocument() throws SAXException {
System.out.println("开始解析内容");
}
/**
* 解析到开始标签
*
* @param uri 命名空间
* @param localName 不支持
* @param qName [返回前缀:]标签名字(如果没有命名空间,返回标签名)
* @param attributes 该标签的所有属性对象
* @throws SAXException
*/
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
// System.out.println("<"+qName);
if ("student".equals(qName)) {
student = new Student();
String id = attributes.getValue("id");
String user = attributes.getValue("user");
student.setId(Integer.parseInt(id));
student.setUser(user);
}
target = qName;
}
//为了在characters方法中取出有效的字符内容,标记开始节点的名字
private String target = null;
/**
* 解析到字符串内容(空格,回车,文字)
*
* @param ch 文档缓存在内存中的内容,长度为8192
* @param start 遇到的文本内容在缓存中的位置,
* @param length 文本内容到下一个元素节点的长度
* @throws SAXException
*/
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String content = new String(ch, start, length);
// System.out.println(content);
switch (target) {
case "name":
student.setName(content);
break;
case "age":
student.setAge(Integer.parseInt(content));
break;
case "sex":
student.setSex(content);
break;
case "height":
student.setHeight(content);
break;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("student".equals(qName)) {
list.add(student);
}
target = null;//为了防止结束节点之后取出的无效字符再次赋值给student,所以重置target
}
@Override
public void endDocument() throws SAXException {
System.out.println("解析结束");
}
}
}
public class JdomParse {
public static void main(String[] args) {
try {
//NO.1 创建jDOM 解析构造器
SAXBuilder saxBuilder = new SAXBuilder();
//NO.2 解析整个XML文档
org.jdom2.Document build = saxBuilder.build(new File("student.xml"));
Element rootElement = build.getRootElement();//得到根节点
List children = rootElement.getChildren();//获取元素节点下的所有子元素节点
ArrayList list = new ArrayList<>();
for(int i=0;iget(i);
String id = element.getAttributeValue("id");
String user = element.getAttributeValue("user");
System.out.println(" "+id+" "+user);
//获取element元素下的name子元素节点
// Element name = element.getChild("name");
// String text = name.getText();//取出元素内容
String name = element.getChildText("name");
String age = element.getChildText("age");
String sex = element.getChildText("sex");
String height = element.getChildText("height");
list.add(new Student(id,user,name,age,sex,height));
}
for (int i=0;iget(i);
System.out.println(student.toString());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.特点
2.代码
public class DOM4jParse {
public static void main(String[] args) {
try {
///创建 XML 的DOM4J解析器
SAXReader saxReader = new SAXReader();
//解析XML文件
Document document = saxReader.read(new File("student.xml"));
//得到根节点
Element rootElement = document.getRootElement();
//得到根节点的所有子元素节点
List elements = rootElement.elements();
ArrayList list = new ArrayList<>();
for (int i=0;iget(i);
//获取属性值
String id = element.attributeValue("id");//得到节点内队形属性名的值
String user = element.attributeValue("user");
// Element name = element.element("name");//得到element节点下的name子元素子节点
// String text = name.getText();
String name = element.elementText("name");//得到节点的内容
String age = element.elementText("age");
String sex = element.elementText("sex");
String height = element.elementText("height");
list.add(new Student(id,user,name,age,sex,height));
}
for (int i=0;iget(i);
System.out.println(student.toString());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}