package com;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Parse{
//Document可以看作是XML在内存中的一个镜像,那么一旦获取这个Document 就意味着可以通过对
//内存的操作来实现对XML的操作,首先第一步获取XML相关的Document
private Document doc=null;
public void init(String xmlFile) throws Exception{
//很明显该类是一个单例,先获取产生DocumentBuilder工厂
//的工厂,在通过这个工厂产生一个DocumentBuilder,
//DocumentBuilder就是用来产生Document的
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
//这个Document就是一个XML文件在内存中的镜像
doc=db.parse(new File(xmlFile));
}
//该方法负责把XML文件的内容显示出来
public void viewXML(String xmlFile) throws Exception{
this.init(xmlFile);
//在xml文件里,只有一个根元素,先把根元素拿出来看看
Element element=doc.getDocumentElement();
System.out.println("根元素为:"+element.getTagName());
NodeList nodeList=doc.getElementsByTagName("book");
System.out.println("book节点链的长度:"+nodeList.getLength());
Node fatherNode=nodeList.item(0);
System.out.println("父节点为:"+fatherNode.getNodeName());
//把父节点的属性拿出来
NamedNodeMap attributes=fatherNode.getAttributes();
for(int i=0;i<attributes.getLength();i++){
Node attribute=attributes.item(i);
System.out.println("book的属性名为:"+attribute.getNodeName()+" 相对应的属性值为:"+attribute.getNodeValue());
}
NodeList childNodes = fatherNode.getChildNodes();
System.out.println(childNodes.getLength());
for(int j=0;j<childNodes.getLength();j++){
Node childNode=childNodes.item(j);
//如果这个节点属于Element ,再进行取值
if(childNode instanceof Element){
//System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
System.out.println("子节点名为:"+childNode.getNodeName()+"相对应的值为"+childNode.getFirstChild().getNodeValue());
}
}
}
public static void main(String[] args)throws Exception{
Parse parse=new Parse();
//我的XML文件
parse.viewXML("netct.xml");
}
}
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/k1113k/archive/2007/01/27/1495807.aspx
分享到:
相关推荐
【Qt】DOM读取XML文档.rar
android dom解析XMl文件,这是一个简单的小示例,希望对大家有所帮助
学习XML语言很重要的一章节,其中的第八章节,掌握如何用DOM操作XML文档
DOMProject(通过DOM读取XML),非常不错的源代码,有利于初学者的学习
DOM操作XML文档向表格添加数据.rarDOM操作XML文档向表格添加数据.rarDOM操作XML文档向表格添加数据.rarDOM操作XML文档向表格添加数据.rar
文档对象模型 节点和节点树 使用DOM加载XML文档 使用DOM访问、创建和修改节点
NULL 博文链接:https://justsee.iteye.com/blog/923633
Java与XML(一):采用DOM操作XML文件.docx
所有相关文件 博文链接:https://anlinko.iteye.com/blog/714273
java DOM 对xml文件的操作【分别对节点、节点属性、节点内容增删改查】
java.dom操作xml完整的例子 例子
Javascript里使用Dom操作Xml.docx
VB读取XML文件的操作实例,演示读取XML各个定义键的值。程序是一个专一读取XML文件的VB类,并没有使用微软的XML.DLL。因此比较有参考价值,很不错。
Javascript里使用Dom操作Xml_1.docx
利用java操作xml的源代码,包括DOM和SAX两种方式
优化读取xml的性能
Java DOM 生成XML,对基本的DOM又封装了一层,使操作XML更简单
王兴魁老师 AJAX视频(NetBeans版)源码,我是使用VS编写的。
利用DOM创建DOMDocument对象,生成指定格式的XML文档,亦可对应读取相应文档。
树形控件的每个节点映射到XML,双击树节点显示其值或属性值,增加、删除、修改树节点的同时修改xml中相对应节点!其中的大部分算法都采用递归,希望对你有所帮助!