<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);
}
}
}
}
以上就是详解XML中Node和Element区别的示例代码的详细内容,更多请关注php中文网其它相关文章!
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
程序员必备接口测试调试工具:点击使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
网页生成APP,用做网站的技术去做APP:立即创建
手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包
相关文章
相关视频