...< /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? ? ? ? ?
Element, Text, Attribute, RootElement, Comment, Namespace等
//转换 if (node.getNodeType() == Element.ELEMENT_NODE) { Element e = (Element) node; }
1 e.getAttributes()
3 e.getTagName()
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); } } } }
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!