DOM解析XML文件实例
XML文件:
response:
- <?xml version="1.0"?>
- <soap:Envelope
- xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
- <soap:Body xmlns:m="http://www.nwpu.edu.cn/soa/xml/test ">
- <m:GetWeatherResponse>
- <m:Temperature>13.2</m:Temperature>
- <m:Weather >sunny</m:Weather >
- </m:GetWeatherResponse>
- </soap:Body>
- </soap:Envelope>
request:
- <?xml version="1.0"?>
- <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
- <soap:Body xmlns:n="http://www.nwpu.edu.cn/soa/xml/test">
- <n:GetWeather>
- <n:CityName>西安</n:CityName>
- </n:GetWeather>
- </soap:Body>
- </soap:Envelope>
解析函数:
- package com.wjy.marshal;
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- public class GetCityName
- {
- private String xmlFilePath="C://Documents and Settings/Administrator/桌面/request.xml";
- public String getCityName()
- {
- String result = "";
- try {
- // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- //System.out.println("class name: " + dbf.getClass().getName());
- // step 2:获得具体的dom解析器
- DocumentBuilder db = dbf.newDocumentBuilder();
- //System.out.println("class name: " + db.getClass().getName());
- // step3: 解析一个xml文档,获得Document对象(根结点)
- Document document = db.parse(new File(xmlFilePath));
- NodeList nodeList=document.getElementsByTagName("n:GetWeather");
- Element element=(Element)nodeList.item();
- result=element.getElementsByTagName("n:CityName").item().getFirstChild().getNodeValue();
- } catch (Exception e) {
- // TODO: handle exception
- }
- return result;
- }
- }
- package com.wjy.marshal;
- import java.io.File;
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- public class GetCityWeather
- {
- private String xmlFilePath="C://Documents and Settings/Administrator/桌面/response.xml";
- public String getCityWeather()
- {
- String tempurature = "";
- String weather="";
- try {
- // step 1: 获得dom解析器工厂(工作的作用是用于创建具体的解析器)
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- //System.out.println("class name: " + dbf.getClass().getName());
- // step 2:获得具体的dom解析器
- DocumentBuilder db = dbf.newDocumentBuilder();
- //System.out.println("class name: " + db.getClass().getName());
- // step3: 解析一个xml文档,获得Document对象(根结点)
- Document document = db.parse(new File(xmlFilePath));
- NodeList nodeList=document.getElementsByTagName("m:GetWeatherResponse");
- Element element=(Element)nodeList.item();
- tempurature=element.getElementsByTagName("m:Temperature").item().getFirstChild().getNodeValue();
- weather=element.getElementsByTagName("m:Weather").item().getFirstChild().getNodeValue();
- System.out.println(tempurature+" "+weather);
- } catch (Exception e) {
- // TODO: handle exception
- }
- return tempurature;
- }
- }
主函数:
- import com.wjy.marshal.GetCityName;
- import com.wjy.marshal.GetCityWeather;
- public class zhu {
- public static void main(String args[]){
- GetCityWeather getCityWeather=new GetCityWeather();
- getCityWeather.getCityWeather();
- GetCityName getCityName=new GetCityName();
- System.out.println(getCityName.getCityName());
- }
- }
DOM解析XML文件实例的更多相关文章
- 使用DOM解析xml文件
使用DOM解析xml文件 要解析的xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <Langu ...
- DOM解析XML文件例子
DOM解析XML文件是一次性将目标文件中的所有节点都读入,然后再进行后续操作的方式. 一般分为以下几步: 1. 定义好目标XML文件路径path . 2. 实例化DOM解析工厂对象 ,Document ...
- JAVA中使用DOM解析XML文件
XML是一种方便快捷高效的数据保存传输的格式,在JSON广泛使用之前,XML是服务器和客户端之间数据传输的主要方式.因此,需要使用各种方式,解析服务器传送过来的信息,以供使用者查看. JAVA作为一种 ...
- 使用DOM解析XML文件,、读取xml文件、保存xml、增加节点、修改节点属性、删除节点
使用的xml文件 <?xml version="1.0" encoding="GB2312" ?> <PhoneInfo> <Br ...
- SAX方式解析XML文件实例
books.XML文件: 书籍book.java实体类: public class Book { private String id; private String name; private Str ...
- DOM方式解析XML文件实例
books.XML文件: <?xml version="1.0" encoding="utf-8"?><bookstore> &l ...
- Qt中使用DOM解析XML文件或者字符串二(实例)
介绍 在Qt中提供了QtXml模块实现了对XML数据的处理,我们在Qt帮助中输入关键字QtXml Module,可以看到该模块的类表.在这里我们可以看到所有相关的类,它们主要是服务于两种操作XML文档 ...
- Qt中使用DOM解析XML文件或者字符串(实例)
因为需要读取配置文件,我的配置文件采用xml:因此编写了使用qt读取xml文件内容的代码,xml文件如下: <?xml version="1.0" encoding=&quo ...
- Dom解析XML文件具体用法
public class Dom4j { public static void main(String[] args) throws Exception { List<Student> l ...
随机推荐
- 深入学习微框架:Spring Boot
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过 ...
- 【状态DP】 HDU 1074 Doing Homework
原题直通车:HDU 1074 Doing Homework 题意:有n门功课需要完成,每一门功课都有时间期限t.完成需要的时间d,如果完成的时间走出时间限制,就会被减 (d-t)个学分.问:按怎样 ...
- 什么是防盗链设置中的空Referer
设置防盗链时候指明和不指明空Referer的差别及实现后的效果? 什么是Referer? 这里的 Referer 指的是HTTP头部的一个字段,也称为HTTP来源地址(HTTP Referer).用来 ...
- linux shell编程指南第十八章------控制流结构
在书写正确脚本前,大概讲一下退出状态.任何命令进行时都将返回一个退出状态.如 果要观察其退出状态,使用最后状态命令: $ echo $? 主要有4种退出状态.前面已经讲到了两种,即最后命令退出状态$ ...
- 百度地图new BMap.LocalCity() 问题
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- c++ try throw catch
c++ try throw catch 这三者联合使用 , try { statement list; } catch( typeA arg ) { statement list; } catch( ...
- objective-c 中数据类型之中的一个 几何数据类型(CGPoint,CGSize,CGRect)
// CGPoint 结构体数据原型, 用于声明一个点: /* Points. */ struct CGPoint { CGFloat x; CGFloat y; }; typedef struct ...
- Android Fragement学习笔记(三)----PreferenceFragment的使用
相信大家对Perference都比較熟悉了,也就是我们常说的偏好设置,首选项设置,能够保存一些数据,比如我们在上一次使用的时候的一些内容,希望在下一次启动后依旧生效,而不须要再进行配置那么麻烦.一般这 ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- linux内核基础(系统调用,简明)
内核基础(系统调用) 在说系统调用之前.先来说说内核是怎么和我们交互的.或者说是怎么和我们产生交集的. 首先,内核是用来控制硬件的仅仅有内核才干直接控制硬件,所以说内核非常重要,假设内核被控制那么电脑 ...