package javawebservice;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.w3c.dom.*;
public class DOMDemo
{
//设置输出编码格式
static final String outputEncoding="UTF-8";
private PrintWriter out;
DOMDemo(PrintWriter out)
{
this.out=out;
}
private void print(Node node)
{
//打印节点名称
out.println("NodeName=\""+node.getNodeName()+"\"");
//打印节点URI
String value=node.getNamespaceURI();
if(value!=null)
{
out.println("URL=\""+value+"\"");
}
//打印节点前缀
value=node.getPrefix();
if(value!=null)
{
out.println("Prefix=\""+value+"\"");
}
//打印节点本地名称
value=node.getLocalName();
if(value!=null)
{
out.println("LocalName=\""+value+"\"");
}
//打印节点值
value=node.getNodeValue();
if(value!=null)
{
out.println("NodeValue=");
if(value.trim().equals(""))
{
out.println("[WS]");
}
else
{
out.println("\""+node.getNodeValue()+"\"");
}
}
}
private void echo(Node node)
{
int type=node.getNodeType();
switch(type)
{
//属性节点
case Node.ATTRIBUTE_NODE:
out.println("ATTR");
print(node);
break;
//CDATA
case Node.CDATA_SECTION_NODE:
out.println("CDATA");
print(node);
break;
//注释
case Node.COMMENT_NODE:
out.println("COMMENT");
print(node);
break;
//段落节点
case Node.DOCUMENT_FRAGMENT_NODE:
out.println("DOC_FRAGMENT");
print(node);
break;
//文档节点
case Node.DOCUMENT_NODE:
out.println("DOC");
print(node);
break;
//文档类型节点
case Node.DOCUMENT_TYPE_NODE:
out.println("DOC_TYPE");
print(node);
break;
//元素节点
case Node.ELEMENT_NODE:
out.println("ELEMENT");
print(node);
//得到该节点所有属性
NamedNodeMap atts=node.getAttributes();
for(int i=0;i<atts.getLength();i++)
{
Node att=atts.item(i);
//递归调用
echo(att);
}
break;
//实体节点
case Node.ENTITY_NODE:
out.println("ENTITY");
print(node);
break;
//实体引用节点
case Node.ENTITY_REFERENCE_NODE:
out.println("ENTITY_REF");
print(node);
break;
//处理说明节点
case Node.NOTATION_NODE:
out.println("NOTATION");
print(node);
break;
//预处理节点
case Node.PROCESSING_INSTRUCTION_NODE:
out.println("PROC_INST");
print(node);
break;
//文本节点
case Node.TEXT_NODE:
out.println("TEXT");
print(node);
break;
//未知节点
default:
out.println("UNKNOWN NODE:"+type);
print(node);
}
//遍历该节点子节点
for(Node child=node.getFirstChild();
child!=null;child=child.getNextSibling())
{
echo(child);
}
}
public static void main(String[] args)
{
//生成一个DocumentBuilderFactory
DocumentBuilderFactory dbf=
DocumentBuilderFactory.newInstance();
//生成一个DocumentBuilder
DocumentBuilder db=null;
try
{
db=dbf.newDocumentBuilder();
}
catch(ParserConfigurationException pce)
{
System.out.println(pce.toString());
}
String fileName=args[0];
//生成文档
Document doc=null;
try
{
//解析文档
doc=db.parse(new File(fileName));
}
catch(IOException ioe)
{
System.out.println(ioe.toString());
}
catch(SAXException se)
{
System.out.println(se.toString());
}
try
{
//显示DOM树
OutputStreamWriter outWriter=
new OutputStreamWriter(System.out,outputEncoding);
new DOMDemo(new PrintWriter(outWriter,true)).echo(doc);
}
catch(UnsupportedEncodingException uee)
{
System.out.println(uee.toString());
}
}
}
分享到:
相关推荐
本篇文章将详细介绍DOM和SAX解析XML的方法,并通过提供的`DomDemo.java`和`SaxDemo.java`示例代码进行阐述。 1. DOM解析: DOM是一种将整个XML文档加载到内存中,构建一个树形结构的解析方式。这种方式允许开发者...
"domdemo"和"jsdemo"是两个子文件夹,可能分别包含了关于DOM操作和JavaScript编程的示例。 1. **HTML**:HTML(HyperText Markup Language)是Web页面的基础结构语言,用于定义网页内容和布局。在HBuilder中,你...
例如:TestDemo、DOMDemo POJO 类命名规范: * 具有含义的名字 * 如果有多个单词,每个单词头字母大写 例如:StudentInfo 接口类命名规范: * 业务逻辑接口实现类的命名以 POJO 名称来制定 * 以大写字母"I"开头...
public class DomDemo { private Document document; private String fileName; // 创建XML文档 public void createXml(String fileName) { DocumentBuilderFactory factory = DocumentBuilderFactory.new...
public class DomDemo implements XmlDocument { private Document document; private String fileName; public void init() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()...
在提供的`DomDemo`项目中,你将找到一个实际的Android应用示例,包含详细的注释,指导你如何在Android环境中实现DOM解析。通过实践这个示例,你可以更直观地理解DOM解析的工作流程,并能更好地应用到自己的项目中。 ...
public class DomDemo implements XmlDocument { private Document document; private String fileName; public void init() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()...
public class DomDemo01 { public static void main(String[] args) { // 创建一个DocumentBuilderFactory的对象 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // 创建一个...
public class DomDemo implements XmlDocument { public void createXml(String fileName) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = ...
public class DomDemo { public static void main(String argv[]) { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory....