XML-DOM

https://www.w3schools.com/xml/dom_nodes.asp

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class DomParserDemo  {

  public static void main(String argv[]) {

    try {

//	File fXmlFile = new File("staff.xml");
	File fXmlFile = new File("book.xml");
	
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	Document doc = dBuilder.parse(fXmlFile);
			
	//optional, but recommended
	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
	doc.getDocumentElement().normalize();

	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			
//	NodeList nList = doc.getElementsByTagName("staff");
	NodeList nList = doc.getElementsByTagName("book");
			
	System.out.println("----------------------------");

	for (int temp = 0; temp < nList.getLength(); temp++) {

		Node nNode = nList.item(temp);
				
		System.out.println("\nCurrent Element :" + nNode.getNodeName());
				
		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			Element eElement = (Element) nNode;

//			System.out.println("Staff id : " + eElement.getAttribute("id"));
//			System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
//			System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
//			System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
//			System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

			System.out.println("category : " + eElement.getAttribute("category"));
			System.out.println("title  : " + eElement.getElementsByTagName("title").item(0).getTextContent());
			System.out.println("author  : " + eElement.getElementsByTagName("author").item(0).getTextContent());
			System.out.println("year  : " + eElement.getElementsByTagName("year").item(0).getTextContent());
			System.out.println("price  : " + eElement.getElementsByTagName("price").item(0).getTextContent());

			
			
			
		}
	}
    } catch (Exception e) {
	e.printStackTrace();
    }
  }

}

OUTPUT :
Root element :bookstore

Current Element :book
category : cooking
title : Everyday Italian
author : Giada De Laurentiis
year : 2005
price : 30.00

Current Element :book
category : children
title : Harry Potter
author : J K. Rowling
year : 2005
price : 29.99

Current Element :book
category : web
title : XQuery Kick Start
author : James McGovern
year : 2003
price : 49.99

Current Element :book
category : web
title : Learning XML
author : Erik T. Ray
year : 2003
price : 39.95

Text is Always Stored in Text Nodes
A common error in DOM processing is to expect an element node to contain text.

However, the text of an element node is stored in a text node.

In this example: 2005, the element node holds a text node with the value “2005”.

“2005” is not the value of the element!

DOM Example
Look at the following XML file (books.xml):

<?xml version="1.0" encoding="UTF-8"?> Everyday Italian Giada De Laurentiis 2005 30.00 Harry Potter J K. Rowling 2005 29.99 XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn Vaidyanathan Nagarajan 2003 49.99 Learning XML Erik T. Ray 2003 39.95

The root node in the XML above is named .

All other nodes in the document are contained within .

The root node holds 4 nodes.

The first node holds the child nodes: , , , and .

The child nodes contain one text node each, “Everyday Italian”, “Giada De Laurentiis”, “2005”, and “30.00”.

Text is Always Stored in Text Nodes
A common error in DOM processing is to expect an element node to contain text.

However, the text of an element node is stored in a text node.

In this example: 2005, the element node holds a text node with the value “2005”.

“2005” is not the value of the element!

The XML DOM Node Tree
The XML DOM views an XML document as a tree-structure. The tree structure is called a node-tree.

All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.

The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree:

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值