云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > XML基础教程2版第第4章(DOM解析器)代码

XML基础教程2版第第4章(DOM解析器)代码

  • 62 次阅读
  • 3 次下载
  • 2026/4/29 1:25:32

第4章 DOM解析器

4.1 DOM解析器 例子1

example4_1.xml

<姓名>张三 <姓名> 李四

<奖励>一等奖学金

JAXPOne.java

import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class JAXPOne{

public static void main(String args[]){ try { DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance();

DocumentBuilder domPaser=factory.newDocumentBuilder(); Document document=domPaser.parse(new File(\ Element root=document.getDocumentElement(); String rootName=root.getNodeName();

System.out.println(\文件根节点的名字:\ NodeList nodelist=root.getElementsByTagName(\姓名\ int size=nodelist.getLength(); for(int k=0;k

Node node=nodelist.item(k); String name=node.getNodeName(); String content=node.getTextContent();

63

System.out.print(name); System.out.println(\ } } catch(Exception e){ System.out.println(e); } } }

4.2 节点的类型 例子2

example4_2.xml

<雇员>

<姓名> 张三 <年龄> 25岁 <工资> 3190元/月 <雇员>

<姓名> 李四 <年龄> 35岁 <工资> 4320元/月

JAXPTwo.java

import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class JAXPTwo{

public static void main(String args[]){ try{ DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();

DocumentBuilder domParser= factory.newDocumentBuilder();

64

Document document=domParser.parse(new File(\ NodeList nodeList=document.getChildNodes(); output(nodeList); } catch(Exception e){ System.out.println(e); } }

public static void output(NodeList nodeList){ //output是一个递归方法 int size=nodeList.getLength(); for(int k=0;k

if(node.getNodeType()==Node.TEXT_NODE){ Text textNode=(Text)node;

String content=textNode.getWholeText(); System.out.print(content); }

if(node.getNodeType()==Node.ELEMENT_NODE){ Element elementNode=(Element)node; String name=elementNode.getNodeName(); System.out.print(name+\

NodeList nodes=elementNode.getChildNodes(); output(nodes); //递归调用 } } } }

4.4 Element节点 例子3

example4_3.xml

<商品名称 分类=\家电\ 电视机

65

<商品名称 分类=\服装\ 雅格尔西装

<商品名称 分类=\食品\ 东北大米

JAXPThree.java

import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class JAXPThree{

public static void main(String args[]){ try { DocumentBuilderFactory factory= DocumentBuilderFactory. newInstance();

DocumentBuilder domPaser=factory.newDocumentBuilder();

Document document=domPaser.parse(new File(\ Element root=document.getDocumentElement() ; NodeList nodeList=root.getChildNodes(); int size=nodeList.getLength(); for(int k=0;k

if(node.getNodeType()==Node.ELEMENT_NODE){ Element elementNode=(Element)node; String name=elementNode.getNodeName(); String id=elementNode.getAttribute(\分类\ String content=elementNode.getTextContent(); System.out.print(name); System.out.print(\ System.out.println(\:\ } } }

catch(Exception e){} } }

66

  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

第4章 DOM解析器 4.1 DOM解析器 例子1 example4_1.xml

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com