Sample code that explains the difference between Node and Element in XML

黄舟
Release: 2017-03-23 16:25:58
Original
1608 people have browsed it

. But a node is not necessarily an element, and an element must be a node.
What is node:

NODE is relative to the data structure of TREE. TREE is composed of NODE. You can refer to the tree diagram of discrete mathematics for this part.

What is element

ELEMENT is a concept in
XML , is an element, which is one of the components of data in XML.
The difference between element (Element) and node (Node). Element is a small-scale definition. It must be a node containing complete information to be an element, such as

...< /p>. But a node is not necessarily an element, and an element must be a node.

DOM regards everything in the document as node node>element

1DOM generates a tree according to the structure of the entire document when parsing the document, and saves all In memory

The advantage is that the entire document is always in memory, we can access any node at any time, and tree traversal is also a relatively familiar operation; the disadvantage is that it consumes memory and must wait until all documents are read. into memory for processing.
2 One thing to note is that the space between the two tags of the XML document is also a node (Text node) of this tree.
a has three nodes

Element root = doc.getDocumentElement();: What is root? ? ? ?
NodeList list = root.getChildNodes(); I don’t know whether root is a node or an element? ? ? ? ?

node has several subtypes:


Element, Text, Attribute, RootElement, Comment, Namespace等
Copy after login

Element is a node that can have

attributesand child nodes.

Element is inherited

from Node

//转换 if (node.getNodeType() == Element.ELEMENT_NODE) { Element e = (Element) node; }
Copy after login

Does the element have children?

elemen et properties

1 e.getAttributes()

2 e.getChildNodes()


3 e.getTagName()


Element root = doc.getDocumentElement();: What is root? ? ? ?


NodeList list = root.getChildNodes(); I don’t know whether root is a node or an element? ? ?

·········································· ··········

public void domParse(String fileName) throws Exception { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); DocumentBuilder db = f.newDocumentBuilder();//builder Document docment = db.parse(new File(fileName));//parese Element el = docment.getDocumentElement();//root domRead(el); } public void domRead(Element currentNode) { if ("struts-config".equals(currentNode.getNodeName())) { config = new StrutsConfig(); } NodeList list = currentNode.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); if (node.getNodeType() == Element.ELEMENT_NODE) { Element e = (Element) node;//???? if ("form-beans".equals(e.getTagName())) { formBeans = new ArrayList(); domRead(e); } if ("form-bean".equals(e.getTagName())) { FormBeanConfig fc = new FormBeanConfig(); NamedNodeMap attrs = e.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Attr attr = (Attr) attrs.item(j); if ("name".equals(attr.getName())) { fc.setName(attr.getValue()); } else { fc.setType(attr.getValue()); } } formBeans.add(fc); } if ("action-mapping".equals(e.getTagName())) { actions = new ArrayList(); domRead(e); } if ("action".equals(e.getTagName())) { ActionConfig ac = new ActionConfig(); NamedNodeMap attrs = e.getAttributes(); for (int k = 0; k < attrs.getLength(); k++) { Attr attr = (Attr) attrs.item(k); if ("path".equals(attr.getName())) { ac.setPath(attr.getValue()); } else if ("type".equals(attr.getName())) { ac.setType(attr.getValue()); } else { ac.setName(attr.getValue()); } } actions.add(ac); } } } }
Copy after login

The above is the detailed content of Sample code that explains the difference between Node and Element in XML. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!