Jav新人之路 -- XML解析

XML解析

1.xml

####1.1 什么是xml文件

以标签的形式存储的文件,后缀为xml

常用语法

<beans>

<bean id="类名" class="类签名"></bean>

</beans>
1.2.xml文件的用途

和json(相同), xml文件也是用于前后台交互中用于数据传递的方式

JSON:是一个轻量级的数据集框架,是存在于前端中的一个对象,存储形式是以键值对的形式进行存储的;

xml:(所有的属性及值虽然也是以键值对的形式存在的,但是需要以标签的格式进行匹配),内容过多的时候会导致整个文件的大小偏大,会影响解析的效率;但是目前的大多数框架使用xml做配置文件

1.3.xml文件的四种解析方式

DOM:优点是直观好理解,缺点是DOM解析需要一次性的解析整个xml文件,容易出现内存溢出

SAX:采用事件驱动,内存消耗小,无法同时访问多处不同的数据

DOM4J:是JDOM的智能分支,灵活好用,开源

JDOM:大量使用了Collections类,贴合java的API

2. DOM解析

xml文件名:item.xml
<beans>
      <bean id="items" class="cn.yunhe.beans.Item"></bean>
      <bean id="items2" class="cn.yunhe.beans.Item"></bean>
      <bean id="items3" class="cn.yunhe.beans.Item"></bean>
</beans>

实体类


public class Item {

	private String itemName;
	
	private double price;
	
	public Item() {}
	
	public Item(String itemName) {
		super();
		this.itemName = itemName;
	}
	public void show(String s) {
		System.out.println(s);
	}

	
	public Item(String itemName, double price) {
		super();
		this.itemName = itemName;
		this.price = price;
		System.out.println(itemName+price);
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	@Override
	public String toString() {
		return "Item [itemName=" + itemName + ", price=" + price + "]";
	}
	
}

解析方法

public static void domParseMethod() {
		//先去创建一个工厂对象,用于去生成和创建document对象的对象 -- DocumentBuilder
		DocumentBuilderFactory  factory  = DocumentBuilderFactory.newInstance();
		//生产用于创建Document对象的builder对象
		DocumentBuilder builder = factory.newDocumentBuilder();
		//生成Document对象
		Document document= builder.parse(new FileInputStream("src/item.xml"));//输入xml文件目录
		//查找文件中的标签
		NodeList nodeList = document.getElementsByTagName("bean");
		//遍历装了所有节点的节点集合
		for(int i =0;i<nodeList.getLength();i++) {
			//遍历当前的Node节点,Node是整个节点的对象,子类Element中包含的是所有的属性相关的操作
			//(Node是对beans说的,Element是对每个bean说的)
			Element element = (Element)nodeList.item(i);
			String idVal = element.getAttribute("id");
			String clsVal = element.getAttribute("class");
			//如果只想要某个key对应的对象,再次加一个if,只让一个指定的key执行以下操作
			//拿到值之后,通过反射进行解析
			System.out.println(idVal+"--"+clsVal);
			
			//以下是利用反射来解析和创建对象
			Class cls = Class.forName(clsVal);
			Constructor cons = cls.getConstructor(String.class);
			//构建对象
			Item item = (Item)cons.newInstance("茶叶");
			System.out.println(item);
		}
		
	}
	
	
	
	输出结果:
	items--cn.yunhe.beans.Item
    Item [itemName=茶叶, price=0.0]
    items2--cn.yunhe.beans.Item
    Item [itemName=茶叶, price=0.0]
    items3--cn.yunhe.beans.Item
    Item [itemName=茶叶, price=0.0]

3. Dom4j解析方法

	public static void dom4jParseMethod() {
		SAXReader reader = new SAXReader();
		// 解析xml文件
		Document document = reader.read(new FileInputStream("src/item.xml"));
		// 获取文档的根结构
		Element rootElement = document.getRootElement();
		// 根据根节点去查找其下 面的所有的子节点(不同于Dom的是,此处的Element是导入的dom4j的包,不在是代表节点中的元素,而是代表节点)
		List<Element> elementList = rootElement.elements();
		// 遍历节点集合
		for (int i = 0; i < elementList.size(); i++) {// 循环次数是bean标签的数量
			// 获取当前节点对象
			Element element = elementList.get(i);
			// 将节点中的所有属性封装到了一个List<Attribute>
			List<Attribute> attrList = element.attributes();
			for (Attribute attr : attrList) {// 遍历集合,循环次数是每个bean标签内的键对值的对数
				String keyName = attr.getName();// 获取当前键对值的key(此处就是id和class)
				String keyVal = attr.getValue();// 获取当前键对值的value(此处就是items和...)
				System.out.println(keyName + "--" + keyVal);
			}

		}
	}
	
	
	输出结果:
	id--items
    class--cn.yunhe.beans.Item
    id--items2
    class--cn.yunhe.beans.Item
    id--items3
    class--cn.yunhe.beans.Item

如果是拿一个指定的对象

public static void dom4jParseMethod2() {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new FileInputStream("src/item.xml"));
		Element rootElement = document.getRootElement();
		List<Element> elementList = rootElement.elements();
		for (int i = 0; i < elementList.size(); i++) {
			Element element = elementList.get(i);
			List<Attribute> attrList = element.attributes();
			String idKey = null;// 一个标识
			for (Attribute attr : attrList) {
				String keyName = attr.getName();
				String keyVal = attr.getValue();
				if ("id".equals(keyName)) {// 判断是否符合要求
					if ("item".equals(keyVal)) {// 出于键值对特点的考虑,也判断一下
						idKey = keyName;
					}
				}
				if (idKey != null) {
					if ("class".equals(keyName)) {// 如果是class,执行映射,取出对象
						Class cls = Class.forName(keyVal);
						Constructor cons = cls.getConstructor(String.class);
						Item item = (Item) cons.newInstance("hello world");
						System.out.println(item);
						idKey = null;// 清空
					}
				}
			}

		}
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值