Sample code for the difference between Node and Element in XML

Source: Internet
Author: User
1. the difference between an Element and a Node is that an Element is defined in a small scope and must be a Node containing complete information, such as <; div & gt ;... & lt; div & gt ;. However, a node is not necessarily an element, but an element must be a node. 1. The difference between an Element and a Node. an Element is defined in a small scope and must be a Node containing complete information. for example

...

. However, a node is not necessarily an element, but an element must be a node.
What is node:

NODE is relative to the data structure such as TREE. A tree is composed of nodes. For this part, you can refer to the tree chart of discrete mathematics.

What is element

ELEMENT is a concept in XML, Is an element, which is a part of the data in XML.

Element and Node are different. an Element is defined in a small scope and must be a Node with complete information. for example

...

. However, a node is not necessarily an element, but an element must be a node.

          


DOM regards everything in the document as node> element

1DOM generates a tree based on the structure of the entire document when parsing the document, all of which are stored in the memory

The advantage is that the entire document is always in the memory. we can access any node at any time and we are familiar with tree traversal. The disadvantage is that memory consumption, you must wait until all documents are read into the memory for processing.
2. Note that the gap between labels in the XML document is also a node (Text node) in the 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, etc.

Element is a node that can have attributes and subnodes.

Element is inherited from Node

// Convert if (node. getNodeType () = Element. ELEMENT_NODE) {Element e = (Element) node ;}

Does element have children?

Elemen et nature
1 e. getAttributes ()

2 e. getChildNodes ()

3 e. getTagName ()

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

NodeList list = root. getChildNodes (); is root 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);    }   }  } }
  

The above is a detailed description of the sample code for the differences between Node and Element in XML. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.