学习xml

xml:是可扩展标识语言就是开发者在符合xml命名规则的基础之上,可以根据自己的需求定义自己的标签
(必须成对存在)
作用:主要用来存储数据
解析xml文件的方法:DOM、DOM4J、SAX
(前两种是一次性将读取到内存中<小型>)
(sax:是一种事件驱动型,边读边解析<大型>)

sax使用
(1)创建解析工厂:通过newInstance()方法获取
(2)创建解析器
(3)通过该解析器去调用解析方法(parse(需要解析的xml文件,要使用的SAX DefaultHandler))

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student>
		<name>吴飞</name>
		<college>java学院</college>
		<telephone>62354666</telephone>
		<notes>男,1982年生,硕士,现就读于北京邮电大学</notes>
	</student>
	<student>
		<name>李雪</name>
		<college>C++学院</college>
		<telephone>62358888</telephone>
		<notes>男,1987年生,硕士,现就读于中国农业大学</notes>
	</student>
	<student>
		<name>Jack</name>
		<college>PHP学院</college>
		<telephone>66666666</telephone>
		<notes>我是澳洲人</notes>
	</student>
	<student>
		<name>Lucy</name>
		<college>Android学院</college>
		<telephone>88888888</telephone>
		<notes>我是美国人</notes>
	</student>
</students>
public static void main(String[] args) {
		try {
			//创建解析器
			SAXReader reader = new SAXReader();
			//通过解析器的read方法将配置文件读取到内存中,生成一个Document[org.dom4j]对象树
			Document document = reader.read("conf/students.xml");
			//获取根节点
			Element root = document.getRootElement();
			//开始遍历根节点
			for(Iterator<Element> rootIter = root.elementIterator();rootIter.hasNext();){
				Element studentElt = rootIter.next();
				for(Iterator<Element> innerIter = studentElt.elementIterator();innerIter.hasNext();){
					Element innerElt = innerIter.next();
					String innerValue = innerElt.getStringValue();
					System.out.println(innerValue);
				}
				System.out.println("-------------------------------");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

在这里插入图片描述

public static void main(String[] args) {
		try {
			//创建解析工厂
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			//创建解析器
			DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
			//通过解析器读取配置文件,生成一个Document[org.w3c.dom]对象树
			Document document = builder.parse("conf/bookstore.xml");
			
			//创建XPath对象
			XPath xPath = XPathFactory.newInstance().newXPath();
			
//			1.获取bookstore节点下book属性category值为web下的第二个title节点的文本内容
//			 bookstore -> book[@category='web'][2] -> title 
//			 xpath路径:/bookstore/book[@category='web'][2]/title/text()
			String titleXpath = "/bookstore/book[@category='web'][2]/title/text()";
			String titleValue = (String) xPath.evaluate(titleXpath, document, XPathConstants.STRING);
			System.out.println(titleValue);
			
//			2.获取bookstore节点下book属性category值为web的titile属性为en的节点内容
//			bookstore -> book[@category='web'] -> title[@lang='en']
//			xpath路径:/bookstore/book[@category='web']/title[@lang='en']/text()
			String titleLangXpath = "/bookstore/book[@category='web']/title[@lang='en']/text()";
			String titleLangValue = (String) xPath.evaluate(titleLangXpath, document, XPathConstants.STRING);
			System.out.println(titleLangValue);
			
//			 3.获取bookstore下book属性category值为cooking的title的lang属性的值
//			 bookstore -> book[@category='cooking'] -> title ->@lang
//			 xpath路径:/bookstore/book[@category='cooking']/title/@lang
			String titleLangAttrXpath = "/bookstore/book[@category='cooking']/title/@lang";
			String titleLangAttrValue = (String) xPath.evaluate(titleLangAttrXpath, document, XPathConstants.STRING);
			System.out.println(titleLangAttrValue);
			
//			 4.获取bookstore节点下所有book的节点集合
//			 /bookstore/book
			NodeList bookList = (NodeList) xPath.evaluate("/bookstore/book", document, XPathConstants.NODESET);
			//开始遍历bookList
			for(int i = 0; i < bookList.getLength(); i++){
				Element bookElt = (Element) bookList.item(i);
				String titleValue01 = (String) xPath.evaluate("title", bookElt, XPathConstants.STRING);
				String authorValue = (String) xPath.evaluate("author", bookElt, XPathConstants.STRING);
				String year = (String) xPath.evaluate("year", bookElt, XPathConstants.STRING);
				String price = (String) xPath.evaluate("price", bookElt, XPathConstants.STRING);
				System.out.println(titleValue01 + " " + authorValue + " " + year + " " + price);
				System.out.println("---------------");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

<bookstore>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="cooking">
		<title lang="en">Everyday Italian</title>
		<author>Giada De Laurentiis</author>
		<year>2005</year>
		<price>30.00</price>
	</book>
	<book category="web">
		<title lang="en">Learning XML</title>
		<author>Erik T. Ray</author>
		<year>2003</year>
		<price>39.95</price>
	</book>
	<book category="web">
		<title lang="uk">XQuery Kick Start</title>
		<author>James McGovern</author>
		<year>2003</year>
		<price>49.99</price>
	</book>
</bookstore>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值