XML学习(个人笔记)
程序员文章站
2024-01-02 14:21:46
...
把视频中的PPT复制上来,以便以后复习(在最后面)
具体的语法就不讲了,
下面是XML的一些操作(增删改查)
解析他们可能用到的jar包如下:
https://download.csdn.net/download/qq_39627843/11199852
一、进行doc解析
假设我们的XML文档长这样
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<书架>
<书>
<书名>好书</书名>
<作者>我</作者>
<售价>999</售价>
<时间>2010</时间>
</书>
<书>
<书名>Java</书名>
<作者>小明</作者>
<售价>888</售价>
</书>
</书架>
那么下面给出代码
package mypackage;
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.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
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;
//使用dom方式,对xml进行crud( create、retrieve、update、delete)
public class Function {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
@Test
public void read()
{
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/mypackage/first.xml");
//最简单的获取内容
NodeList list = doc.getElementsByTagName("书名");
Node node = list.item(0);
String content = node.getTextContent();
System.out.println("xml content: "+content);
//递归获取内容(递归遍历)
System.out.println("递归遍历");
Node first = doc.getFirstChild();
dfs(first); //调用递归方法
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void dfs( Node node )
{
String content = node.getNodeName();
System.out.println(" xml content: "+content);
NodeList list = node.getChildNodes();
for( int i = 0 ; i < list.getLength() ; i++ )
{
Node n = list.item(i);
dfs(n);
}
}
@Test
//得到xml中标签的属性值:<书名 name="xxxx">Java</书名>
public void read2()
{
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("src/mypackage/first.xml");
//此处使用Element更好
Element element = (Element) document.getElementsByTagName("书名").item(0);
String value = element.getAttribute("name");
System.out.println(" 属性值:" + value);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//对xml文档添加
@Test
public void add()
{
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/mypackage/first.xml");
//得到第一个书的那个element
Element element = (Element) doc.getElementsByTagName("书").item(0);
//创建新的element
Element child = doc.createElement("折扣");
child.setTextContent("9折");
Element child2 = doc.createElement("时间");
child2.setTextContent("2010");
//①普通添加(尾随其后添加)
//element.appendChild(child);
/*②
下面是按指定位置添加 假设我们想在第一本书后面加上时间这个节点,也就是child2
*/
Node n = doc.getElementsByTagName("折扣").item(0); //n是插入位置后面的一个节点
Node book = doc.getElementsByTagName("书").item(0); //得到要挂孩子的节点
book.insertBefore(child2, n);
/*
*②指定位置添加结束
*/
/*
* ③添加属性attribute
* 我们想把书名加一个属性 即变为,<书名 书名的属性="attri">好书</书名>
*/
Element e3 = (Element) doc.getElementsByTagName("书名").item(0);
e3.setAttribute("书名的属性", "attri");
/*
* ③结束
*/
//把更新的东西写回xml文档
TransformerFactory transfactory = TransformerFactory.newInstance();
Transformer transformer = transfactory.newTransformer();
//这里传的两个参数都是接口(Source,Result),要用具体的
transformer.transform(new DOMSource(doc), new StreamResult( new FileOutputStream("src/mypackage/first.xml")));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void delete()
{
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/mypackage/first.xml");
//father节点
Element father = (Element) doc.getElementsByTagName("书").item(0);
Element child = (Element) doc.getElementsByTagName("时间").item(0);
father.removeChild(child);
//把更新的东西写回xml文档
TransformerFactory transfactory = TransformerFactory.newInstance();
Transformer transformer = transfactory.newTransformer();
//这里传的两个参数都是接口(Source,Result),要用具体的
transformer.transform(new DOMSource(doc), new StreamResult( new FileOutputStream("src/mypackage/first.xml")));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//更新xml
@Test
public void update()
{
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("src/mypackage/first.xml");
//假如我们想改书的售价
Element e = (Element) doc.getElementsByTagName("售价").item(0);
e.setTextContent("999"); //改售价
//把更新的东西写回xml文档
TransformerFactory transfactory = TransformerFactory.newInstance();
Transformer transformer = transfactory.newTransformer();
//这里传的两个参数都是接口(Source,Result),要用具体的
transformer.transform(new DOMSource(doc), new StreamResult( new FileOutputStream("src/mypackage/first.xml")));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
基础操作学了之后就有个小练习:用xml实习学生管理系统
下面是老师画的解析图
感觉学到了不少,可别小看这个系统,将系统分成了几部分,让人更清晰的编写程序
下面是代码:
package bean;
//Student类,作为bean
public class Student
{
private String id;
private String name;
private double grades;
public Student()
{
}
public Student( String id,String name,double grades )
{
this.id = id;
this.name = name;
this.grades = grades;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public double getGrades()
{
return grades;
}
public void setGrades(double grades)
{
this.grades = grades;
}
}
package utils;
//工具类,主要是读取xml和存取xml(访问数据库)
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.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.xml.sax.SAXException;
public class XmlUtils
{
private static String filename = "src/info.xml";
public static Document getDocument() throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(filename);
}
public static Document getDocument( String filename ) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(filename);
}
public static void writeXML( Document document ) throws Exception
{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform( new DOMSource(document), new StreamResult( new FileOutputStream(filename) ) );
}
}
package dao;
//增删改查
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import bean.Student;
import exception.StudentNotExistException;
import utils.XmlUtils;
public class StudentDAO
{
public void add( Student student )
{
try
{
Document document = XmlUtils.getDocument();
//创建学生标签
Element student_tag = document.createElement("student");
student_tag.setAttribute("id", student.getId() );
//创建name和grades
Element name = document.createElement("name");
Element grades = document.createElement("grades");
name.setTextContent(student.getName());
grades.setTextContent( ""+student.getGrades() );
student_tag.appendChild(name);
student_tag.appendChild(grades);
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
XmlUtils.writeXML(document);
} catch (Exception e)
{
throw new RuntimeException(e);
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
public Student find( String id )
{
try
{
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");
for( int i = 0 ; i < list.getLength() ; i++ )
{
Element element = (Element) list.item(i);
if( element.getAttribute("id").equals(id) )
{
Student s = new Student();
s.setId(id);
String name = element.getElementsByTagName("name").item(0).getTextContent();
String grades = element.getElementsByTagName("grades").item(0).getTextContent();
s.setGrades(Double.parseDouble(grades) );
s.setName(name);
return s;
}
}
return null;
} catch (Exception e)
{
// TODO Auto-generated catch block
throw new RuntimeException(e);
//e.printStackTrace();
}
}
public void delete( String name ) throws StudentNotExistException
{
try
{
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for( int i = 0 ; i < list.getLength() ; i++ )
{
Element e = (Element) list.item(i);
if( e.getTextContent().equals(name) )
{
Node node = e.getParentNode().getParentNode();
node.removeChild( e.getParentNode() );
XmlUtils.writeXML(document);
return;
}
}
throw new StudentNotExistException();
}
catch( StudentNotExistException e )
{
throw e;
}
catch (Exception e)
{
// TODO Auto-generated catch block
//e.printStackTrace();
throw new RuntimeException(e);
}
}
}
package ui;
//用户界面
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import bean.Student;
import dao.StudentDAO;
public class Main
{
public static void main( String args[] )
{
StudentDAO dao = new StudentDAO();
System.out.println("(1)添加学生 (2)查找学生 (3)删除学生");
System.out.println("请输入操作");
BufferedReader reader = new BufferedReader( new InputStreamReader(System.in) );
Scanner in = new Scanner(System.in);
try
{
String type = reader.readLine();
if( type.equals(String.valueOf(1) ) )
{
System.out.println("请输入姓名,学号,成绩");
String name,id;
double grades;
name = in.next();
id = in.next();
grades = in.nextDouble();
Student s = new Student(id, name, grades);
dao.add(s);
System.out.println("添加成功");
}
else if( type.equals(String.valueOf(2) ) )
{
}
else if( type.equals(String.valueOf(3) ) )
{
}
else System.out.println("不支持此操作");
} catch (IOException e)
{
// TODO Auto-generated catch block
System.out.println("出错了");
e.printStackTrace();
}
}
}
package exception;
public class StudentNotExistException extends Exception
{
public StudentNotExistException()
{
// TODO Auto-generated constructor stub
}
public StudentNotExistException(String message)
{
super(message);
// TODO Auto-generated constructor stub
}
public StudentNotExistException(Throwable cause)
{
super(cause);
// TODO Auto-generated constructor stub
}
public StudentNotExistException(String message, Throwable cause)
{
super(message, cause);
// TODO Auto-generated constructor stub
}
public StudentNotExistException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace)
{
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}
二、使用SAX解析
假设我们的XML文档如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
<student>
<name>Tom</name>
<grades>89</grades>
</student>
<student>
<name>Yang</name>
<grades>99</grades>
</student>
</exam>
package sax;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class Demo1
{
public static void main( String args[] )
{
//1.创建工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
//2.得到解析器
SAXParser parser = factory.newSAXParser();
//3.得到读取器
XMLReader reader = parser.getXMLReader();
//4.设置内容处理器
BeanHandler handler = new BeanHandler();
reader.setContentHandler( handler );
//5.读取xml文档内容
reader.parse("src/info.xml");
List l = handler.getStudents();
System.out.println(l.size() );
for( int i = 0 ; i < l.size() ; i++ )
{
Student t = (Student)l.get(i);
System.out.println(t.getName()+" "+t.getGrades());
}
}
catch ( Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package sax;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//获取指定标签的值
public class TaxValueHandler extends DefaultHandler
{
String currentTag = null;
int cnt = 0,goal = 2;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
currentTag = qName;
if( currentTag.equals("name") )
cnt++;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
currentTag = null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
// TODO Auto-generated method stub
super.characters(ch, start, length);
if( currentTag == null )
return;
if( cnt == goal && currentTag.equals("name") )
{
System.out.println( new String(ch,start,length) );
}
}
}
package sax;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
//遍历整个XML文档的handler
class TraverseHandler implements ContentHandler
{
@Override
public void setDocumentLocator(Locator locator)
{
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void endPrefixMapping(String prefix) throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
{
// TODO Auto-generated method stub
//先输出名称
System.out.println("<"+qName+">");
//输出属性
if( atts != null )
for( int i = 0 ; i < atts.getLength() ; i++ )
{
String aname = atts.getQName(i);
String avalue = atts.getValue(i);
System.out.println(aname+"="+avalue);
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
// TODO Auto-generated method stub
System.out.println("</"+qName+">");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
// TODO Auto-generated method stub
System.out.println(new String(ch,start,length) );
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target, String data) throws SAXException
{
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException
{
// TODO Auto-generated method stub
}
}
package sax;
import java.util.List;
import java.util.Vector;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
//将信息存储到list中
public class BeanHandler extends DefaultHandler
{
String currentTag = null;
boolean haveRead = false; //记录是否已经读了内容
List<Student> list = new Vector<Student>();
Student student = null;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
{
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if( qName.equals("student") )
{
student = new Student();
}
currentTag = qName;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
// TODO Auto-generated method stub
super.characters(ch, start, length);
String s = new String(ch,start,length);
s.trim();
if( currentTag == null )
return;
//System.out.println("内容: "+s);
if( currentTag.equals("name") )
{
student.setName(s);
//haveRead = true;
}
else if( currentTag.equals("grades") )
{
student.setGrades(s);
//haveRead = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if( qName.equals("student") )
{
if( student != null )
{
list.add(student);
student = null;
}
}
else if( qName.equals("name") || qName.equals("grades")) //一个element结束
currentTag = null;
}
public List getStudents()
{
return list;
}
}
三、使用dom4j解析XML文档
package dom4j;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
//使用dom4j对XML文档进行增删改查
public class Demo1
{
@Test
//读取内容
public void test() throws Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read( new File("src/info.xml") );
Element root = document.getRootElement();
//得到某一具体内容
Element student1 = (Element) root.elements("student").get(0);
String s = student1.element("name").getText();
System.out.println(s);
//得到某一属性值
Element student2 = (Element) root.elements("student").get(1);
String s2 = student2.attribute("id").getName();
System.out.println(s2);
}
@Test
//添加内容(简单添加)
public void add() throws Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read( new File("src/info.xml") );
Element root = document.getRootElement();
Element student1 = (Element) root.elements("student").get(0);
//给第一个学生加一个班级<class>3-1</class>
student1.addElement("class").setText("3-1");
XMLWriter writer = new XMLWriter( new FileWriter("src/info.xml") );
writer.write(document);
writer.close();
}
@Test
//指定位置添加
public void add2() throws Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read( new File("src/info.xml") );
//<grades>89</grades>后面加一个grades
Element root = document.getRootElement();
Element student1 = root.element("student"); //得到第一个学生
List l = student1.elements();
Element grades = DocumentHelper.createElement("grades");
grades.setText("100");
l.add(2, grades); //在list的指定位置添加,也就相当于在xml中添加
//最后写回
XMLWriter writer = new XMLWriter( new FileWriter("src/info.xml") );
writer.write(document);
writer.close();
}
@Test
public void delete() throws Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read( new File("src/info.xml") );
Element root = document.getRootElement();
Element grades = root.element("student").element("grades");
grades.getParent().remove(grades);
//最后写回
XMLWriter writer = new XMLWriter( new FileWriter("src/info.xml") );
writer.write(document);
writer.close();
}
@Test
public void update() throws Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read( new File("src/info.xml") );
Element root = document.getRootElement();
Element student1 = (Element) root.elements().get(0);
student1.element("grades").setText("59"); //把成绩修改为59
//最后写回
XMLWriter writer = new XMLWriter( new FileWriter("src/info.xml") );
writer.write(document);
writer.close();
}
}
XPath
package mine;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Test
{
public static void main( String args[] )
{
try
{
//解析文档
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("src/flight.xml");
XPathFactory factory = XPathFactory.newInstance(); //创建 XPathFactory
XPath xpath = factory.newXPath();//用这个工厂创建 XPath 对象
String query = "info/flight[price>100 and place = '北京']";
//查询price>100 且 place=北京 的flight节点
NodeList nodes = (NodeList)xpath.evaluate(query, doc, XPathConstants.NODESET);
String id = "";
String price = "";
for (int i = 0; i < nodes.getLength(); i++)
{
Node node = nodes.item(i);
//打印XML中 id,price两个值
id = (String) xpath.evaluate("id", node, XPathConstants.STRING);
price = (String) xpath.evaluate("price", node, XPathConstants.STRING);
System.out.println(id+" "+price);
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
下面是我学过的PPT
第一讲
第二节
(约束技术)