XmlElement is a subclass of XmlNode (and so is XmlAttribute, XmlText, XmlDocument, etc.). There is no performance implication of using one over the other at all, since XmlNode is simply the base class for all types of nodes in a DOM Xml document instance. It just so happens, that many of the APIs in the DOM return an XmlNode reference because they could return several kinds of nodes. For example, with SelectSingleNode(), your XPath expression could return an element, an attribute, an XmlText and what not; whether you handle it as an XmlNode or cast it to the right type is just a matter of how you write your code but won't affect performance at all. XmlElements are a type of node. In fact, if you look at the members of XmlNode and XmlElement in the .NET Framework, you will see that they are very much alike, but XmlElement has more going on. It inherits XmlNode and then is further customized. This is because an element is more specialized. A node is more general in scope. The document is a node, a processing instruction is a node, and so forth. Elements are different. If you look at the XmlNodeType property of an element, you will see that it is Element, one of the many types of nodes you find.
Element是Node的子集,XmlNode表示一个节点,包括XmlElement(元素)和XmlAttribute(属性)等。
<Alarm lock="true"> //node <Time> //node StringValue //node </Time> //node </Alarm> //node
以上Alarm(元素节点),lock(属性节点),Time(元素节点),StringValue(文本节点)都是Node,但是只有
Alarm>......</Alarm>和<Time>StringValue</Time>是Element
以下是我自己的理解-------------- xml element是继承于node,除了element之外,还有XmlAttribute、XmlText等。 通过某些方法可能可以得到某个值,可能是element、attribute、xmlText等,如果你不确定,则可以使用node来做返回值的类型。 node只是具有了一些通用的方法,而element则具有更具体的功能。
另一篇:
1.元素(Element)和结点(Node)的区别,元素是一个小范围的定义,必须是含有完整信息的结点才是一个元素,例如<div>...</div>。但是一个结点不一定是一个元素,而一个元素一定是一个结点。
什么是node:
NODE是相对TREE这种数据结构而言的。TREE就是由NODE组成。这个部分你可以参考离散数学的树图。
什么是element
ELEMENT则是XML里的概念,<xxx>就是元素,是XML中的数据的组成部分之一。
<a>
<b> </b>
<b> </b>
<a>
DOM将文档中的所有都看作节点 node>element
1DOM在解析文档的时候按整个文档的结构生成一棵树,全部保存在内存
优点就是整个文档都一直在内存中,我们可以随时访问任何节点,并且对树的遍历也是比较熟悉的操作;缺点则是耗内存,并且必须等到所有的文档都读入内存才能进行处理。
2一个需要注意的地方就是,XML文档两个标签之间的空白也是这棵树的一个节点(Text节点)。 <a> <b></b> <a> a有三个节点
Element root = doc.getDocumentElement();:root是什么????
NodeList list = root.getChildNodes(); root 到底是节点还是元素我不清楚?????
node有几个子类型:
Element,
Text,
Attribute,
RootElement,
Comment,
Namespace等
Element是可以有属性和子节点的node。
Element是从Node继承的
//转换
if (node.getNodeType() == Element.ELEMENT_NODE)
{
Element e = (Element) node;
}
?元素有孩子吗
elemen et 性质
1 e.getAttributes()
2 e.getChildNodes()
3 e.getTagName()
Element root = doc.getDocumentElement();:root是什么????
NodeList list = root.getChildNodes(); root 到底是节点还是元素我不清楚???
······················································
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<FormBeanConfig>();
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<ActionConfig>();
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);
}
}
}
}
tree-------xml 文件
另外:
an element
is that which consists of a start tag, and end tag, and the content in between,(element有开始标签,内容和闭合标签组成)。 or alternately an empty element tag (which has no content or end tag). In other words, these are all elements:
<foo> stuff </foo>
<foo bar="baz"></foo>
<foo baz="qux" />
Though you hear "node" used with roughly the same meaning, it has no precise definition per XML specs. It's usually used to refer to nodes of things like DOMs, which may be closely related to XML or use XML for their representation.