xml解析
dom解析(Document object model文档对象模型,官方推荐)
- dom解析特点
- 将整个xml文档存入内存中,即一个document对象
- 优点
- 缺点
- 使用dom解析xml文档,如果文件比较大,对内存消耗就特别大,极容易导致内存溢出, 所以dom方式不适合操作大的xml文档
sax解析
- sax解析特点
- 优点
- 缺点
- 由于是逐行读取,所以只适合查找数据,不适合增删改操作
xml解析包
public classJaxp {
/**
* 使用jaxp操作xml文档
*
* @param args
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1.获取工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//2.产生解析器
DocumentBuilder builder = factory.newDocumentBuilder();
//3.解析xml文档,得到代表文档的document
Document document = builder.parse(new File("src/book1.xml"));
//遍历
list(document);
}
//得到售价结点的值
@Test
public void read() throws Exception {
//1.获取工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book.xml"));
NodeList list = document.getElementsByTagName("售价");
Node price = list.item(0);
String value = price.getTextContent();
System.out.println(value);
}
//修改结点的值:<售价>39.00元</售价>改为109
@Test
public void update() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book1.xml"));
Node price = document.getElementsByTagName("售价").item(0);
price.setTextContent("109");
//把内存中的document写到xml文档
TransformerFactory tf = TransformerFactory.newInstance();
//得到转换器
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));
}
//向指定节点中增加孩子节点(售价节点)
@Test
public void add() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book1.xml"));
//创建需要增加的节点
Node price = document.createElement("售价");
price.setTextContent("59元");
//得到需要增加的节点的父亲
Node parent = document.getElementsByTagName("书").item(0);
//把需要增加的节点挂到父结点上
parent.appendChild(price);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));
}
//向指定位置上插入售价节点
@Test
public void add2() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book1.xml"));
Node node = document.createElement("售价");
node.setTextContent("39元");
Node parent = document.getElementsByTagName("书").item(0);
parent.insertBefore(node, document.getElementsByTagName("书名").item(0));
TransformerFactory tf = TransformerFactory.newInstance();
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));
}
//删除xml文档的售价结点
@Test
public void delete() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book1.xml"));
Node node = document.getElementsByTagName("售价").item(2);
node.getParentNode().removeChild(node);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));
}
//操作xml文档属性
@Test
public void updateAttribute() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/book1.xml"));
//操作xml文档的元素时,一般都把元素当作node对象,但是程序员如果发现node不好使时,就应把node强转成相应类型
Node node = document.getElementsByTagName("书").item(0);
Element book = null;
if (node.getNodeType() == Node.ELEMENT_NODE) { //在作结点转换之前,最好先判断结点类型
book = (Element) node;
}
book.setAttribute("name", "yyyyyyy");
book.setAttribute("password", "123");
book.removeAttribute("password");
TransformerFactory tf = TransformerFactory.newInstance();
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/book1.xml")));
}
//遍历
public static void list(Node node) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(node.getNodeName());
}
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
list(child);
}
}
}
public classDom4j {
//读取xml文档数据:<书名>Java就业培训教程</书名>
@Test
public void read() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element root = document.getRootElement();
Element bookname = root.element("书").element("书名");
System.out.println(bookname.getText());
}
//<书 name="yyyyyyy">
@Test
public void readAttr() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element root = document.getRootElement();
String value = root.element("书").attributeValue("name");
System.out.println(value);
}
//向xml文档中添加<售价>19元</售价>
@Test
public void add() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = DocumentHelper.createElement("售价");
price.setText("19元");
document.getRootElement().element("书").add(price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"), format);
writer.write(document); //utf-8
writer.close();
}
//修改:<售价>109</售价> 为209
@Test
public void update() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = (Element) document.getRootElement().element("书").elements("售价").get(1);
price.setText("209元");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"), format);
writer.write(document); //utf-8
writer.close();
}
//删除:<售价>109</售价>
@Test
public void delete() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = (Element) document.getRootElement().element("书").elements("售价").get(0);
price.getParent().remove(price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"), format);
writer.write(document); //utf-8
writer.close();
}
//向指定位置增加售价结点
@Test
public void add2() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element price = DocumentHelper.createElement("售价");
price.setText("19元");
List list = document.getRootElement().element("书").elements();
list.add(1, price);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("src/book1.xml"), format);
writer.write(document); //utf-8
writer.close();
}
@Test
public void findWithXpath() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/book1.xml"));
Element e = (Element) document.selectNodes("//书名").get(1);
System.out.println(e.getText());
}
@Test
public void findUser() throws Exception {
String username = "aaa";
String password = "1233";
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/users.xml"));
Element e = (Element) document.selectSingleNode("//user[@username='" + username + "' and @password='" + password + "']");
if (e != null) {
System.out.println("让用户登陆成功!!");
} else {
System.out.println("用户名和密码不正确!!");
}
}
}
sax解析原理和实例
publicclassSax {
/**
* sax方式解析book1.xml文件
*
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/publicstaticvoidmain(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1.创建工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
//2.用工厂创建解析器
SAXParser sp = factory.newSAXParser();
//3.利用解析器得到reader
XMLReader reader = sp.getXMLReader();
//4、在解析xml文档之前,设置好事件处理器
reader.setContentHandler(new MyContentHandler2());
//4.利用reader读取 xml文档
reader.parse("src/book1.xml");
}
//用于获取第一个售价节点的值:<售价>109</售价>classMyContentHandler2extendsDefaultHandler {
privateboolean isOk = false;
privateint index = 1;
@Override
publicvoidcharacters(char[] ch, int start, int length)
throws SAXException {
if (isOk == true && index == 1) {
System.out.println(new String(ch, start, length));
}
}
@Override
publicvoidstartElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
if (name.equals("售价")) {
isOk = true;
}
}
@Override
publicvoidendElement(String uri, String localName, String name)
throws SAXException {
if (name.equals("售价")) {
isOk = false;
index++;
}
}
}
//得到xml文档内容的事件处理器classMyContentHandlerimplementsContentHandler {
publicvoidstartElement(String uri, String localName, String name,
Attributes atts) throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是开始标签");
for (int i = 0; i < atts.getLength(); i++) {
String attname = atts.getQName(i);
String attvalue = atts.getValue(i);
System.out.println(attname + "=" + attvalue);
}
}
publicvoidendElement(String uri, String localName, String name)
throws SAXException {
System.out.println("当前解析到了:" + name + ",这个标签是结束标签");
}
publicvoidcharacters(char[] ch, int start, int length)
throws SAXException {
System.out.println("当前解析到了内容:" + new String(ch, start, length));
}
publicvoidendDocument() throws SAXException {
// TODO Auto-generated method stub
}
publicvoidendPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
publicvoidignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
}
publicvoidprocessingInstruction(String target, String data)
throws SAXException {
// TODO Auto-generated method stub
}
publicvoidsetDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
publicvoidskippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
publicvoidstartDocument() throws SAXException {
// TODO Auto-generated method stub
}
publicvoidstartPrefixMapping(String prefix, String uri)
throws SAXException {
// TODO Auto-generated method stub
}
}
}
XPATH(属于dom4j的包,需要导入使用xpath需要导入jaxen-1.1-beta-6)