解析一个XML文档有哪些内容
解析有:dom和sax两种
dom:把整个XML文档放入内存,适合XML随机访问,占用内存资源大
sax:事件驱动型的XML解析方式,顺序读取,不用一次装载整个文件,遇到标签会触发一个事件,适合对XML的顺序访问,占用内存资源稍小

Node:
  XML 文档的 documentElement 属性是根节点。
  nodeName 属性是节点的名称。nodeName 是只读的
  元素节点的 nodeName 与标签名相同
  属性节点的 nodeName 是属性的名称
  文本节点的 nodeName 永远是 #text
  文档节点的 nodeName 永远是 #document

  nodeType 属性是节点的类型。
  元素类型 节点类型
    元素 1
    属性 2
    文本 3
    注释 8
    文档 9

nodeValue属性规定节点的值。
元素节点的 nodeValue 是 undefined
文本节点的 nodeValue 是文本自身
属性节点的 nodeValue 是属性的值

DOM解析:

package com.briup.test3;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
//用递归DOM方法解析
public class DomBookTest {
//获取解析器 将其封装
public static Document getDoc(String filename) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(filename);
} public static void main(String[] args) throws Exception {
//获取解析地址
Document doc = getDoc("src/com/briup/test3/book.xml");
//输出头部
System.out.println("<? version="+doc.getXmlVersion()+" encoding="+doc.getXmlEncoding()+">");
//获取根节点
Element element = doc.getDocumentElement();
printdoc(element); }
public static void printdoc(Node n){
//获取节点类型
short type = n.getNodeType();
if (type==1) {
//获取节点内容
System.out.print("<"+n.getNodeName()+" ");
//用Map集合存储获取的节点属性
NamedNodeMap map = n.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
Node attr =map.item(i);
System.out.print(attr.getNodeName()+"="+attr.getNodeValue());
}
System.out.print(">");
//获取孩子节点
NodeList list = n.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node child = list.item(i);
printdoc(child);
}
System.out.print("</"+n.getNodeName()+">"); } //获取节点内容
else if(type ==3){
System.err.print(n.getNodeValue()); } } }

SAX解析:

  

package com.briup.test3;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler; public class SaxBookTest { public static void main(String[] args) throws Exception{
//获取解析器
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
//使用内部类解析文件
parser.parse("src/com/briup/test3/book.xml", new DefaultHandler(){ //解析开始标题文档
public void startDocument() throws SAXException {
System.out.println("<?xml version= 1.0 encoding= utf-8 ?>"); } //解析节点
@Override
public void startElement(String uri, String localName,
String qName, Attributes attributes) throws SAXException {
System.out.print("<"+qName+" ");
for (int i = 0; i < attributes.getLength(); i++) {
System.out.print(attributes.getQName(i)+"="+attributes.getValue(i)); }
System.out.print(">");
} @Override
//解析结束
public void endElement(String uri, String localName, String qName)
throws SAXException {
System.out.print("</"+qName+">");
} @Override
//解析内容
public void characters(char[] ch, int start, int length)
throws SAXException {
String string = new String(ch, start, length);
System.out.print(string);
} });
} }

BOOK的XML文档

<?xml version="1.0" encoding="utf-8"?>
<books>
<book bid="1">
<name>java与模式</name>
<price>80</price>
</book>
<book bid="2">
<name>java编程思想</name>
<price>95</price>
</book>
<book bid="3">
<name>疯狂java讲义</name>
<price>90</price>
</book>
</books>

最新文章

  1. iOS小知识:使UIButton中的图片和文字实现左对齐
  2. 【Swift学习】Swift编程之旅---集合类型之数组(六)
  3. omnet++5.0安装使用
  4. php -- 获取当月天数及当月第一天及最后一天、上月第一天及最后一天(备忘)
  5. Android手机号码不是所有的都能获取
  6. JAVA 根据用户输入数据求某年到某年有多少天
  7. netfilter
  8. java list三种遍历方法性能比較
  9. iOS - 操作文件目录的方法
  10. pl/sql developer 中文字段显示乱码
  11. .NET学习笔记(3) — VisualStudio使用总结
  12. T4模板合并js
  13. 智能打印SDK-源码剖析
  14. js一些重点知识总结(二)
  15. 搭建git远程服务器三步骤
  16. JSON Web Tokens(JWT)
  17. 20175324 《Java程序设计》第3周学习总结
  18. JavaScript 中 replace方法 替换所有字符串
  19. GitHub for Windows离线安装包
  20. Draw your Next App Idea with Ink to Code

热门文章

  1. session讲解(二)——商城购物车练习
  2. Android SDK 安卓失败 提示: “Failed to fetch URL…” 的错误提示
  3. 菜鸟,大牛和教主三者的区别(转自hzwer)
  4. MFC 进度条控件
  5. Watir资源列表【转】
  6. java总结第二次//数组及面向对象
  7. 基于mjpg_streamer视频服务器移植【转】
  8. 使用BusyBox制作根文件系统【转】
  9. React笔记_(2)_react语法1
  10. Pro ASP.NET MVC 5 Framework.学习笔记.6.3.MVC的必备工具